diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java index 1098dfbce47..7f12944cb2b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Tag.java @@ -138,9 +138,12 @@ public class Tag extends BaseElement implements IElement { return result; } + /** + * Returns true if either scheme or term is populated. + */ @Override public boolean isEmpty() { - return StringUtils.isBlank(myLabel) && StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm); + return StringUtils.isBlank(myScheme) && StringUtils.isBlank(myTerm); } /** diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java index 3ad863bbfcb..fd72f3bcc25 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/TagList.java @@ -191,7 +191,12 @@ public class TagList implements Set, Serializable, IBase { @Override public boolean isEmpty() { - return myTagSet.isEmpty(); + for (Tag next : myTagSet) { + if (next.isEmpty() == false) { + return false; + } + } + return true; } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index fba5c4dd3cb..62dce8b6dbe 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -754,6 +754,9 @@ public class JsonParser extends BaseParser implements IParser { if (tags != null && tags.isEmpty() == false) { theEventWriter.writeStartArray("tag"); for (Tag tag : tags) { + if (tag.isEmpty()) { + continue; + } theEventWriter.writeStartObject(); writeOptionalTagWithTextNode(theEventWriter, "system", tag.getScheme()); writeOptionalTagWithTextNode(theEventWriter, "code", tag.getTerm()); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java index 38e1ee45426..1bb8dcdb876 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java @@ -820,6 +820,9 @@ public class XmlParser extends BaseParser implements IParser { } if (tags != null) { for (Tag tag : tags) { + if (tag.isEmpty()) { + continue; + } theEventWriter.writeStartElement("tag"); writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme()); writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm()); diff --git a/hapi-fhir-structures-dstu2/pom.xml b/hapi-fhir-structures-dstu2/pom.xml index 3e77cc10f56..ab3c0033a49 100644 --- a/hapi-fhir-structures-dstu2/pom.xml +++ b/hapi-fhir-structures-dstu2/pom.xml @@ -36,6 +36,12 @@ junit ${junit_version} test + + + hamcrest-core + org.hamcrest + + xmlunit diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java index 5b3c0218af8..0d16dae3db7 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java @@ -55,430 +55,8 @@ import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.StringDt; public class JsonParserDstu2Test { - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class); private static final FhirContext ourCtx = FhirContext.forDstu2(); - - @Test - public void testOmitResourceId() { - Patient p = new Patient(); - p.setId("123"); - p.addName().addFamily("ABC"); - - assertThat(ourCtx.newJsonParser().encodeResourceToString(p), stringContainsInOrder("123", "ABC")); - assertThat(ourCtx.newJsonParser().setOmitResourceId(true).encodeResourceToString(p), containsString("ABC")); - assertThat(ourCtx.newJsonParser().setOmitResourceId(true).encodeResourceToString(p), not(containsString("123"))); - } - - @Test - public void testParseAndEncodeBundleWithUuidBase() { - //@formatter:off - String input = - "{\n" + - " \"resourceType\":\"Bundle\",\n" + - " \"type\":\"document\",\n" + - " \"entry\":[\n" + - " {\n" + - " \"base\":\"urn:uuid:\",\n" + - " \"resource\":{\n" + - " \"resourceType\":\"Composition\",\n" + - " \"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" + - " \"meta\":{\n" + - " \"lastUpdated\":\"2013-05-28T22:12:21Z\"\n" + - " },\n" + - " \"text\":{\n" + - " \"status\":\"generated\",\n" + - " \"div\":\"

Generated Narrative with Details

id: 180f219f-97a8-486d-99d9-ed631fe4fc57

meta:

date: Feb 1, 2013 12:30:02 PM

type: Discharge Summary from Responsible Clinician (Details : {LOINC code '28655-9' = 'Physician attending Discharge summary)

status: final

confidentiality: N

author: Doctor Dave. Generated Summary: 23; Adam Careful

encounter: http://fhir.healthintersections.com.au/open/Encounter/doc-example

\"\n" + - " },\n" + - " \"date\":\"2013-02-01T12:30:02Z\",\n" + - " \"type\":{\n" + - " \"coding\":[\n" + - " {\n" + - " \"system\":\"http://loinc.org\",\n" + - " \"code\":\"28655-9\"\n" + - " }\n" + - " ],\n" + - " \"text\":\"Discharge Summary from Responsible Clinician\"\n" + - " },\n" + - " \"status\":\"final\",\n" + - " \"confidentiality\":\"N\",\n" + - " \"subject\":{\n" + - " \"reference\":\"http://fhir.healthintersections.com.au/open/Patient/d1\",\n" + - " \"display\":\"Eve Everywoman\"\n" + - " },\n" + - " \"author\":[\n" + - " {\n" + - " \"reference\":\"Practitioner/example\",\n" + - " \"display\":\"Doctor Dave\"\n" + - " }\n" + - " ],\n" + - " \"encounter\":{\n" + - " \"reference\":\"http://fhir.healthintersections.com.au/open/Encounter/doc-example\"\n" + - " },\n" + - " \"section\":[\n" + - " {\n" + - " \"title\":\"Reason for admission\",\n" + - " \"content\":{\n" + - " \"reference\":\"urn:uuid:d0dd51d3-3ab2-4c84-b697-a630c3e40e7a\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"title\":\"Medications on Discharge\",\n" + - " \"content\":{\n" + - " \"reference\":\"urn:uuid:673f8db5-0ffd-4395-9657-6da00420bbc1\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"title\":\"Known allergies\",\n" + - " \"content\":{\n" + - " \"reference\":\"urn:uuid:68f86194-e6e1-4f65-b64a-5314256f8d7b\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }" + - " ]" + - "}"; - //@formatter:on - - ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); - - String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); - ourLog.info(encoded); - - assertEquals("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57", parsed.getEntry().get(0).getResource().getId().getValue()); - assertEquals("urn:uuid:", parsed.getEntry().get(0).getResource().getId().getBaseUrl()); - assertEquals("180f219f-97a8-486d-99d9-ed631fe4fc57", parsed.getEntry().get(0).getResource().getId().getIdPart()); - assertThat(encoded, containsString("\"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\"")); - } - - - @Test - public void testParseBundleWithBinary() { - Binary patient = new Binary(); - patient.setId(new IdDt("http://base/Binary/11/_history/22")); - patient.setContentType("foo"); - patient.setContent(new byte[] { 1, 2, 3, 4 }); - - String val = ourCtx.newJsonParser().encodeResourceToString(patient); - assertEquals("{\"resourceType\":\"Binary\",\"id\":\"11\",\"meta\":{\"versionId\":\"22\"},\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val); - } - - - @Test - public void testEncodingNullExtension() { - Patient p = new Patient(); - ExtensionDt extension = new ExtensionDt(false, "http://foo#bar"); - p.addUndeclaredExtension(extension); - String str = ourCtx.newJsonParser().encodeResourceToString(p); - - assertEquals("{\"resourceType\":\"Patient\"}", str); - - extension.setValue(new StringDt()); - - str = ourCtx.newJsonParser().encodeResourceToString(p); - assertEquals("{\"resourceType\":\"Patient\"}", str); - - extension.setValue(new StringDt("")); - - str = ourCtx.newJsonParser().encodeResourceToString(p); - assertEquals("{\"resourceType\":\"Patient\"}", str); - - } - - /** - * See #163 - */ - @Test - public void testParseResourceType() { - IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); - - // Patient - Patient patient = new Patient(); - String patientId = UUID.randomUUID().toString(); - patient.setId(new IdDt("Patient", patientId)); - patient.addName().addGiven("John").addFamily("Smith"); - patient.setGender(AdministrativeGenderEnum.MALE); - patient.setBirthDate(new DateDt("1987-04-16")); - - // Bundle - ca.uhn.fhir.model.dstu2.resource.Bundle bundle = new ca.uhn.fhir.model.dstu2.resource.Bundle(); - bundle.setType(BundleTypeEnum.COLLECTION); - bundle.addEntry().setResource(patient); - - String bundleText = jsonParser.encodeResourceToString(bundle); - ourLog.info(bundleText); - - ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = jsonParser.parseResource (ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText); - Patient reincarnatedPatient = (Patient) reincarnatedBundle.getEntry().get(0).getResource(); - - assertEquals("Patient", patient.getId().getResourceType()); - assertEquals("Patient", reincarnatedPatient.getId().getResourceType()); - } - - /** - * See #144 and #146 - */ - @Test - public void testReportSerialize() { - - ReportObservation obsv = new ReportObservation(); - obsv.getCode().addCoding().setCode("name"); - obsv.setValue(new StringDt("value test")); - obsv.setStatus(ObservationStatusEnum.FINAL); - obsv.setReliability(ObservationReliabilityEnum.OK); - obsv.addIdentifier().setSystem("System").setValue("id value"); - - DiagnosticReport report = new DiagnosticReport(); - report.getContained().getContainedResources().add(obsv); - report.addResult().setResource(obsv); - - IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); - String message = parser.encodeResourceToString(report); - ourLog.info(message); - Assert.assertThat(message, containsString("contained")); - } - - @Test - public void testEncodeBundleOldBundleNoText() { - - Bundle b = new Bundle(); - - BundleEntry e = b.addEntry(); - e.setResource(new Patient()); - b.addCategory("scheme", "term", "label"); - - String val = ourCtx.newJsonParser().setPrettyPrint(false).encodeBundleToString(b); - ourLog.info(val); - - assertThat(val, not(containsString("text"))); - - b = ourCtx.newJsonParser().parseBundle(val); - assertEquals(1, b.getEntries().size()); - - } - - @Test - public void testEncodeBundleNewBundleNoText() { - - ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle(); - b.getText().setDiv(""); - b.getText().getStatus().setValueAsString(""); - ; - - Entry e = b.addEntry(); - e.setResource(new Patient()); - - String val = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(b); - ourLog.info(val); - assertThat(val, not(containsString("text"))); - - val = ourCtx.newXmlParser().setPrettyPrint(false).encodeResourceToString(b); - ourLog.info(val); - assertThat(val, not(containsString("text"))); - - } - - /** - * See #144 and #146 - */ - @Test - public void testReportSerializeWithMatchingId() { - - ReportObservation obsv = new ReportObservation(); - obsv.getCode().addCoding().setCode("name"); - obsv.setValue(new StringDt("value test")); - obsv.setStatus(ObservationStatusEnum.FINAL); - obsv.setReliability(ObservationReliabilityEnum.OK); - obsv.addIdentifier().setSystem("System").setValue("id value"); - - DiagnosticReport report = new DiagnosticReport(); - report.getContained().getContainedResources().add(obsv); - - obsv.setId("#123"); - report.addResult().setReference("#123"); - - IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); - String message = parser.encodeResourceToString(report); - ourLog.info(message); - Assert.assertThat(message, containsString("contained")); - } - - /** - * see #144 and #146 - */ - @Test - public void testParseContained() { - - FhirContext c = FhirContext.forDstu2(); - IParser parser = c.newJsonParser().setPrettyPrint(true); - - Observation o = new Observation(); - o.getCode().setText("obs text"); - - Patient p = new Patient(); - p.addName().addFamily("patient family"); - o.getSubject().setResource(p); - - String enc = parser.encodeResourceToString(o); - ourLog.info(enc); - - //@formatter:off - assertThat(enc, stringContainsInOrder( - "\"resourceType\":\"Observation\"", - "\"contained\":[", - "\"resourceType\":\"Patient\",", - "\"id\":\"1\"", - "\"reference\":\"#1\"" - )); - //@formatter:on - - o = parser.parseResource(Observation.class, enc); - assertEquals("obs text", o.getCode().getText()); - - assertNotNull(o.getSubject().getResource()); - p = (Patient) o.getSubject().getResource(); - assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString()); - } - - @Test - public void testEncodeAndParseMetaProfileAndTags() { - Patient p = new Patient(); - p.addName().addFamily("FAMILY"); - - List profiles = new ArrayList(); - profiles.add(new IdDt("http://foo/Profile1")); - profiles.add(new IdDt("http://foo/Profile2")); - ResourceMetadataKeyEnum.PROFILES.put(p, profiles); - - TagList tagList = new TagList(); - tagList.addTag("scheme1", "term1", "label1"); - tagList.addTag("scheme2", "term2", "label2"); - ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); - - String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p); - ourLog.info(enc); - - //@formatter:off - assertThat(enc, stringContainsInOrder("\"meta\":{", - "\"profile\":[", - "\"http://foo/Profile1\",", - "\"http://foo/Profile2\"", - "],", - "\"tag\":[", - "{", - "\"system\":\"scheme1\",", - "\"code\":\"term1\",", - "\"display\":\"label1\"", - "},", - "{", - "\"system\":\"scheme2\",", - "\"code\":\"term2\",", - "\"display\":\"label2\"", - "}", - "]", - "},")); - //@formatter:on - - Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc); - List gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed); - - assertEquals(2, gotLabels.size()); - - IdDt label = (IdDt) gotLabels.get(0); - assertEquals("http://foo/Profile1", label.getValue()); - label = (IdDt) gotLabels.get(1); - assertEquals("http://foo/Profile2", label.getValue()); - - tagList = ResourceMetadataKeyEnum.TAG_LIST.get(parsed); - assertEquals(2, tagList.size()); - - assertEquals(new Tag("scheme1", "term1", "label1"), tagList.get(0)); - assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1)); - } - - @Test - public void testEncodeAndParseSecurityLabels() { - Patient p = new Patient(); - p.addName().addFamily("FAMILY"); - - List labels = new ArrayList(); - labels.add(new CodingDt().setSystem("SYSTEM1").setCode("CODE1").setDisplay("DISPLAY1").setPrimary(true).setVersion("VERSION1")); - labels.add(new CodingDt().setSystem("SYSTEM2").setCode("CODE2").setDisplay("DISPLAY2").setPrimary(false).setVersion("VERSION2")); - - ResourceMetadataKeyEnum.SECURITY_LABELS.put(p, labels); - - String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p); - ourLog.info(enc); - - //@formatter:off - assertEquals("{\n" + - " \"resourceType\":\"Patient\",\n" + - " \"meta\":{\n" + - " \"security\":[\n" + - " {\n" + - " \"system\":\"SYSTEM1\",\n" + - " \"version\":\"VERSION1\",\n" + - " \"code\":\"CODE1\",\n" + - " \"display\":\"DISPLAY1\",\n" + - " \"primary\":true\n" + - " },\n" + - " {\n" + - " \"system\":\"SYSTEM2\",\n" + - " \"version\":\"VERSION2\",\n" + - " \"code\":\"CODE2\",\n" + - " \"display\":\"DISPLAY2\",\n" + - " \"primary\":false\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"name\":[\n" + - " {\n" + - " \"family\":[\n" + - " \"FAMILY\"\n" + - " ]\n" + - " }\n" + - " ]\n" + - "}", enc.trim()); - //@formatter:on - - Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc); - List gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed); - - assertEquals(2, gotLabels.size()); - - CodingDt label = (CodingDt) gotLabels.get(0); - assertEquals("SYSTEM1", label.getSystem()); - assertEquals("CODE1", label.getCode()); - assertEquals("DISPLAY1", label.getDisplay()); - assertEquals(true, label.getPrimary()); - assertEquals("VERSION1", label.getVersion()); - - label = (CodingDt) gotLabels.get(1); - assertEquals("SYSTEM2", label.getSystem()); - assertEquals("CODE2", label.getCode()); - assertEquals("DISPLAY2", label.getDisplay()); - assertEquals(false, label.getPrimary()); - assertEquals("VERSION2", label.getVersion()); - } - - /** - * Fixing #89 - */ - @Test - public void testEncodeBundleWithDeletedEntry() throws ConfigurationException, DataFormatException, IOException { - Bundle b = ourCtx.newXmlParser().parseBundle(IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/xml-bundle.xml"))); - String val = ourCtx.newJsonParser().encodeBundleToString(b); - - ourLog.info(val); - - //@formatter:off - assertThat(val, containsString("\"deleted\":{" + - "\"type\":\"Patient\"," + - "\"resourceId\":\"4384\"," + - "\"instant\":\"2015-01-15T11:04:43.054-05:00\"" + - "}")); - //@formatter:on - } + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class); @Test public void testEncodeAndParseExtensions() throws Exception { @@ -565,6 +143,243 @@ public class JsonParserDstu2Test { } + @Test + public void testEncodeAndParseMetaProfileAndTags() { + Patient p = new Patient(); + p.addName().addFamily("FAMILY"); + + List profiles = new ArrayList(); + profiles.add(new IdDt("http://foo/Profile1")); + profiles.add(new IdDt("http://foo/Profile2")); + ResourceMetadataKeyEnum.PROFILES.put(p, profiles); + + TagList tagList = new TagList(); + tagList.addTag("scheme1", "term1", "label1"); + tagList.addTag("scheme2", "term2", "label2"); + ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); + + String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p); + ourLog.info(enc); + + //@formatter:off + assertThat(enc, stringContainsInOrder("\"meta\":{", + "\"profile\":[", + "\"http://foo/Profile1\",", + "\"http://foo/Profile2\"", + "],", + "\"tag\":[", + "{", + "\"system\":\"scheme1\",", + "\"code\":\"term1\",", + "\"display\":\"label1\"", + "},", + "{", + "\"system\":\"scheme2\",", + "\"code\":\"term2\",", + "\"display\":\"label2\"", + "}", + "]", + "},")); + //@formatter:on + + Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc); + List gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed); + + assertEquals(2, gotLabels.size()); + + IdDt label = (IdDt) gotLabels.get(0); + assertEquals("http://foo/Profile1", label.getValue()); + label = (IdDt) gotLabels.get(1); + assertEquals("http://foo/Profile2", label.getValue()); + + tagList = ResourceMetadataKeyEnum.TAG_LIST.get(parsed); + assertEquals(2, tagList.size()); + + assertEquals(new Tag("scheme1", "term1", "label1"), tagList.get(0)); + assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1)); + } + + /** + * #158 + */ + @Test + public void testEncodeEmptyTag2() { + TagList tagList = new TagList(); + tagList.addTag("scheme", "code", null); + tagList.addTag(null, null, "Label"); + + Patient p = new Patient(); + ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); + + String encoded = ourCtx.newJsonParser().encodeResourceToString(p); + assertThat(encoded, containsString("tag")); + assertThat(encoded, containsString("scheme")); + assertThat(encoded, not(containsString("Label"))); + } + + @Test + public void testEncodeAndParseSecurityLabels() { + Patient p = new Patient(); + p.addName().addFamily("FAMILY"); + + List labels = new ArrayList(); + labels.add(new CodingDt().setSystem("SYSTEM1").setCode("CODE1").setDisplay("DISPLAY1").setPrimary(true).setVersion("VERSION1")); + labels.add(new CodingDt().setSystem("SYSTEM2").setCode("CODE2").setDisplay("DISPLAY2").setPrimary(false).setVersion("VERSION2")); + + ResourceMetadataKeyEnum.SECURITY_LABELS.put(p, labels); + + String enc = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p); + ourLog.info(enc); + + //@formatter:off + assertEquals("{\n" + + " \"resourceType\":\"Patient\",\n" + + " \"meta\":{\n" + + " \"security\":[\n" + + " {\n" + + " \"system\":\"SYSTEM1\",\n" + + " \"version\":\"VERSION1\",\n" + + " \"code\":\"CODE1\",\n" + + " \"display\":\"DISPLAY1\",\n" + + " \"primary\":true\n" + + " },\n" + + " {\n" + + " \"system\":\"SYSTEM2\",\n" + + " \"version\":\"VERSION2\",\n" + + " \"code\":\"CODE2\",\n" + + " \"display\":\"DISPLAY2\",\n" + + " \"primary\":false\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"name\":[\n" + + " {\n" + + " \"family\":[\n" + + " \"FAMILY\"\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}", enc.trim()); + //@formatter:on + + Patient parsed = ourCtx.newJsonParser().parseResource(Patient.class, enc); + List gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed); + + assertEquals(2, gotLabels.size()); + + CodingDt label = (CodingDt) gotLabels.get(0); + assertEquals("SYSTEM1", label.getSystem()); + assertEquals("CODE1", label.getCode()); + assertEquals("DISPLAY1", label.getDisplay()); + assertEquals(true, label.getPrimary()); + assertEquals("VERSION1", label.getVersion()); + + label = (CodingDt) gotLabels.get(1); + assertEquals("SYSTEM2", label.getSystem()); + assertEquals("CODE2", label.getCode()); + assertEquals("DISPLAY2", label.getDisplay()); + assertEquals(false, label.getPrimary()); + assertEquals("VERSION2", label.getVersion()); + } + + + @Test + public void testEncodeBundleNewBundleNoText() { + + ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle(); + b.getText().setDiv(""); + b.getText().getStatus().setValueAsString(""); + ; + + Entry e = b.addEntry(); + e.setResource(new Patient()); + + String val = ourCtx.newJsonParser().setPrettyPrint(false).encodeResourceToString(b); + ourLog.info(val); + assertThat(val, not(containsString("text"))); + + val = ourCtx.newXmlParser().setPrettyPrint(false).encodeResourceToString(b); + ourLog.info(val); + assertThat(val, not(containsString("text"))); + + } + + + @Test + public void testEncodeBundleOldBundleNoText() { + + Bundle b = new Bundle(); + + BundleEntry e = b.addEntry(); + e.setResource(new Patient()); + b.addCategory("scheme", "term", "label"); + + String val = ourCtx.newJsonParser().setPrettyPrint(false).encodeBundleToString(b); + ourLog.info(val); + + assertThat(val, not(containsString("text"))); + + b = ourCtx.newJsonParser().parseBundle(val); + assertEquals(1, b.getEntries().size()); + + } + + /** + * Fixing #89 + */ + @Test + public void testEncodeBundleWithDeletedEntry() throws ConfigurationException, DataFormatException, IOException { + Bundle b = ourCtx.newXmlParser().parseBundle(IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/xml-bundle.xml"))); + String val = ourCtx.newJsonParser().encodeBundleToString(b); + + ourLog.info(val); + + //@formatter:off + assertThat(val, containsString("\"deleted\":{" + + "\"type\":\"Patient\"," + + "\"resourceId\":\"4384\"," + + "\"instant\":\"2015-01-15T11:04:43.054-05:00\"" + + "}")); + //@formatter:on + } + + /** + * #158 + */ + @Test + public void testEncodeEmptyTag() { + TagList tagList = new TagList(); + tagList.addTag(null, null, null); + tagList.addTag(null, null, "Label"); + + Patient p = new Patient(); + ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); + + String encoded = ourCtx.newJsonParser().encodeResourceToString(p); + assertThat(encoded, not(containsString("tag"))); + } + + @Test + public void testEncodingNullExtension() { + Patient p = new Patient(); + ExtensionDt extension = new ExtensionDt(false, "http://foo#bar"); + p.addUndeclaredExtension(extension); + String str = ourCtx.newJsonParser().encodeResourceToString(p); + + assertEquals("{\"resourceType\":\"Patient\"}", str); + + extension.setValue(new StringDt()); + + str = ourCtx.newJsonParser().encodeResourceToString(p); + assertEquals("{\"resourceType\":\"Patient\"}", str); + + extension.setValue(new StringDt("")); + + str = ourCtx.newJsonParser().encodeResourceToString(p); + assertEquals("{\"resourceType\":\"Patient\"}", str); + + } + /** * #65 */ @@ -581,6 +396,323 @@ public class JsonParserDstu2Test { } + @Test + public void testOmitResourceId() { + Patient p = new Patient(); + p.setId("123"); + p.addName().addFamily("ABC"); + + assertThat(ourCtx.newJsonParser().encodeResourceToString(p), stringContainsInOrder("123", "ABC")); + assertThat(ourCtx.newJsonParser().setOmitResourceId(true).encodeResourceToString(p), containsString("ABC")); + assertThat(ourCtx.newJsonParser().setOmitResourceId(true).encodeResourceToString(p), not(containsString("123"))); + } + + @Test + public void testParseAndEncodeBundle() throws Exception { + String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); + + Bundle parsed = ourCtx.newJsonParser().parseBundle(content); + assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue()); + assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION)); + assertEquals("1", parsed.getId().getVersionIdPart()); + assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); + assertEquals("searchset", parsed.getType().getValue()); + assertEquals(3, parsed.getTotalResults().getValue().intValue()); + assertEquals("http://example.com/base", parsed.getLinkBase().getValue()); + assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2", parsed.getLinkNext().getValue()); + assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLinkSelf().getValue()); + + assertEquals(2, parsed.getEntries().size()); + + MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource(); + assertEquals("Patient/347", p.getPatient().getReference().getValue()); + assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); + assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); + + Medication m = (Medication) parsed.getEntries().get(1).getResource(); + assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); + assertSame(p.getMedication().getResource(), m); + + String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); + ourLog.info(reencoded); + + JsonConfig cfg = new JsonConfig(); + + JSON expected = JSONSerializer.toJSON(content.trim(), cfg); + JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); + + String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); + String act = actual.toString().replace("\\r\\n", "\\n"); + + ourLog.info("Expected: {}", exp); + ourLog.info("Actual : {}", act); + + assertEquals(exp, act); + + } + + /** + * Test for #146 + */ + @Test + public void testParseAndEncodeBundleFromXmlToJson() throws Exception { + String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example2.xml")); + + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content); + + MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); + assertEquals("#med", p.getMedication().getReference().getValue()); + + Medication m = (Medication) p.getMedication().getResource(); + assertNotNull(m); + assertEquals("#med", m.getId().getValue()); + assertEquals(1, p.getContained().getContainedResources().size()); + assertSame(m, p.getContained().getContainedResources().get(0)); + + String reencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(parsed); + ourLog.info(reencoded); + assertThat(reencoded, containsString("contained")); + + reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); + ourLog.info(reencoded); + assertThat(reencoded, containsString("contained")); + } + + @Test + public void testParseAndEncodeBundleNewStyle() throws Exception { + String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); + + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content); + assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue()); + assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION)); + assertEquals("1", parsed.getId().getVersionIdPart()); + assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); + assertEquals("searchset", parsed.getType()); + assertEquals(3, parsed.getTotal().intValue()); + assertEquals("http://example.com/base", parsed.getBaseElement().getValueAsString()); + assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2", parsed.getLink().get(0).getUrlElement().getValueAsString()); + assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLink().get(1).getUrlElement().getValueAsString()); + + assertEquals(2, parsed.getEntry().size()); + + MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); + assertEquals("Patient/347", p.getPatient().getReference().getValue()); + assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); + assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); + + Medication m = (Medication) parsed.getEntry().get(1).getResource(); + assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); + assertEquals("Medication/example", p.getMedication().getReference().getValue()); + assertSame(p.getMedication().getResource(), m); + + String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); + ourLog.info(reencoded); + + JsonConfig cfg = new JsonConfig(); + + JSON expected = JSONSerializer.toJSON(content.trim(), cfg); + JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); + + String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); + String act = actual.toString().replace("\\r\\n", "\\n"); + + ourLog.info("Expected: {}", exp); + ourLog.info("Actual : {}", act); + + assertEquals(exp, act); + + } + + @Test + public void testParseAndEncodeBundleOldStyle() throws Exception { + String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); + + Bundle parsed = ourCtx.newJsonParser().parseBundle(content); + + assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); + assertEquals("searchset", parsed.getType().getValue()); + assertEquals(3, parsed.getTotalResults().getValue().intValue()); + + assertEquals(2, parsed.getEntries().size()); + + MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource(); + assertEquals("Patient/347", p.getPatient().getReference().getValue()); + assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); + assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); + + Medication m = (Medication) parsed.getEntries().get(1).getResource(); + assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); + assertEquals("Medication/example", p.getMedication().getReference().getValue()); + assertSame(p.getMedication().getResource(), m); + + String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); + ourLog.info(reencoded); + + JsonConfig cfg = new JsonConfig(); + + JSON expected = JSONSerializer.toJSON(content.trim(), cfg); + JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); + + String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); + String act = actual.toString().replace("\\r\\n", "\\n"); + + exp = exp.replace(",\"ifNoneExist\":\"Patient?identifier=234234\"", ""); + + ourLog.info("Expected: {}", exp); + ourLog.info("Actual : {}", act); + + assertEquals(exp, act); + + } + + @Test + public void testParseAndEncodeBundleWithDeletedEntry() { + + Patient res = new Patient(); + res.setId(new IdDt("Patient", "111", "222")); + ResourceMetadataKeyEnum.DELETED_AT.put(res, new InstantDt("2011-01-01T12:12:22Z")); + + Bundle bundle = new Bundle(); + bundle.addResource(res, ourCtx, "http://foo/base"); + + String actual = ourCtx.newJsonParser().encodeBundleToString(bundle); + ourLog.info(actual); + + String expected = "{\"resourceType\":\"Bundle\",\"entry\":[{\"deleted\":{\"type\":\"Patient\",\"resourceId\":\"111\",\"versionId\":\"222\",\"instant\":\"2011-01-01T12:12:22Z\"}}]}"; + assertEquals(expected, actual); + + } + + @Test + public void testParseAndEncodeBundleWithUuidBase() { + //@formatter:off + String input = + "{\n" + + " \"resourceType\":\"Bundle\",\n" + + " \"type\":\"document\",\n" + + " \"entry\":[\n" + + " {\n" + + " \"base\":\"urn:uuid:\",\n" + + " \"resource\":{\n" + + " \"resourceType\":\"Composition\",\n" + + " \"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\",\n" + + " \"meta\":{\n" + + " \"lastUpdated\":\"2013-05-28T22:12:21Z\"\n" + + " },\n" + + " \"text\":{\n" + + " \"status\":\"generated\",\n" + + " \"div\":\"

Generated Narrative with Details

id: 180f219f-97a8-486d-99d9-ed631fe4fc57

meta:

date: Feb 1, 2013 12:30:02 PM

type: Discharge Summary from Responsible Clinician (Details : {LOINC code '28655-9' = 'Physician attending Discharge summary)

status: final

confidentiality: N

author: Doctor Dave. Generated Summary: 23; Adam Careful

encounter: http://fhir.healthintersections.com.au/open/Encounter/doc-example

\"\n" + + " },\n" + + " \"date\":\"2013-02-01T12:30:02Z\",\n" + + " \"type\":{\n" + + " \"coding\":[\n" + + " {\n" + + " \"system\":\"http://loinc.org\",\n" + + " \"code\":\"28655-9\"\n" + + " }\n" + + " ],\n" + + " \"text\":\"Discharge Summary from Responsible Clinician\"\n" + + " },\n" + + " \"status\":\"final\",\n" + + " \"confidentiality\":\"N\",\n" + + " \"subject\":{\n" + + " \"reference\":\"http://fhir.healthintersections.com.au/open/Patient/d1\",\n" + + " \"display\":\"Eve Everywoman\"\n" + + " },\n" + + " \"author\":[\n" + + " {\n" + + " \"reference\":\"Practitioner/example\",\n" + + " \"display\":\"Doctor Dave\"\n" + + " }\n" + + " ],\n" + + " \"encounter\":{\n" + + " \"reference\":\"http://fhir.healthintersections.com.au/open/Encounter/doc-example\"\n" + + " },\n" + + " \"section\":[\n" + + " {\n" + + " \"title\":\"Reason for admission\",\n" + + " \"content\":{\n" + + " \"reference\":\"urn:uuid:d0dd51d3-3ab2-4c84-b697-a630c3e40e7a\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"title\":\"Medications on Discharge\",\n" + + " \"content\":{\n" + + " \"reference\":\"urn:uuid:673f8db5-0ffd-4395-9657-6da00420bbc1\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"title\":\"Known allergies\",\n" + + " \"content\":{\n" + + " \"reference\":\"urn:uuid:68f86194-e6e1-4f65-b64a-5314256f8d7b\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }" + + " ]" + + "}"; + //@formatter:on + + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); + + String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); + ourLog.info(encoded); + + assertEquals("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57", parsed.getEntry().get(0).getResource().getId().getValue()); + assertEquals("urn:uuid:", parsed.getEntry().get(0).getResource().getId().getBaseUrl()); + assertEquals("180f219f-97a8-486d-99d9-ed631fe4fc57", parsed.getEntry().get(0).getResource().getId().getIdPart()); + assertThat(encoded, containsString("\"id\":\"180f219f-97a8-486d-99d9-ed631fe4fc57\"")); + } + + @Test + public void testParseBundleWithBinary() { + Binary patient = new Binary(); + patient.setId(new IdDt("http://base/Binary/11/_history/22")); + patient.setContentType("foo"); + patient.setContent(new byte[] { 1, 2, 3, 4 }); + + String val = ourCtx.newJsonParser().encodeResourceToString(patient); + assertEquals("{\"resourceType\":\"Binary\",\"id\":\"11\",\"meta\":{\"versionId\":\"22\"},\"contentType\":\"foo\",\"content\":\"AQIDBA==\"}", val); + } + + /** + * see #144 and #146 + */ + @Test + public void testParseContained() { + + FhirContext c = FhirContext.forDstu2(); + IParser parser = c.newJsonParser().setPrettyPrint(true); + + Observation o = new Observation(); + o.getCode().setText("obs text"); + + Patient p = new Patient(); + p.addName().addFamily("patient family"); + o.getSubject().setResource(p); + + String enc = parser.encodeResourceToString(o); + ourLog.info(enc); + + //@formatter:off + assertThat(enc, stringContainsInOrder( + "\"resourceType\":\"Observation\"", + "\"contained\":[", + "\"resourceType\":\"Patient\",", + "\"id\":\"1\"", + "\"reference\":\"#1\"" + )); + //@formatter:on + + o = parser.parseResource(Observation.class, enc); + assertEquals("obs text", o.getCode().getText()); + + assertNotNull(o.getSubject().getResource()); + p = (Patient) o.getSubject().getResource(); + assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString()); + } + @Test public void testParseMetadata() throws Exception { //@formatter:off @@ -665,179 +797,81 @@ public class JsonParserDstu2Test { } /** - * Test for #146 + * See #163 */ @Test - public void testParseAndEncodeBundleFromXmlToJson() throws Exception { - String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example2.xml")); + public void testParseResourceType() { + IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); - ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content); + // Patient + Patient patient = new Patient(); + String patientId = UUID.randomUUID().toString(); + patient.setId(new IdDt("Patient", patientId)); + patient.addName().addGiven("John").addFamily("Smith"); + patient.setGender(AdministrativeGenderEnum.MALE); + patient.setBirthDate(new DateDt("1987-04-16")); - MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); - assertEquals("#med", p.getMedication().getReference().getValue()); + // Bundle + ca.uhn.fhir.model.dstu2.resource.Bundle bundle = new ca.uhn.fhir.model.dstu2.resource.Bundle(); + bundle.setType(BundleTypeEnum.COLLECTION); + bundle.addEntry().setResource(patient); - Medication m = (Medication) p.getMedication().getResource(); - assertNotNull(m); - assertEquals("#med", m.getId().getValue()); - assertEquals(1, p.getContained().getContainedResources().size()); - assertSame(m, p.getContained().getContainedResources().get(0)); - - String reencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(parsed); - ourLog.info(reencoded); - assertThat(reencoded, containsString("contained")); - - reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); - ourLog.info(reencoded); - assertThat(reencoded, containsString("contained")); + String bundleText = jsonParser.encodeResourceToString(bundle); + ourLog.info(bundleText); + + ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = jsonParser.parseResource (ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText); + Patient reincarnatedPatient = (Patient) reincarnatedBundle.getEntry().get(0).getResource(); + + assertEquals("Patient", patient.getId().getResourceType()); + assertEquals("Patient", reincarnatedPatient.getId().getResourceType()); } + /** + * See #144 and #146 + */ @Test - public void testParseAndEncodeBundle() throws Exception { - String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); + public void testReportSerialize() { - Bundle parsed = ourCtx.newJsonParser().parseBundle(content); - assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue()); - assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION)); - assertEquals("1", parsed.getId().getVersionIdPart()); - assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); - assertEquals("searchset", parsed.getType().getValue()); - assertEquals(3, parsed.getTotalResults().getValue().intValue()); - assertEquals("http://example.com/base", parsed.getLinkBase().getValue()); - assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2", parsed.getLinkNext().getValue()); - assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLinkSelf().getValue()); + ReportObservation obsv = new ReportObservation(); + obsv.getCode().addCoding().setCode("name"); + obsv.setValue(new StringDt("value test")); + obsv.setStatus(ObservationStatusEnum.FINAL); + obsv.setReliability(ObservationReliabilityEnum.OK); + obsv.addIdentifier().setSystem("System").setValue("id value"); - assertEquals(2, parsed.getEntries().size()); - - MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource(); - assertEquals("Patient/347", p.getPatient().getReference().getValue()); - assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); - assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); - - Medication m = (Medication) parsed.getEntries().get(1).getResource(); - assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); - assertSame(p.getMedication().getResource(), m); - - String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); - ourLog.info(reencoded); - - JsonConfig cfg = new JsonConfig(); - - JSON expected = JSONSerializer.toJSON(content.trim(), cfg); - JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); - - String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); - String act = actual.toString().replace("\\r\\n", "\\n"); - - ourLog.info("Expected: {}", exp); - ourLog.info("Actual : {}", act); - - assertEquals(exp, act); + DiagnosticReport report = new DiagnosticReport(); + report.getContained().getContainedResources().add(obsv); + report.addResult().setResource(obsv); + IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); + String message = parser.encodeResourceToString(report); + ourLog.info(message); + Assert.assertThat(message, containsString("contained")); } + /** + * See #144 and #146 + */ @Test - public void testParseAndEncodeBundleOldStyle() throws Exception { - String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); + public void testReportSerializeWithMatchingId() { - Bundle parsed = ourCtx.newJsonParser().parseBundle(content); + ReportObservation obsv = new ReportObservation(); + obsv.getCode().addCoding().setCode("name"); + obsv.setValue(new StringDt("value test")); + obsv.setStatus(ObservationStatusEnum.FINAL); + obsv.setReliability(ObservationReliabilityEnum.OK); + obsv.addIdentifier().setSystem("System").setValue("id value"); - assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); - assertEquals("searchset", parsed.getType().getValue()); - assertEquals(3, parsed.getTotalResults().getValue().intValue()); + DiagnosticReport report = new DiagnosticReport(); + report.getContained().getContainedResources().add(obsv); - assertEquals(2, parsed.getEntries().size()); - - MedicationPrescription p = (MedicationPrescription) parsed.getEntries().get(0).getResource(); - assertEquals("Patient/347", p.getPatient().getReference().getValue()); - assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); - assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); - - Medication m = (Medication) parsed.getEntries().get(1).getResource(); - assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); - assertEquals("Medication/example", p.getMedication().getReference().getValue()); - assertSame(p.getMedication().getResource(), m); - - String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(parsed); - ourLog.info(reencoded); - - JsonConfig cfg = new JsonConfig(); - - JSON expected = JSONSerializer.toJSON(content.trim(), cfg); - JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); - - String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); - String act = actual.toString().replace("\\r\\n", "\\n"); - - exp = exp.replace(",\"ifNoneExist\":\"Patient?identifier=234234\"", ""); - - ourLog.info("Expected: {}", exp); - ourLog.info("Actual : {}", act); - - assertEquals(exp, act); - - } - - @Test - public void testParseAndEncodeBundleNewStyle() throws Exception { - String content = IOUtils.toString(JsonParserDstu2Test.class.getResourceAsStream("/bundle-example.json")); - - ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newJsonParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content); - assertEquals("http://example.com/base/Bundle/example/_history/1", parsed.getId().getValue()); - assertEquals("1", parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION)); - assertEquals("1", parsed.getId().getVersionIdPart()); - assertEquals(new InstantDt("2014-08-18T01:43:30Z"), parsed.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED)); - assertEquals("searchset", parsed.getType()); - assertEquals(3, parsed.getTotal().intValue()); - assertEquals("http://example.com/base", parsed.getBaseElement().getValueAsString()); - assertEquals("https://example.com/base/MedicationPrescription?patient=347&searchId=ff15fd40-ff71-4b48-b366-09c706bed9d0&page=2", parsed.getLink().get(0).getUrlElement().getValueAsString()); - assertEquals("https://example.com/base/MedicationPrescription?patient=347&_include=MedicationPrescription.medication", parsed.getLink().get(1).getUrlElement().getValueAsString()); - - assertEquals(2, parsed.getEntry().size()); - - MedicationPrescription p = (MedicationPrescription) parsed.getEntry().get(0).getResource(); - assertEquals("Patient/347", p.getPatient().getReference().getValue()); - assertEquals("2014-08-16T05:31:17Z", ResourceMetadataKeyEnum.UPDATED.get(p).getValueAsString()); - assertEquals("http://example.com/base/MedicationPrescription/3123/_history/1", p.getId().getValue()); - - Medication m = (Medication) parsed.getEntry().get(1).getResource(); - assertEquals("http://example.com/base/Medication/example", m.getId().getValue()); - assertEquals("Medication/example", p.getMedication().getReference().getValue()); - assertSame(p.getMedication().getResource(), m); - - String reencoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(parsed); - ourLog.info(reencoded); - - JsonConfig cfg = new JsonConfig(); - - JSON expected = JSONSerializer.toJSON(content.trim(), cfg); - JSON actual = JSONSerializer.toJSON(reencoded.trim(), cfg); - - String exp = expected.toString().replace("\\r\\n", "\\n"); // .replace("§", "§"); - String act = actual.toString().replace("\\r\\n", "\\n"); - - ourLog.info("Expected: {}", exp); - ourLog.info("Actual : {}", act); - - assertEquals(exp, act); - - } - - @Test - public void testParseAndEncodeBundleWithDeletedEntry() { - - Patient res = new Patient(); - res.setId(new IdDt("Patient", "111", "222")); - ResourceMetadataKeyEnum.DELETED_AT.put(res, new InstantDt("2011-01-01T12:12:22Z")); - - Bundle bundle = new Bundle(); - bundle.addResource(res, ourCtx, "http://foo/base"); - - String actual = ourCtx.newJsonParser().encodeBundleToString(bundle); - ourLog.info(actual); - - String expected = "{\"resourceType\":\"Bundle\",\"entry\":[{\"deleted\":{\"type\":\"Patient\",\"resourceId\":\"111\",\"versionId\":\"222\",\"instant\":\"2011-01-01T12:12:22Z\"}}]}"; - assertEquals(expected, actual); + obsv.setId("#123"); + report.addResult().setReference("#123"); + IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); + String message = parser.encodeResourceToString(report); + ourLog.info(message); + Assert.assertThat(message, containsString("contained")); } } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index a49f5f82175..3ca49a348fb 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -87,57 +87,33 @@ public class XmlParserDstu2Test { " \n" + ""; //@formatter:on - + Bundle b = ourCtx.newXmlParser().parseBundle(bundle); assertEquals(1, b.getEntries().size()); - + Binary bin = (Binary) b.getEntries().get(0).getResource(); - assertArrayEquals(new byte[] {1,2,3,4}, bin.getContent()); - + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); + } - @Test - public void testParseMetaUpdatedDate() { - //@formatter:off - String input = "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - ""; - //@formatter:on - ca.uhn.fhir.model.dstu2.resource.Bundle b = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); - - InstantDt updated = ResourceMetadataKeyEnum.UPDATED.get(b); - assertEquals("2015-06-22T15:48:57.554-04:00", updated.getValueAsString()); - - } - @Test public void testContainedResourceInExtensionUndeclared() { Patient p = new Patient(); p.addName().addFamily("PATIENT"); - + Organization o = new Organization(); o.setName("ORG"); p.addUndeclaredExtension(new ExtensionDt(false, "urn:foo", new ResourceReferenceDt(o))); - + String str = ourCtx.newXmlParser().encodeResourceToString(p); ourLog.info(str); - + p = ourCtx.newXmlParser().parseResource(Patient.class, str); assertEquals("PATIENT", p.getName().get(0).getFamily().get(0).getValue()); - + List exts = p.getUndeclaredExtensionsByUrl("urn:foo"); assertEquals(1, exts.size()); - ResourceReferenceDt rr = (ResourceReferenceDt)exts.get(0).getValue(); + ResourceReferenceDt rr = (ResourceReferenceDt) exts.get(0).getValue(); o = (Organization) rr.getResource(); assertEquals("ORG", o.getName()); } @@ -148,67 +124,28 @@ public class XmlParserDstu2Test { DurationDt duration = new DurationDt(); duration.setUnits("day").setValue(123L); enc.setLength(duration); - + String str = ourCtx.newXmlParser().encodeResourceToString(enc); ourLog.info(str); - + assertThat(str, not(containsString("meta"))); assertThat(str, containsString("")); } - - - @Test public void testEncodeAndParseBundleWithoutResourceIds() { Organization org = new Organization(); org.addIdentifier().setSystem("urn:system").setValue("someval"); - + Bundle bundle = Bundle.withSingleResource(org); String str = ourCtx.newXmlParser().encodeBundleToString(bundle); ourLog.info(str); - + Bundle parsed = ourCtx.newXmlParser().parseBundle(str); assertThat(parsed.getEntries().get(0).getResource().getId().getValue(), emptyOrNullString()); assertTrue(parsed.getEntries().get(0).getResource().getId().isEmpty()); } - public static void main(String[] args) { - IGenericClient c = ourCtx.newRestfulGenericClient("http://fhir-dev.healthintersections.com.au/open"); -// c.registerInterceptor(new LoggingInterceptor(true)); - c.read().resource("Patient").withId("324").execute(); - } - - @Test - public void testEncodeBundleWithContained() { - DiagnosticReport rpt = new DiagnosticReport(); - rpt.addResult().setResource(new Observation().setCode(new CodeableConceptDt().setText("Sharp1")).setId("#1")); - rpt.addResult().setResource(new Observation().setCode(new CodeableConceptDt().setText("Uuid1")).setId("urn:uuid:UUID1")); - - ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle(); - b.addEntry().setResource(rpt); - - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(encoded); - - assertThat(encoded, stringContainsInOrder( - "", "", "", ""))); @@ -269,7 +206,7 @@ public class XmlParserDstu2Test { // And re-encode once more, with the references cleared and a manually set local ID patient.getContained().getContainedResources().clear(); - patient.getManagingOrganization().setReference((String)null); + patient.getManagingOrganization().setReference((String) null); patient.getManagingOrganization().getResource().setId(("#333")); encoded = xmlParser.encodeResourceToString(patient); ourLog.info(encoded); @@ -278,7 +215,6 @@ public class XmlParserDstu2Test { } - @Test public void testEncodeAndParseExtensionOnResourceReference() { DataElement de = new DataElement(); @@ -291,20 +227,19 @@ public class XmlParserDstu2Test { String str = ourCtx.newXmlParser().encodeResourceToString(de); ourLog.info(str); - + de = ourCtx.newXmlParser().parseResource(DataElement.class, str); b = de.getElement().get(0).getBinding(); assertEquals("BINDING", b.getName()); - + List exts = b.getUndeclaredExtensionsByUrl("urn:foo"); assertEquals(1, exts.size()); - ResourceReferenceDt rr = (ResourceReferenceDt)exts.get(0).getValue(); + ResourceReferenceDt rr = (ResourceReferenceDt) exts.get(0).getValue(); o = (Organization) rr.getResource(); assertEquals("ORG", o.getName()); } - @Test public void testEncodeAndParseExtensions() throws Exception { @@ -348,12 +283,10 @@ public class XmlParserDstu2Test { String enc = ourCtx.newXmlParser().encodeResourceToString(patient); assertThat(enc, containsString("")); assertThat(enc, containsString("")); - assertThat( - enc, - containsString("")); + assertThat(enc, containsString("")); assertThat(enc, containsString("")); assertThat(enc, containsString("")); - + /* * Now parse this back */ @@ -396,7 +329,7 @@ public class XmlParserDstu2Test { public void testEncodeAndParseMetaProfileAndTags() { Patient p = new Patient(); p.addName().addFamily("FAMILY"); - + List profiles = new ArrayList(); profiles.add(new IdDt("http://foo/Profile1")); profiles.add(new IdDt("http://foo/Profile2")); @@ -406,10 +339,10 @@ public class XmlParserDstu2Test { tagList.addTag("scheme1", "term1", "label1"); tagList.addTag("scheme2", "term2", "label2"); ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); - + String enc = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p); ourLog.info(enc); - + //@formatter:off assertThat(enc, stringContainsInOrder("", "", @@ -433,38 +366,37 @@ public class XmlParserDstu2Test { "", "")); //@formatter:on - + Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, enc); List gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed); - - assertEquals(2,gotLabels.size()); + + assertEquals(2, gotLabels.size()); IdDt label = (IdDt) gotLabels.get(0); assertEquals("http://foo/Profile1", label.getValue()); label = (IdDt) gotLabels.get(1); assertEquals("http://foo/Profile2", label.getValue()); - + tagList = ResourceMetadataKeyEnum.TAG_LIST.get(parsed); assertEquals(2, tagList.size()); - + assertEquals(new Tag("scheme1", "term1", "label1"), tagList.get(0)); assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1)); } - @Test public void testEncodeAndParseMetaProfiles() { Patient p = new Patient(); p.addName().addFamily("FAMILY"); - + TagList tagList = new TagList(); tagList.addTag("scheme1", "term1", "label1"); tagList.addTag("scheme2", "term2", "label2"); ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); - + String enc = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p); ourLog.info(enc); - + //@formatter:off assertThat(enc, stringContainsInOrder("", "", @@ -486,32 +418,32 @@ public class XmlParserDstu2Test { "", "")); //@formatter:on - + Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, enc); List gotLabels = ResourceMetadataKeyEnum.PROFILES.get(parsed); assertNull(gotLabels); tagList = ResourceMetadataKeyEnum.TAG_LIST.get(parsed); assertEquals(2, tagList.size()); - + assertEquals(new Tag("scheme1", "term1", "label1"), tagList.get(0)); assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1)); } - + @Test public void testEncodeAndParseSecurityLabels() { Patient p = new Patient(); p.addName().addFamily("FAMILY"); - + List labels = new ArrayList(); labels.add(new CodingDt().setSystem("SYSTEM1").setCode("CODE1").setDisplay("DISPLAY1").setPrimary(true).setVersion("VERSION1")); labels.add(new CodingDt().setSystem("SYSTEM2").setCode("CODE2").setDisplay("DISPLAY2").setPrimary(false).setVersion("VERSION2")); - + ResourceMetadataKeyEnum.SECURITY_LABELS.put(p, labels); String enc = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p); ourLog.info(enc); - + //@formatter:off assertThat(enc, stringContainsInOrder("", "", @@ -535,11 +467,11 @@ public class XmlParserDstu2Test { "", "")); //@formatter:on - + Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, enc); List gotLabels = ResourceMetadataKeyEnum.SECURITY_LABELS.get(parsed); - - assertEquals(2,gotLabels.size()); + + assertEquals(2, gotLabels.size()); CodingDt label = (CodingDt) gotLabels.get(0); assertEquals("SYSTEM1", label.getSystem()); @@ -565,9 +497,9 @@ public class XmlParserDstu2Test { comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section0_Allergy0")); comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section1_Allergy0")); comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section2_Allergy0")); - + IParser parser = ourCtx.newJsonParser().setPrettyPrint(true); - + String string = parser.encodeResourceToString(comp); ourLog.info(string); @@ -590,9 +522,9 @@ public class XmlParserDstu2Test { comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section0_Allergy0")); comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section1_Allergy0")); comp.addSection().getContent().setResource(new AllergyIntolerance().setComment("Section2_Allergy0")); - + IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); - + String string = parser.encodeResourceToString(comp); ourLog.info(string); @@ -609,11 +541,11 @@ public class XmlParserDstu2Test { @Test public void testEncodeBinaryWithNoContentType() { Binary b = new Binary(); - b.setContent(new byte[] {1,2,3,4}); - + b.setContent(new byte[] { 1, 2, 3, 4 }); + String output = ourCtx.newXmlParser().encodeResourceToString(b); ourLog.info(output); - + assertEquals("", output); } @@ -622,25 +554,25 @@ public class XmlParserDstu2Test { Patient p = new Patient(); p.setId(IdDt.newRandomUuid()); p.addName().addFamily("PATIENT"); - + ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle(); b.addEntry().setResource(p); - + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); ourLog.info(encoded); assertThat(encoded, stringContainsInOrder("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "")); + + // @formatter:on + assertThat( + encoded, + stringContainsInOrder("", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "")); //@formatter:off } - - - - /** * See #113 */ @@ -792,27 +721,14 @@ public class XmlParserDstu2Test { ourLog.info(encoded); //@formatter:on - assertThat(encoded, stringContainsInOrder( - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "")); + assertThat( + encoded, + stringContainsInOrder("", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "")); //@formatter:off } + /** * See #113 */ @@ -844,29 +760,14 @@ public class XmlParserDstu2Test { ourLog.info(encoded); //@formatter:on - assertThat(encoded, stringContainsInOrder( - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "")); + assertThat( + encoded, + stringContainsInOrder("", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "")); //@formatter:off } - @Test public void testEncodeContainedWithNarrativeIsSuppresed() throws Exception { IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); @@ -894,6 +795,43 @@ public class XmlParserDstu2Test { } + + + + + /** + * #158 + */ + @Test + public void testEncodeEmptyTag() { + TagList tagList = new TagList(); + tagList.addTag(null, null, null); + tagList.addTag(null, null, "Label"); + + Patient p = new Patient(); + ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); + + String encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, not(containsString("tag"))); + } + + /** + * #158 + */ + @Test + public void testEncodeEmptyTag2() { + TagList tagList = new TagList(); + tagList.addTag("scheme", "code", null); + tagList.addTag(null, null, "Label"); + + Patient p = new Patient(); + ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList); + + String encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, containsString("tag")); + assertThat(encoded, containsString("scheme")); + assertThat(encoded, not(containsString("Label"))); + } @Test public void testEncodeExtensionWithResourceContent() { @@ -915,6 +853,7 @@ public class XmlParserDstu2Test { assertEquals("Organization/123", ref.getReference().getValue()); } + @Test public void testEncodeNonContained() { @@ -956,6 +895,7 @@ public class XmlParserDstu2Test { } + @Test public void testMoreExtensions() throws Exception { @@ -1056,7 +996,7 @@ public class XmlParserDstu2Test { assertTrue(d.toString(), d.identical()); } - + @Test public void testParseAndEncodeBundleNewStyle() throws Exception { String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml")); @@ -1301,48 +1241,29 @@ public class XmlParserDstu2Test { //@formatter:on DataElement de = ourCtx.newXmlParser().parseResource(DataElement.class, input); String output = ourCtx.newXmlParser().encodeResourceToString(de).replace(" xmlns=\"http://hl7.org/fhir\"", ""); - + ElementDefinitionDt elem = de.getElement().get(0); Binding b = elem.getBinding(); assertEquals("Gender", b.getName()); - + ResourceReferenceDt ref = (ResourceReferenceDt) b.getValueSet(); assertEquals("#2179414", ref.getReference().getValue()); - + assertEquals(2, ref.getUndeclaredExtensions().size()); ExtensionDt ext = ref.getUndeclaredExtensions().get(0); assertEquals("http://hl7.org/fhir/StructureDefinition/11179-permitted-value-valueset", ext.getUrl()); assertEquals(ResourceReferenceDt.class, ext.getValue().getClass()); - assertEquals("#2179414-permitted", ((ResourceReferenceDt)ext.getValue()).getReference().getValue()); - + assertEquals("#2179414-permitted", ((ResourceReferenceDt) ext.getValue()).getReference().getValue()); + ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(de)); - + assertThat(output, containsString("http://hl7.org/fhir/StructureDefinition/11179-permitted-value-valueset")); - + ourLog.info("Expected: {}", input); ourLog.info("Actual : {}", output); assertEquals(input, output); } - /** - * See #191 - */ - @Test - public void testParseBundleWithLinksOfUnknownRelation() throws Exception { - String input =IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml")); - ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); - - Link link = parsed.getLink().get(0); - assertEquals("just trying add link", link.getRelation()); - assertEquals("blarion", link.getUrl()); - - Entry entry = parsed.getEntry().get(0); - link = entry.getLink().get(0); - assertEquals("orionhealth.edit", link.getRelation()); - assertEquals("Observation", link.getUrl()); - } - - @Test public void testParseBundleNewWithPlaceholderIds() { //@formatter:off @@ -1358,10 +1279,10 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); assertEquals("urn:oid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); - + //@formatter:off input = "\n" + " \n" + @@ -1378,7 +1299,7 @@ public class XmlParserDstu2Test { parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); assertEquals("urn:oid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); - + } @Test @@ -1397,11 +1318,11 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); assertEquals("urn:oid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); } - + @Test public void testParseBundleNewWithPlaceholderIdsInBase2() { //@formatter:off @@ -1417,10 +1338,10 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); assertEquals("urn:uuid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); - + //@formatter:off input = "\n" + " \n" + @@ -1434,7 +1355,7 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); assertEquals("urn:uuid:0.1.2.3", parsed.getEntry().get(0).getResource().getId().getValue()); @@ -1468,10 +1389,9 @@ public class XmlParserDstu2Test { " \n" + ""; //@formatter:on - + Bundle b = ourCtx.newXmlParser().parseBundle(bundle); assertEquals(1, b.getEntries().size()); - } @@ -1490,10 +1410,10 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + Bundle parsed = ourCtx.newXmlParser().parseBundle(input); assertEquals("urn:oid:0.1.2.3", parsed.getEntries().get(0).getResource().getId().getValue()); - + //@formatter:off input = "\n" + " \n" + @@ -1507,18 +1427,35 @@ public class XmlParserDstu2Test { " \n" + "\n"; //@formatter:on - + parsed = ourCtx.newXmlParser().parseBundle(input); assertEquals("urn:oid:0.1.2.3", parsed.getEntries().get(0).getResource().getId().getValue()); } - @Test public void testParseBundleWithBinary() { // TODO: implement this test, make sure we handle ID and meta correctly in Binary } - + + /** + * See #191 + */ + @Test + public void testParseBundleWithLinksOfUnknownRelation() throws Exception { + String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml")); + ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); + + Link link = parsed.getLink().get(0); + assertEquals("just trying add link", link.getRelation()); + assertEquals("blarion", link.getUrl()); + + Entry entry = parsed.getEntry().get(0); + link = entry.getLink().get(0); + assertEquals("orionhealth.edit", link.getRelation()); + assertEquals("Observation", link.getUrl()); + } + /** * see #144 and #146 */ @@ -1534,10 +1471,10 @@ public class XmlParserDstu2Test { Patient p = new Patient(); p.addName().addFamily("patient family"); o.getSubject().setResource(p); - + String enc = parser.encodeResourceToString(o); ourLog.info(enc); - + //@formatter:off assertThat(enc, stringContainsInOrder( "", @@ -1548,15 +1485,14 @@ public class XmlParserDstu2Test { "" )); //@formatter:on - + o = parser.parseResource(Observation.class, enc); assertEquals("obs text", o.getCode().getText()); - + assertNotNull(o.getSubject().getResource()); p = (Patient) o.getSubject().getResource(); assertEquals("patient family", p.getNameFirstRep().getFamilyAsSingleString()); } - /** * Thanks to Alexander Kley! @@ -1584,7 +1520,7 @@ public class XmlParserDstu2Test { ca.uhn.fhir.model.dstu2.resource.DocumentManifest actual = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.DocumentManifest.class, encoded); assertEquals(1, actual.getContained().getContainedResources().size()); assertEquals(1, actual.getContent().size()); - assertNotNull(((ResourceReferenceDt)actual.getContent().get(0).getP()).getResource()); + assertNotNull(((ResourceReferenceDt) actual.getContent().get(0).getP()).getResource()); } @@ -1621,10 +1557,10 @@ public class XmlParserDstu2Test { " \n" + ""; //@formatter:on - + Bundle b = ourCtx.newXmlParser().parseBundle(bundle); assertEquals(1, b.getEntries().size()); - + Patient pt = (Patient) b.getEntries().get(0).getResource(); assertEquals("http://foo/fhirBase2/Patient/1/_history/2", pt.getId().getValue()); assertEquals("2012-01-02", pt.getBirthDateElement().getValueAsString()); @@ -1633,22 +1569,46 @@ public class XmlParserDstu2Test { assertEquals("POST", ResourceMetadataKeyEnum.ENTRY_TRANSACTION_METHOD.get(pt).getCode()); assertEquals("http://foo/Patient?identifier=value", ResourceMetadataKeyEnum.LINK_SEARCH.get(pt)); assertEquals("2001-02-22T11:22:33-05:00", ResourceMetadataKeyEnum.UPDATED.get(pt).getValueAsString()); - + Bundle toBundle = new Bundle(); toBundle.getLinkBase().setValue("http://foo/fhirBase1"); toBundle.getTotalResults().setValue(1); toBundle.getLinkSelf().setValue("http://localhost:52788/Binary?_pretty=true"); - + toBundle.addResource(pt, ourCtx, "http://foo/fhirBase1"); String reEncoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(toBundle); ourLog.info(reEncoded); - + Diff d = new Diff(new StringReader(bundle), new StringReader(reEncoded)); assertTrue(d.toString(), d.identical()); } + @Test + public void testParseMetaUpdatedDate() { + //@formatter:off + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + ca.uhn.fhir.model.dstu2.resource.Bundle b = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input); + + InstantDt updated = ResourceMetadataKeyEnum.UPDATED.get(b); + assertEquals("2015-06-22T15:48:57.554-04:00", updated.getValueAsString()); + + } + @Test public void testParseNarrative() throws Exception { //@formatter:off @@ -1661,11 +1621,11 @@ public class XmlParserDstu2Test { " \n" + ""; //@formatter:on - + Patient p = ourCtx.newXmlParser().parseResource(Patient.class, res); assertEquals(htmlNs, p.getText().getDiv().getValueAsString()); } - + /** * See #163 */ @@ -1688,10 +1648,10 @@ public class XmlParserDstu2Test { String bundleText = xmlParser.encodeResourceToString(bundle); ourLog.info(bundleText); - - ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = xmlParser.parseResource (ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText); - Patient reincarnatedPatient = reincarnatedBundle.getAllPopulatedChildElementsOfType(Patient.class).get(0); - + + ca.uhn.fhir.model.dstu2.resource.Bundle reincarnatedBundle = xmlParser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, bundleText); + Patient reincarnatedPatient = reincarnatedBundle.getAllPopulatedChildElementsOfType(Patient.class).get(0); + assertEquals("Patient", patient.getId().getResourceType()); assertEquals("Patient", reincarnatedPatient.getId().getResourceType()); } @@ -1703,6 +1663,10 @@ public class XmlParserDstu2Test { XMLUnit.setIgnoreWhitespace(true); } - - + public static void main(String[] args) { + IGenericClient c = ourCtx.newRestfulGenericClient("http://fhir-dev.healthintersections.com.au/open"); + // c.registerInterceptor(new LoggingInterceptor(true)); + c.read().resource("Patient").withId("324").execute(); + } + } diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java index 608f99cd9cb..fe7e5798614 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java @@ -59,6 +59,8 @@ import org.xml.sax.SAXException; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; +import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.base.composite.BaseNarrativeDt; import ca.uhn.fhir.narrative.INarrativeGenerator; import ca.uhn.fhir.parser.JsonParserHl7OrgTest.MyPatientWithOneDeclaredAddressExtension; @@ -598,6 +600,27 @@ public class XmlParserHl7OrgDstu2Test { } + /** + * #158 + */ + @Test + public void testEncodeEmptyTag() { + Patient p = new Patient(); + p.getMeta().addTag(); + + String encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, not(containsString("tag"))); + + // With tag + + p = new Patient(); + p.getMeta().addTag().setSystem("sys").setCode("code"); + + encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, (containsString("tag"))); + } + + /** * Thanks to Alexander Kley! */ diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f8e66d75cd9..fdcf23a585e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -37,6 +37,10 @@ into a BaseServerResponseException. This is useful so that servers using ResponseHighlighterInterceptor will highlight exceptions even if they aren't created with an OperationOutcome. + + XmlParser and JsonParser in DSTU2 mode should not encode empty + tags in resource. Thanks to Bill De Beaubien for reporting! +