"));
+ strings.addAll(Arrays.asList("
", "2 ", " ", " ", " "));
+ strings.addAll(Arrays.asList("
"));
+ assertThat(bundleString, StringContainsInOrder.stringContainsInOrder(strings));
+ assertThat(bundleString, not(containsString("at:by")));
+
+ }
+
+ @Test
+ public void testEncodeBundleCategory() {
+
+ Bundle b = new Bundle();
+ BundleEntry e = b.addEntry();
+ e.setResource(new Patient());
+ e.addCategory("scheme", "term", "label");
+
+ String val = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b);
+ ourLog.info(val);
+
+ assertThat(val, StringContains.containsString("
"));
+
+ b = ourCtx.newXmlParser().parseBundle(val);
+ assertEquals(1, b.getEntries().size());
+ assertEquals(1, b.getEntries().get(0).getCategories().size());
+ assertEquals("term", b.getEntries().get(0).getCategories().get(0).getTerm());
+ assertEquals("label", b.getEntries().get(0).getCategories().get(0).getLabel());
+ assertEquals("scheme", b.getEntries().get(0).getCategories().get(0).getScheme());
+ assertNull(b.getEntries().get(0).getResource());
+
+ }
+
+ @Test
+ public void testEncodeBundleResultCount() {
+
+ Bundle b = new Bundle();
+ b.getTotalResults().setValue(123);
+
+ String val = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b);
+ ourLog.info(val);
+
+ assertThat(val, StringContains.containsString("123 "));
+
+ }
+
+ @Test
+ public void testEncodeEscapedChars() {
+
+ Patient p = new Patient();
+ p.addName().addFamily("and <>&ü");
+
+ String enc = ourCtx.newXmlParser().encodeResourceToString(p);
+ ourLog.info(enc);
+
+ p = ourCtx.newXmlParser().parseResource(Patient.class, enc);
+ assertEquals("and <>&ü", p.getNameFirstRep().getFamilyFirstRep().getValue());
+
+ p = ourCtx.newXmlParser().parseResource(Patient.class, " ");
+ assertEquals("quot \"", p.getNameFirstRep().getFamilyFirstRep().getValue());
+
+ }
+
+ @Test
+ public void testEncodeEscapedExtendedChars() {
+ Patient p = ourCtx.newXmlParser().parseResource(Patient.class, " ");
+ assertEquals("uuml ü", p.getNameFirstRep().getFamilyFirstRep().getValue());
+ }
+
+ @Test
+ public void testEncodeContainedAndIncludedResources() {
+
+ DiagnosticReport rpt = new DiagnosticReport();
+ rpt.getName().setText("Report");
+
+ Specimen spm = new Specimen();
+ spm.addIdentifier().setLabel("Report1ContainedSpecimen1");
+ rpt.addSpecimen().setResource(spm);
+
+ IParser p = ourCtx.newXmlParser().setPrettyPrint(true);
+ String str = p.encodeResourceToString(rpt);
+
+ ourLog.info(str);
+
+ }
+
+ @Test
+ public void testEncodeContainedResources() {
+
+ DiagnosticReport rpt = new DiagnosticReport();
+ Specimen spm = new Specimen();
+ spm.addIdentifier("urn", "123");
+ rpt.getText().setDiv("AAA");
+ rpt.addSpecimen().setResource(spm);
+
+ IParser p = ourCtx.newXmlParser().setPrettyPrint(true);
+ String str = p.encodeResourceToString(rpt);
+
+ ourLog.info(str);
+ assertThat(str, StringContains.containsString("AAA
"));
+ assertThat(str, StringContains.containsString("reference value=\"#"));
+
+ int idx = str.indexOf("reference value=\"#") + "reference value=\"#".length();
+ int idx2 = str.indexOf('"', idx + 1);
+ String id = str.substring(idx, idx2);
+ assertThat(str, StringContains.containsString(""));
+ assertThat(str, IsNot.not(StringContains.containsString("")));
+
+ }
+
+ @Test
+ public void testEncodeDeclaredExtensionWithAddressContent() {
+ IParser parser = ourCtx.newXmlParser();
+
+ MyPatientWithOneDeclaredAddressExtension patient = new MyPatientWithOneDeclaredAddressExtension();
+ patient.addAddress().setUse(AddressUseEnum.HOME);
+ patient.setFoo(new AddressDt().addLine("line1"));
+
+ String val = parser.encodeResourceToString(patient);
+ ourLog.info(val);
+ assertThat(val, StringContains.containsString(" "));
+
+ MyPatientWithOneDeclaredAddressExtension actual = parser.parseResource(MyPatientWithOneDeclaredAddressExtension.class, val);
+ assertEquals(AddressUseEnum.HOME, patient.getAddressFirstRep().getUse().getValueAsEnum());
+ AddressDt ref = actual.getFoo();
+ assertEquals("line1", ref.getLineFirstRep().getValue());
+
+ }
+
+ @Test
+ public void testEncodeDeclaredExtensionWithResourceContent() {
+ IParser parser = ourCtx.newXmlParser();
+
+ MyPatientWithOneDeclaredExtension patient = new MyPatientWithOneDeclaredExtension();
+ patient.addAddress().setUse(AddressUseEnum.HOME);
+ patient.setFoo(new ResourceReferenceDt("Organization/123"));
+
+ String val = parser.encodeResourceToString(patient);
+ ourLog.info(val);
+ assertThat(val, StringContains.containsString(" "));
+
+ MyPatientWithOneDeclaredExtension actual = parser.parseResource(MyPatientWithOneDeclaredExtension.class, val);
+ assertEquals(AddressUseEnum.HOME, patient.getAddressFirstRep().getUse().getValueAsEnum());
+ ResourceReferenceDt ref = actual.getFoo();
+ assertEquals("Organization/123", ref.getReference().getValue());
+
+ }
+
+ @Test
+ public void testEncodeExtensionWithResourceContent() {
+ IParser parser = ourCtx.newXmlParser();
+
+ Patient patient = new Patient();
+ patient.addAddress().setUse(AddressUseEnum.HOME);
+ patient.addUndeclaredExtension(false, "urn:foo", new ResourceReferenceDt("Organization/123"));
+
+ String val = parser.encodeResourceToString(patient);
+ ourLog.info(val);
+ assertThat(val, StringContains.containsString(" "));
+
+ Patient actual = parser.parseResource(Patient.class, val);
+ assertEquals(AddressUseEnum.HOME, patient.getAddressFirstRep().getUse().getValueAsEnum());
+ List ext = actual.getUndeclaredExtensionsByUrl("urn:foo");
+ assertEquals(1, ext.size());
+ ResourceReferenceDt ref = (ResourceReferenceDt) ext.get(0).getValue();
+ assertEquals("Organization/123", ref.getReference().getValue());
+
+ }
+
+ @Test
+ public void testEncodeInvalidChildGoodException() {
+ Observation obs = new Observation();
+ obs.setValue(new DecimalDt(112.22));
+
+ IParser p = ourCtx.newJsonParser();
+
+ try {
+ p.encodeResourceToString(obs);
+ } catch (DataFormatException e) {
+ assertThat(e.getMessage(), StringContains.containsString("DecimalDt"));
+ }
+ }
+
+ @Test
+ public void testEncodeNarrativeBlockInBundle() {
+ Patient p = new Patient();
+ p.addIdentifier("foo", "bar");
+ p.getText().setStatus(NarrativeStatusEnum.GENERATED);
+ p.getText().setDiv("hello
");
+
+ Bundle b = new Bundle();
+ b.getTotalResults().setValue(123);
+ b.addEntry().setResource(p);
+
+ String out = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b);
+ ourLog.info(out);
+ assertThat(out, containsString("hello
"));
+
+ p.getText().setDiv("hello ");
+ out = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(b);
+ ourLog.info(out);
+ assertThat(out, containsString("hello "));
+
+ }
+
+ @Test
+ public void testEncodePrettyPrint() throws DataFormatException {
+
+ Patient patient = new Patient();
+ patient.getText().getDiv().setValueAsString("\n
hello \n LINE1\n LINE2 \n\n\n\n
");
+ patient.addName().addFamily("Family").addGiven("Given");
+
+ //@formatter:off
+ String encoded = ourCtx.newXmlParser().setPrettyPrint(false).encodeResourceToString(patient);
+ ourLog.info(encoded);
+ /*
+ * Note at least one space is placed where any whitespace was, as
+ * it is hard to tell what whitespace had no purpose
+ */
+ String expected = ""
+ + "
hello "
+ + "\n LINE1\n LINE2 "
+ + " ";
+ assertEquals(expected, encoded);
+
+ encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
+ ourLog.info(encoded);
+ expected = "\n"
+ + " \n"
+ + " \n"
+ + "
hello \n"
+ + " \n LINE1\n LINE2 \n"
+ + " \n"
+ + "
\n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + " ";
+ //@formatter:on
+
+ // Whitespace should be preserved and not reformatted in narrative blocks
+ assertEquals(expected, encoded);
+
+ }
+
+ @Test
+ public void testEncodeQuery() {
+ Query q = new Query();
+ ExtensionDt parameter = q.addParameter();
+ parameter.setUrl("http://foo").setValue(new StringDt("bar"));
+
+ String val = ourCtx.newXmlParser().encodeResourceToString(q);
+ ourLog.info(val);
+
+ assertEquals(" ", val);
+
+ }
+
+ @Test
+ public void testEncodeResourceRef() throws DataFormatException {
+
+ Patient patient = new Patient();
+ patient.setManagingOrganization(new ResourceReferenceDt());
+
+ IParser p = ourCtx.newXmlParser();
+ String str = p.encodeResourceToString(patient);
+ assertThat(str, IsNot.not(StringContains.containsString("managingOrganization")));
+
+ ResourceReferenceDt ref = new ResourceReferenceDt();
+ ref.setReference("Organization/123");
+ ref.setDisplay("DISPLAY!");
+ patient.setManagingOrganization(ref);
+ str = p.encodeResourceToString(patient);
+ assertThat(str, StringContains.containsString(" "));
+
+ Organization org = new Organization();
+ org.addIdentifier().setSystem("foo").setValue("bar");
+ patient.setManagingOrganization(new ResourceReferenceDt(org));
+ str = p.encodeResourceToString(patient);
+ assertThat(str, StringContains.containsString(" "));
+
+ MyPatientWithOneDeclaredAddressExtension actual = parser.parseResource(MyPatientWithOneDeclaredAddressExtension.class, val);
+ assertEquals(AddressUseEnum.HOME, patient.getAddressFirstRep().getUse().getValueAsEnum());
+ AddressDt ref = actual.getFoo();
+ assertEquals("line1", ref.getLineFirstRep().getValue());
+
+ }
+
+ @Test
+ public void testExtensionOnComposite() throws Exception {
+
+ Patient patient = new Patient();
+
+ HumanNameDt name = patient.addName();
+ name.addFamily().setValue("Shmoe");
+ HumanNameDt given = name.addGiven("Joe");
+ ExtensionDt ext2 = new ExtensionDt(false, "http://examples.com#givenext", new StringDt("Hello"));
+ given.addUndeclaredExtension(ext2);
+ String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
+ ourLog.info(output);
+
+ String enc = ourCtx.newXmlParser().encodeResourceToString(patient);
+ assertThat(enc, containsString(" "));
+
+ Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, new StringReader(enc));
+ assertEquals(1, parsed.getNameFirstRep().getUndeclaredExtensionsByUrl("http://examples.com#givenext").size());
+ ExtensionDt ext = parsed.getNameFirstRep().getUndeclaredExtensionsByUrl("http://examples.com#givenext").get(0);
+ assertEquals("Hello", ext.getValueAsPrimitive().getValue());
+
+ }
+
+ @Test
+ public void testExtensionOnPrimitive() throws Exception {
+
+ Patient patient = new Patient();
+
+ HumanNameDt name = patient.addName();
+ StringDt family = name.addFamily();
+ family.setValue("Shmoe");
+
+ ExtensionDt ext2 = new ExtensionDt(false, "http://examples.com#givenext", new StringDt("Hello"));
+ family.addUndeclaredExtension(ext2);
+ String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
+ ourLog.info(output);
+
+ String enc = ourCtx.newXmlParser().encodeResourceToString(patient);
+ assertThat(enc, containsString(" "));
+
+ Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, new StringReader(enc));
+ assertEquals(1, parsed.getNameFirstRep().getFamilyFirstRep().getUndeclaredExtensionsByUrl("http://examples.com#givenext").size());
+ ExtensionDt ext = parsed.getNameFirstRep().getFamilyFirstRep().getUndeclaredExtensionsByUrl("http://examples.com#givenext").get(0);
+ assertEquals("Hello", ext.getValueAsPrimitive().getValue());
+
+ }
+
+ @Test
+ public void testExtensions() throws DataFormatException {
+
+ MyPatient patient = new MyPatient();
+ patient.setPetName(new StringDt("Fido"));
+ patient.getImportantDates().add(new DateTimeDt("2010-01-02"));
+ patient.getImportantDates().add(new DateTimeDt("2014-01-26T11:11:11"));
+
+ patient.addName().addFamily("Smith");
+
+ IParser p = ourCtx.newXmlParser();
+ String str = p.encodeResourceToString(patient);
+
+ ourLog.info(str);
+
+ assertThat(str, StringContains.containsString(""));
+ assertThat(str, StringContains.containsString(" "));
+ assertThat(str, StringContains.containsString(" "));
+ assertThat(str, StringContains.containsString(" "));
+ assertThat(str, StringContains.containsString(" "));
+
+ }
+
+ @Test
+ public void testLoadAndAncodeMessage() throws SAXException, IOException {
+
+ //@formatter:off
+ String msg = ""
+ + "John Cardinal: 444333333
"
+ + " "
+ + " "
+ + " "
+ + " "
+ + "
"
+ + " "
+ + " ";
+ //@formatter:on
+
+ Patient patient = ourCtx.newXmlParser().parseResource(Patient.class, msg);
+
+ assertEquals(NarrativeStatusEnum.GENERATED, patient.getText().getStatus().getValueAsEnum());
+ assertEquals("John Cardinal: 444333333
", patient.getText().getDiv().getValueAsString());
+ assertEquals("PRP1660", patient.getIdentifier().get(0).getValue().getValueAsString());
+
+ String encoded = ourCtx.newXmlParser().encodeResourceToString(patient);
+
+ Diff d = new Diff(new StringReader(msg), new StringReader(encoded));
+ assertTrue(d.toString(), d.identical());
+
+ }
+
+ @Test
+ public void testLoadAndEncodeDeclaredExtensions() throws ConfigurationException, DataFormatException, SAXException, IOException {
+ IParser p = new FhirContext(ResourceWithExtensionsA.class).newXmlParser();
+
+ //@formatter:off
+ String msg = "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " ";
+ //@formatter:on
+
+ ResourceWithExtensionsA resource = (ResourceWithExtensionsA) p.parseResource(msg);
+ assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getLabel().getValue());
+ assertEquals("Foo1Value", resource.getFoo1().get(0).getValue());
+ assertEquals("Foo1Value2", resource.getFoo1().get(1).getValue());
+ assertEquals("Foo2Value1", resource.getFoo2().getValue());
+ assertEquals("2013-01-01", resource.getBar1().get(0).getBar11().get(0).getValueAsString());
+ assertEquals("2013-01-02", resource.getBar1().get(0).getBar12().get(0).getBar121().get(0).getValueAsString());
+ assertEquals("2013-01-12", resource.getBar1().get(0).getBar12().get(0).getBar121().get(1).getValueAsString());
+ assertEquals("2013-01-03", resource.getBar1().get(0).getBar12().get(0).getBar122().get(0).getValueAsString());
+
+ String encoded = p.encodeResourceToString(resource);
+ ourLog.info(encoded);
+
+ Diff d = new Diff(new StringReader(msg), new StringReader(encoded));
+ assertTrue(d.toString(), d.identical());
+ }
+
+ @Test
+ public void testLoadAndEncodeUndeclaredExtensions() throws ConfigurationException, DataFormatException, SAXException, IOException {
+ IParser p = ourCtx.newXmlParser();
+
+ //@formatter:off
+ String msg = "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " ";
+ //@formatter:on
+
+ Patient resource = (Patient) p.parseResource(msg);
+ assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getLabel().getValue());
+ assertEquals("Foo1Value", resource.getUndeclaredExtensions().get(0).getValueAsPrimitive().getValueAsString());
+ assertEquals("Foo1Value2", resource.getUndeclaredExtensions().get(1).getValueAsPrimitive().getValueAsString());
+ assertEquals("Foo2Value1", resource.getUndeclaredModifierExtensions().get(0).getValueAsPrimitive().getValueAsString());
+
+ assertEquals("2013-01-01", resource.getUndeclaredExtensions().get(2).getUndeclaredExtensions().get(0).getValueAsPrimitive().getValueAsString());
+ assertEquals("2013-01-02", resource.getUndeclaredExtensions().get(2).getUndeclaredExtensions().get(1).getUndeclaredExtensions().get(0).getValueAsPrimitive().getValueAsString());
+
+ String encoded = p.encodeResourceToString(resource);
+ ourLog.info(encoded);
+
+ Diff d = new Diff(new StringReader(msg), new StringReader(encoded));
+ assertTrue(d.toString(), d.identical());
+ }
+
+ @Test
+ public void testLoadObservation() throws ConfigurationException, DataFormatException, IOException {
+
+ IParser p = ourCtx.newXmlParser();
+
+ String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/observation-example-eeg.xml"), Charset.forName("UTF-8"));
+ IResource resource = p.parseResource(string);
+
+ String result = p.encodeResourceToString(resource);
+ ourLog.info(result);
+ }
+
+ @Test
+ public void testParseFeedWithListResource() throws ConfigurationException, DataFormatException, IOException {
+
+ // Use new context here to ensure List isn't already loaded
+ IParser p = new FhirContext().newXmlParser();
+
+ String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/feed-with-list.xml"), Charset.forName("UTF-8"));
+ Bundle bundle = p.parseBundle(string);
+
+ ListResource res = (ListResource) bundle.toListOfResources().get(2);
+ assertEquals("cid:patient@bundle", res.getSubject().getReference().getValue());
+
+ }
+
+ @Test
+ public void testLoadPatient() throws ConfigurationException, DataFormatException, IOException {
+
+ IParser p = ourCtx.newXmlParser();
+
+ String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-dicom.xml"), Charset.forName("UTF-8"));
+ IResource resource = p.parseResource(string);
+
+ String result = p.encodeResourceToString(resource);
+ ourLog.info(result);
+
+ // Nothing
+
+ string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/patient-example-us-extensions.xml"), Charset.forName("UTF-8"));
+ resource = p.parseResource(string);
+
+ result = p.encodeResourceToString(resource);
+ ourLog.info(result);
+
+ }
+
+ @Test
+ public void testLoadQuestionnaire() throws ConfigurationException, DataFormatException, IOException {
+
+ IParser p = ourCtx.newXmlParser();
+
+ String string = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/questionnaire-example.xml"), Charset.forName("UTF-8"));
+ IResource resource = p.parseResource(string);
+
+ String result = p.encodeResourceToString(resource);
+ ourLog.info(result);
+ }
+
+ @Test
+ public void testMessageWithMultipleTypes() throws SAXException, IOException {
+
+ //@formatter:off
+ String msg = ""
+ + " "
+ + " ";
+ //@formatter:on
+
+ Patient patient1 = ourCtx.newXmlParser().parseResource(Patient.class, msg);
+ String encoded1 = ourCtx.newXmlParser().encodeResourceToString(patient1);
+
+ ca.uhn.fhir.testmodel.Patient patient2 = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.testmodel.Patient.class, msg);
+ String encoded2 = ourCtx.newXmlParser().encodeResourceToString(patient2);
+
+ Diff d = new Diff(new StringReader(encoded1), new StringReader(encoded2));
+ assertTrue(d.toString(), d.identical());
+
+ }
+
+ @Test
+ public void testMoreExtensions() throws Exception {
+
+ Patient patient = new Patient();
+ patient.addIdentifier(IdentifierUseEnum.OFFICIAL, "urn:example", "7000135", null);
+
+ ExtensionDt ext = new ExtensionDt();
+ ext.setModifier(false);
+ ext.setUrl("http://example.com/extensions#someext");
+ ext.setValue(new DateTimeDt("2011-01-02T11:13:15"));
+
+ // Add the extension to the resource
+ patient.addUndeclaredExtension(ext);
+ // END SNIPPET: resourceExtension
+
+ // START SNIPPET: resourceStringExtension
+ HumanNameDt name = patient.addName();
+ name.addFamily().setValue("Shmoe");
+ StringDt given = name.addGiven();
+ given.setValue("Joe");
+ ExtensionDt ext2 = new ExtensionDt(false, "http://examples.com#givenext", new StringDt("given"));
+ given.addUndeclaredExtension(ext2);
+ // END SNIPPET: resourceStringExtension
+
+ // START SNIPPET: subExtension
+ ExtensionDt parent = new ExtensionDt(false, "http://example.com#parent");
+ patient.addUndeclaredExtension(parent);
+
+ ExtensionDt child1 = new ExtensionDt(false, "http://example.com#child", new StringDt("value1"));
+ parent.addUndeclaredExtension(child1);
+
+ ExtensionDt child2 = new ExtensionDt(false, "http://example.com#child", new StringDt("value1"));
+ parent.addUndeclaredExtension(child2);
+ // END SNIPPET: subExtension
+
+ String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
+ ourLog.info(output);
+
+ String enc = ourCtx.newXmlParser().encodeResourceToString(patient);
+ assertThat(enc, containsString(" "));
+ assertThat(
+ enc,
+ containsString(" "));
+ assertThat(enc, containsString(" "));
+ }
+
+ @Test
+ public void testNarrativeGeneration() throws DataFormatException {
+
+ Patient patient = new Patient();
+
+ patient.addName().addFamily("Smith");
+
+ INarrativeGenerator gen = mock(INarrativeGenerator.class);
+ XhtmlDt xhtmlDt = new XhtmlDt("help
");
+ NarrativeDt nar = new NarrativeDt(xhtmlDt, NarrativeStatusEnum.GENERATED);
+ when(gen.generateNarrative(eq("http://hl7.org/fhir/profiles/Patient"), eq(patient))).thenReturn(nar);
+
+ FhirContext context = ourCtx;
+ context.setNarrativeGenerator(gen);
+ IParser p = context.newXmlParser();
+ String str = p.encodeResourceToString(patient);
+
+ ourLog.info(str);
+
+ assertThat(str, StringContains.containsString(""));
+ assertThat(str, StringContains.containsString(""));
+ }
+
+ @Test
+ public void testDuplicateContainedResources() {
+
+ Observation resA = new Observation();
+ resA.getName().setText("A");
+
+ Observation resB = new Observation();
+ resB.getName().setText("B");
+ resB.addRelated().setTarget(new ResourceReferenceDt(resA));
+ resB.addRelated().setTarget(new ResourceReferenceDt(resA));
+
+ String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(resB);
+ ourLog.info(encoded);
+
+ assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", " ")));
+ assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "", ""))));
+
+ }
+
+ @Test
+ public void testNestedContainedResources() {
+
+ Observation A = new Observation();
+ A.getName().setText("A");
+
+ Observation B = new Observation();
+ B.getName().setText("B");
+ A.addRelated().setTarget(new ResourceReferenceDt(B));
+
+ Observation C = new Observation();
+ C.getName().setText("C");
+ B.addRelated().setTarget(new ResourceReferenceDt(C));
+
+ String str = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(A);
+ ourLog.info(str);
+
+ assertThat(str, stringContainsInOrder(Arrays.asList("", "", "")));
+ assertThat(str, stringContainsInOrder(Arrays.asList("", " ")));
+
+ // Only one (outer) contained block
+ int idx0 = str.indexOf("");
+ int idx1 = str.indexOf("", idx0 + 1);
+ assertNotEquals(-1, idx0);
+ assertEquals(-1, idx1);
+
+ Observation obs = ourCtx.newXmlParser().parseResource(Observation.class, str);
+ assertEquals("A", obs.getName().getText().getValue());
+
+ Observation obsB = (Observation) obs.getRelatedFirstRep().getTarget().getResource();
+ assertEquals("B", obsB.getName().getText().getValue());
+
+ Observation obsC = (Observation) obsB.getRelatedFirstRep().getTarget().getResource();
+ assertEquals("C", obsC.getName().getText().getValue());
+
+ }
+
+ @Test
+ public void testParseBinaryResource() {
+
+ Binary val = ourCtx.newXmlParser().parseResource(Binary.class, "AQIDBA== ");
+ assertEquals("foo", val.getContentType());
+ assertArrayEquals(new byte[] { 1, 2, 3, 4 }, val.getContent());
+
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testParseBundle() {
+
+ //@formatter:off
+ String summaryText =
+ "\n" +
+ "
Value set \"LOINC Codes for Cholesterol\": This is an example value set that includes \n" +
+ " all the LOINC codes for serum cholesterol from v2.36. \n" +
+ " Developed by: FHIR project team (example)
";
+
+ String msg = "\n" +
+ " FHIR Core Valuesets \n" +
+ " http://hl7.org/fhir/profile/valuesets \n" +
+ " \n" +
+ " \n" +
+ " 2014-02-10T04:11:24.435-00:00 \n" +
+ " \n" +
+ " Valueset "256a5231-a2bb-49bd-9fea-f349d428b70d" to support automated processing \n" +
+ " http://hl7.org/fhir/valueset/256a5231-a2bb-49bd-9fea-f349d428b70d \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " 2014-02-10T04:10:46.987-00:00 \n" +
+ " \n" +
+ " HL7, Inc (FHIR Project) \n" +
+ " http://hl7.org/fhir \n" +
+ " \n" +
+ " 2014-02-10T04:10:46.987-00:00 \n" +
+ " \n "+
+ " \n" +
+ " \n" +
+ " \n" +
+ " " +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "
\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " "+
+ summaryText +
+ " \n" +
+ " " +
+ " ";
+ //@formatter:on
+
+ IParser p = new FhirContext(ValueSet.class).newXmlParser();
+ Bundle bundle = p.parseBundle(msg);
+
+ assertEquals(1, bundle.getCategories().size());
+ assertEquals("http://hl7.org/fhir/tag", bundle.getCategories().get(0).getScheme());
+
+ assertEquals("FHIR Core Valuesets", bundle.getTitle().getValue());
+ assertEquals("http://hl7.org/implement/standards/fhir/valuesets.xml", bundle.getLinkSelf().getValue());
+ assertEquals("2014-02-10T04:11:24.435-00:00", bundle.getUpdated().getValueAsString());
+ assertEquals(1, bundle.getEntries().size());
+
+ BundleEntry entry = bundle.getEntries().get(0);
+ assertEquals("HL7, Inc (FHIR Project)", entry.getAuthorName().getValue());
+ assertEquals("http://hl7.org/fhir/valueset/256a5231-a2bb-49bd-9fea-f349d428b70d", entry.getId().getValue());
+ assertEquals("http://hl7.org/foo", entry.getLinkAlternate().getValue());
+ assertEquals("http://hl7.org/foo/search", entry.getLinkSearch().getValue());
+ assertEquals(1, entry.getCategories().size());
+ assertEquals("term", entry.getCategories().get(0).getTerm());
+ assertEquals("label", entry.getCategories().get(0).getLabel());
+ assertEquals("http://foo", entry.getCategories().get(0).getScheme());
+
+ ValueSet resource = (ValueSet) entry.getResource();
+ assertEquals("LOINC Codes for Cholesterol", resource.getName().getValue());
+
+ String exp = summaryText.trim();
+ exp = exp.replace("\"LOINC", ""LOINC");
+ exp = exp.replace("terol\"", "terol"");
+ assertEquals(exp, entry.getSummary().getValueAsString().trim());
+
+ TagList tl = (TagList) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.TAG_LIST);
+ assertEquals(1, tl.size());
+ assertEquals("term", tl.get(0).getTerm());
+ assertEquals("label", tl.get(0).getLabel());
+ assertEquals("http://foo", tl.get(0).getScheme());
+
+ assertEquals("256a5231-a2bb-49bd-9fea-f349d428b70d", resource.getId().getIdPart());
+
+ msg = msg.replace(" ",
+ " ");
+ entry = p.parseBundle(msg).getEntries().get(0);
+ resource = (ValueSet) entry.getResource();
+ assertEquals("256a5231-a2bb-49bd-9fea-f349d428b70d", resource.getId().getIdPart());
+ assertEquals("12345", resource.getId().getVersionIdPart());
+ assertEquals("12345", ((IdDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION_ID)).getVersionIdPart());
+
+ assertThat(entry.getSummary().getValueAsString(), containsString("LOINC Codes for Cholesterol"));
+
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testParseBundleDeletedEntry() {
+
+ //@formatter:off
+ String msg = "" +
+ "FHIR Core Valuesets " +
+ "http://hl7.org/fhir/profile/valuesets " +
+ " " +
+ "2014-02-10T04:11:24.435+00:00 " +
+ "" +
+ "" +
+ "John Doe " +
+ "jdoe@example.org " +
+ " " +
+ "Removed comment spam " +
+ " " +
+ " " +
+ " ";
+ //@formatter:on
+
+ IParser p = ourCtx.newXmlParser();
+ Bundle bundle = p.parseBundle(msg);
+
+ BundleEntry entry = bundle.getEntries().get(0);
+ assertEquals("http://foo/Patient/1", entry.getId().getValue());
+ assertEquals("2013-02-10T04:11:24.435+00:00", entry.getDeletedAt().getValueAsString());
+ assertEquals("http://foo/Patient/1/_history/2", entry.getLinkSelf().getValue());
+ assertEquals("1", entry.getResource().getId().getIdPart());
+ assertEquals("2", entry.getResource().getId().getVersionIdPart());
+ assertEquals("2", ((IdDt) entry.getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.VERSION_ID)).getVersionIdPart());
+ assertEquals("John Doe", entry.getDeletedByName().getValue());
+ assertEquals("jdoe@example.org", entry.getDeletedByEmail().getValue());
+ assertEquals("Removed comment spam", entry.getDeletedComment().getValue());
+ assertEquals(new InstantDt("2013-02-10T04:11:24.435+00:00"), entry.getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.DELETED_AT));
+
+ ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle));
+
+ String encoded = ourCtx.newXmlParser().encodeBundleToString(bundle);
+ assertEquals(msg, encoded);
+
+ }
+
+ @Test
+ public void testParseBundleLarge() throws IOException {
+
+ String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/atom-document-large.xml"));
+ IParser p = ourCtx.newXmlParser();
+ Bundle bundle = p.parseBundle(msg);
+
+ assertEquals("http://spark.furore.com/fhir/_snapshot?id=327d6bb9-83b0-4929-aa91-6dd9c41e587b&start=0&_count=20", bundle.getLinkSelf().getValue());
+ assertEquals("Patient resource with id 3216379", bundle.getEntries().get(0).getTitle().getValue());
+ assertEquals("http://spark.furore.com/fhir/Patient/3216379", bundle.getEntries().get(0).getId().getValue());
+ assertEquals("3216379", bundle.getEntries().get(0).getResource().getId().getIdPart());
+
+ }
+
+ @Test
+ public void testParseBundleWithMixedReturnTypes() {
+ InputStreamReader str = new InputStreamReader(getClass().getResourceAsStream("/mixed-return-bundle.xml"));
+ Bundle b = ourCtx.newXmlParser().parseBundle(Patient.class, str);
+ assertEquals(Patient.class, b.getEntries().get(0).getResource().getClass());
+ assertEquals(Patient.class, b.getEntries().get(1).getResource().getClass());
+ assertEquals(Organization.class, b.getEntries().get(2).getResource().getClass());
+ }
+
+ @Test
+ public void testParseContainedResources() throws IOException {
+
+ String msg = IOUtils.toString(XmlParser.class.getResourceAsStream("/contained-diagnosticreport.xml"));
+ IParser p = ourCtx.newXmlParser();
+ DiagnosticReport bundle = p.parseResource(DiagnosticReport.class, msg);
+
+ ResourceReferenceDt result0 = bundle.getResult().get(0);
+ Observation obs = (Observation) result0.getResource();
+
+ assertNotNull(obs);
+ assertEquals("718-7", obs.getName().getCoding().get(0).getCode().getValue());
+
+ }
+
+ @Test
+ public void testParseEncodeNarrative() {
+
+ String input = "Identifier 7000135 Address 10 Duxon Street VICTORIA BC Can Date of birth 01 June 1980
";
+ IResource res = ourCtx.newXmlParser().parseResource(input);
+
+ String output = ourCtx.newXmlParser().encodeResourceToString(res);
+
+ // Should occur exactly twice (once for the resource, once for the DIV
+ assertThat(output, (StringContainsInOrder.stringContainsInOrder(Arrays.asList("Patient xmlns", "div xmlns"))));
+ assertThat(output, not(StringContainsInOrder.stringContainsInOrder(Arrays.asList("b xmlns"))));
+
+ output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
+
+ // Should occur exactly twice (once for the resource, once for the DIV
+ assertThat(output, (StringContainsInOrder.stringContainsInOrder(Arrays.asList("Patient xmlns", "div xmlns"))));
+ assertThat(output, not(StringContainsInOrder.stringContainsInOrder(Arrays.asList("b xmlns"))));
+
+ }
+
+ /**
+ * This sample has extra elements in that are not actually a part of the spec any more..
+ */
+ @Test
+ public void testParseFuroreMetadataWithExtraElements() throws IOException {
+ String msg = IOUtils.toString(XmlParserTest.class.getResourceAsStream("/furore-conformance.xml"));
+
+ IParser p = new FhirContext(ValueSet.class).newXmlParser();
+ Conformance conf = p.parseResource(Conformance.class, msg);
+ RestResource res = conf.getRestFirstRep().getResourceFirstRep();
+ assertEquals("_id", res.getSearchParam().get(1).getName().getValue());
+ }
+
+ @Test
+ public void testParseLanguage() {
+ String input = "Identifier URNo Address 99 Houston Road BENTLEIGH Victoria Date of birth 01 January 1997
";
+ Patient pt = ourCtx.newXmlParser().parseResource(Patient.class, input);
+
+ assertEquals("zh-CN", pt.getLanguage().getValue());
+ }
+
+ @Test
+ public void testParseQuery() {
+ String msg = "\n" + " \n" + " \n" + " [Put rendering here]
\n"
+ + " \n" + "\n" + " \n"
+ + " \n" + " \n" + " \n"
+ + " \n" + " ";
+ Query query = ourCtx.newXmlParser().parseResource(Query.class, msg);
+
+ assertEquals("urn:uuid:42b253f5-fa17-40d0-8da5-44aeb4230376", query.getIdentifier().getValueAsString());
+ assertEquals("http://hl7.org/fhir/query#_query", query.getParameterFirstRep().getUrlAsString());
+ assertEquals("example", query.getParameterFirstRep().getValueAsPrimitive().getValueAsString());
+
+ }
+
+ @Test
+ public void testParseWithXmlHeader() throws ConfigurationException, DataFormatException {
+ IParser p = ourCtx.newXmlParser();
+
+ //@formatter:off
+ String msg = "" +
+ "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " ";
+ //@formatter:on
+
+ Patient resource = (Patient) p.parseResource(msg);
+ assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getLabel().getValue());
+ }
+
+ @Test
+ public void testSimpleResourceEncode() throws IOException, SAXException {
+
+ String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"), Charset.forName("UTF-8"));
+ Patient obs = ourCtx.newJsonParser().parseResource(Patient.class, xmlString);
+
+ List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getUndeclaredExtensions();
+ ExtensionDt undeclaredExtension = undeclaredExtensions.get(0);
+ assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl().getValue());
+
+ ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
+
+ IParser jsonParser = ourCtx.newXmlParser();
+ String encoded = jsonParser.encodeResourceToString(obs);
+ ourLog.info(encoded);
+
+ String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
+
+ String expected = (jsonString);
+ String actual = (encoded.trim());
+
+ Diff d = new Diff(new StringReader(expected), new StringReader(actual));
+ assertTrue(d.toString(), d.identical());
+
+ }
+
+ @Test
+ public void testSimpleResourceEncodeWithCustomType() throws IOException, SAXException {
+
+ FhirContext fhirCtx = new FhirContext(MyObservationWithExtensions.class);
+ String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"), Charset.forName("UTF-8"));
+ MyObservationWithExtensions obs = fhirCtx.newJsonParser().parseResource(MyObservationWithExtensions.class, xmlString);
+
+ assertEquals(0, obs.getAllUndeclaredExtensions().size());
+ assertEquals("aaaa", obs.getExtAtt().getContentType().getValue());
+ assertEquals("str1", obs.getMoreExt().getStr1().getValue());
+ assertEquals("2011-01-02", obs.getModExt().getValueAsString());
+
+ List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getUndeclaredExtensions();
+ ExtensionDt undeclaredExtension = undeclaredExtensions.get(0);
+ assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl().getValue());
+
+ fhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out));
+
+ IParser jsonParser = fhirCtx.newXmlParser();
+ String encoded = jsonParser.encodeResourceToString(obs);
+ ourLog.info(encoded);
+
+ String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
+
+ String expected = (jsonString);
+ String actual = (encoded.trim());
+
+ Diff d = new Diff(new StringReader(expected), new StringReader(actual));
+ assertTrue(d.toString(), d.identical());
+
+ }
+
+ @Test
+ public void testTagList() {
+
+ //@formatter:off
+ String tagListStr = " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " ";
+ //@formatter:on
+
+ TagList tagList = ourCtx.newXmlParser().parseTagList(tagListStr);
+ assertEquals(3, tagList.size());
+ assertEquals("term0", tagList.get(0).getTerm());
+ assertEquals("label0", tagList.get(0).getLabel());
+ assertEquals("scheme0", tagList.get(0).getScheme());
+ assertEquals("term1", tagList.get(1).getTerm());
+ assertEquals("label1", tagList.get(1).getLabel());
+ assertEquals(null, tagList.get(1).getScheme());
+ assertEquals("term2", tagList.get(2).getTerm());
+ assertEquals("label2", tagList.get(2).getLabel());
+ assertEquals(null, tagList.get(2).getScheme());
+
+ /*
+ * Encode
+ */
+
+ //@formatter:off
+ String expected = "" +
+ "" +
+ "" +
+ "" +
+ " ";
+ //@formatter:on
+
+ String encoded = ourCtx.newXmlParser().encodeTagListToString(tagList);
+ assertEquals(expected, encoded);
+
+ }
+
+ @Test
+ public void testTotalResultsUsingOldNamespace() {
+
+ //@formatter:off
+ String bundle = "\n" +
+ " Search results for Patient \n" +
+ " urn:uuid:374f2876-0da7-4441-87da-526e2fc624f8 \n" +
+ " 15 \n" +
+ " 2014-05-04T13:19:47.027-04:00 \n" +
+ " \n" +
+ " AEGIS Wildfhir Server \n" +
+ " " +
+ " ";
+ //@formatter:off
+
+ Bundle bundleR = ourCtx.newXmlParser().parseBundle(bundle);
+ assertEquals(15, bundleR.getTotalResults().getValue().intValue());
+ }
+
+ @BeforeClass
+ public static void beforeClass() {
+ XMLUnit.setIgnoreAttributeOrder(true);
+ XMLUnit.setIgnoreComments(true);
+ XMLUnit.setIgnoreWhitespace(true);
+ ourCtx = new FhirContext();
+ }
+
+}