From 72640dc72076bf58cde98d620ae3d1a30c1eda45 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 14 Apr 2016 07:53:37 -0400 Subject: [PATCH] Correct encoding order for DSTU2 resources --- ...BaseRuntimeElementCompositeDefinition.java | 28 + .../uhn/fhir/parser/XmlParserDstu2Test.java | 67 + .../ResourceValidatorDstu2Test.java | 312 +- .../uhn/fhir/parser/XmlParserDstu3Test.java | 301 +- .../ResourceValidatorDstu3Test.java | 56 +- .../fhir/parser/XmlParserHl7OrgDstu2Test.java | 2908 +++++++++-------- 6 files changed, 2011 insertions(+), 1661 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java index 52281e0387a..64c0c484ba4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java @@ -101,6 +101,34 @@ public abstract class BaseRuntimeElementCompositeDefinition ext List children = new ArrayList(); children.addAll(myChildren); + + /* + * Because of the way the type hierarchy works for DSTU2 resources, + * things end up in the wrong order + */ + int extIndex = findIndex(children, "extension"); + int containedIndex = findIndex(children, "contained"); + if (containedIndex != -1 && extIndex != -1 && extIndex < containedIndex) { + BaseRuntimeChildDefinition extension = children.remove(extIndex); + if (containedIndex > children.size()) { + children.add(extension); + } else { + children.add(containedIndex, extension); + } + } + int modIndex = findIndex(children, "modifierExtension"); + if (modIndex < containedIndex) { + BaseRuntimeChildDefinition extension = children.remove(modIndex); + if (containedIndex > children.size()) { + children.add(extension); + } else { + children.add(containedIndex, extension); + } + } + + /* + * Add declared extensions alongside the undeclared ones + */ if (getExtensionsNonModifier().isEmpty() == false) { children.addAll(findIndex(children, "extension"), getExtensionsNonModifier()); } 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 c2fcefe782a..b3be2363024 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 @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.UUID; @@ -48,6 +49,9 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.Tag; import ca.uhn.fhir.model.api.TagList; +import ca.uhn.fhir.model.api.TemporalPrecisionEnum; +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.dstu2.composite.AnnotationDt; import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt; @@ -66,6 +70,7 @@ import ca.uhn.fhir.model.dstu2.resource.Binary; import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry; import ca.uhn.fhir.model.dstu2.resource.Bundle.Link; import ca.uhn.fhir.model.dstu2.resource.Composition; +import ca.uhn.fhir.model.dstu2.resource.Condition; import ca.uhn.fhir.model.dstu2.resource.DataElement; import ca.uhn.fhir.model.dstu2.resource.DiagnosticReport; import ca.uhn.fhir.model.dstu2.resource.Encounter; @@ -108,6 +113,68 @@ public class XmlParserDstu2Test { } + @ResourceDef(name="Patient") + public static class TestPatientFor327 extends Patient + { + + private static final long serialVersionUID = 1L; + + @Child(name = "testCondition") + @ca.uhn.fhir.model.api.annotation.Extension(url="testCondition", definedLocally=true, isModifier=false) + private List testConditions = null; + + public void setCondition(List ref) + { + this.testConditions = ref; + } + + public List getConditions() + { + return this.testConditions; + } + } + + /** + * See #327 + */ + @Test + public void testEncodeExtensionWithContainedResource() { + + TestPatientFor327 patient = new TestPatientFor327(); + patient.setBirthDate(new Date(), TemporalPrecisionEnum.DAY); + + List conditions = new ArrayList(); + Condition condition = new Condition(); + condition.addBodySite().setText("BODY SITE"); + conditions.add(new ResourceReferenceDt(condition)); + patient.setCondition(conditions); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + )); + //@formatter:on + } + + @Test public void testBundleWithBinary() { //@formatter:off diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java index c8f7c4b77d7..3a870d9f642 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/validation/ResourceValidatorDstu2Test.java @@ -12,6 +12,9 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; import org.apache.commons.io.IOUtils; import org.hamcrest.core.StringContains; @@ -21,18 +24,25 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.ExtensionDt; +import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; +import ca.uhn.fhir.model.api.TemporalPrecisionEnum; import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt; import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu2.composite.TimingDt; import ca.uhn.fhir.model.dstu2.resource.AllergyIntolerance; +import ca.uhn.fhir.model.dstu2.resource.Condition; import ca.uhn.fhir.model.dstu2.resource.MedicationOrder; import ca.uhn.fhir.model.dstu2.resource.OperationOutcome; import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.dstu2.valueset.ConditionVerificationStatusEnum; import ca.uhn.fhir.model.dstu2.valueset.ContactPointSystemEnum; +import ca.uhn.fhir.model.dstu2.valueset.NarrativeStatusEnum; import ca.uhn.fhir.model.dstu2.valueset.UnitsOfTimeEnum; import ca.uhn.fhir.model.primitive.DateDt; +import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.parser.XmlParserDstu2Test.TestPatientFor327; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.schematron.SchematronBaseValidator; @@ -42,145 +52,19 @@ public class ResourceValidatorDstu2Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceValidatorDstu2Test.class); private FhirValidator createFhirValidator() { - - AllergyIntolerance allergy = new AllergyIntolerance(); - allergy.getSubstance().addCoding().setCode("some substance"); - FhirValidator val = ourCtx.newValidator(); val.setValidateAgainstStandardSchema(true); val.setValidateAgainstStandardSchematron(true); return val; } - @AfterClass - public static void afterClassClearContext() { - TestUtil.clearAllStaticFieldsForUnitTest(); - } - - - /** - * See - * https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= - * email&utm_source=footer - */ - @Test - public void testValidateWithExtensionsXml() { - PatientProfileDstu2 myPatient = new PatientProfileDstu2(); - myPatient.setColorPrimary(new CodeableConceptDt("http://example.com#animalColor", "furry-grey")); - myPatient.setColorSecondary(new CodeableConceptDt("http://example.com#animalColor", "furry-white")); - myPatient.setOwningOrganization(new ResourceReferenceDt("Organization/2.25.79433498044103547197447759549862032393")); - myPatient.addName().addFamily("FamilyName"); - myPatient.addUndeclaredExtension(new ExtensionDt().setUrl("http://foo.com/example").setValue(new StringDt("String Extension"))); - - IParser p = FhirContext.forDstu2().newXmlParser().setPrettyPrint(true); - String messageString = p.encodeResourceToString(myPatient); - ourLog.info(messageString); - - //@formatter:off - assertThat(messageString, stringContainsInOrder( - "meta", - "Organization/2.25.79433498044103547197447759549862032393", - "furry-grey", - "furry-white", - "String Extension", - "FamilyName" - )); - assertThat(messageString, not(stringContainsInOrder( - "extension", - "meta" - ))); - assertThat(messageString, containsString("url=\"http://ahr.copa.inso.tuwien.ac.at/StructureDefinition/Patient#animal-colorSecondary\"")); - assertThat(messageString, containsString("url=\"http://foo.com/example\"")); - //@formatter:on - - FhirValidator val = ourCtx.newValidator(); - val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); - val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); - - ValidationResult result = val.validateWithResult(messageString); - + private String logOperationOutcome(ValidationResult result) { OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); ourLog.info(encoded); - - assertTrue(result.isSuccessful()); - - assertThat(messageString, containsString("valueReference")); - assertThat(messageString, not(containsString("valueResource"))); + return encoded; } - /** - * See - * https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= - * email&utm_source=footer - */ - @Test - public void testValidateWithExtensionsJson() { - PatientProfileDstu2 myPatient = new PatientProfileDstu2(); - myPatient.setColorPrimary(new CodeableConceptDt("http://example.com#animalColor", "furry-grey")); - myPatient.setColorSecondary(new CodeableConceptDt("http://example.com#animalColor", "furry-white")); - myPatient.setOwningOrganization(new ResourceReferenceDt("Organization/2.25.79433498044103547197447759549862032393")); - myPatient.addName().addFamily("FamilyName"); - myPatient.addUndeclaredExtension(new ExtensionDt().setUrl("http://foo.com/example").setValue(new StringDt("String Extension"))); - - IParser p = FhirContext.forDstu2().newJsonParser().setPrettyPrint(true); - String messageString = p.encodeResourceToString(myPatient); - ourLog.info(messageString); - - //@formatter:off - assertThat(messageString, stringContainsInOrder( - "meta", - "String Extension", - "Organization/2.25.79433498044103547197447759549862032393", - "furry-grey", - "furry-white", - "FamilyName" - )); - assertThat(messageString, not(stringContainsInOrder( - "extension", - "meta" - ))); - //@formatter:on - - FhirValidator val = ourCtx.newValidator(); - val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); - val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); - - ValidationResult result = val.validateWithResult(messageString); - - OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); - ourLog.info(encoded); - - assertTrue(result.isSuccessful()); - - assertThat(messageString, containsString("valueReference")); - assertThat(messageString, not(containsString("valueResource"))); - } - -// @Test -// public void testValidateWithAny() { -// Provenance prov = new Provenance(); -// prov. -// -// IParser p = FhirContext.forDstu2().newJsonParser().setPrettyPrint(true); -// String messageString = p.encodeResourceToString(myPatient); -// ourLog.info(messageString); -// -// FhirValidator val = ourCtx.newValidator(); -//// val.setValidateAgainstStandardSchema(true); -//// val.setValidateAgainstStandardSchematron(true); -// val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); -// val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); -// -// ValidationResult result = val.validateWithResult(messageString); -// -// OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); -// String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); -// ourLog.info(encoded); -// -// assertTrue(result.isSuccessful()); -// } /** * See issue #50 @@ -206,7 +90,7 @@ public class ResourceValidatorDstu2Test { assertEquals(2, ((OperationOutcome) result.toOperationOutcome()).getIssue().size()); assertThat(resultString, StringContains.containsString("2000-15-31")); } - + @SuppressWarnings("deprecation") @Test public void testSchemaBundleValidator() throws IOException { @@ -251,9 +135,8 @@ public class ResourceValidatorDstu2Test { validationResult = val.validateWithResult(b); assertFalse(validationResult.isSuccessful()); - OperationOutcome operationOutcome = (OperationOutcome) validationResult.toOperationOutcome(); - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); - ourLog.info(encoded); + + String encoded = logOperationOutcome(validationResult); assertThat(encoded, containsString("tim-1:")); } @@ -275,6 +158,30 @@ public class ResourceValidatorDstu2Test { assertEquals(1, operationOutcome.getIssue().size()); } +// @Test +// public void testValidateWithAny() { +// Provenance prov = new Provenance(); +// prov. +// +// IParser p = FhirContext.forDstu2().newJsonParser().setPrettyPrint(true); +// String messageString = p.encodeResourceToString(myPatient); +// ourLog.info(messageString); +// +// FhirValidator val = ourCtx.newValidator(); +//// val.setValidateAgainstStandardSchema(true); +//// val.setValidateAgainstStandardSchematron(true); +// val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); +// val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); +// +// ValidationResult result = val.validateWithResult(messageString); +// +// OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); +// String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); +// ourLog.info(encoded); +// +// assertTrue(result.isSuccessful()); +// } + @SuppressWarnings("deprecation") @Test public void testSchemaResourceValidator() throws IOException { @@ -325,4 +232,145 @@ public class ResourceValidatorDstu2Test { validationResult = val.validateWithResult(p); assertTrue(validationResult.isSuccessful()); } + + /** + * Make sure that the elements that appear in all resources (meta, language, extension, etc) + * all appear in the correct order + */ + @Test + public void testValidateResourceWithResourceElements() { + + TestPatientFor327 patient = new TestPatientFor327(); + patient.setBirthDate(new Date(), TemporalPrecisionEnum.DAY); + patient.setId("123"); + patient.getText().setDiv("
FOO
"); + patient.getText().setStatus(NarrativeStatusEnum.GENERATED); + patient.getLanguage().setValue("en"); + patient.addUndeclaredExtension(true, "http://foo").setValue(new StringDt("MOD")); + ResourceMetadataKeyEnum.UPDATED.put(patient, new InstantDt(new Date())); + + List conditions = new ArrayList(); + Condition condition = new Condition(); + condition.getPatient().setReference("Patient/123"); + condition.addBodySite().setText("BODY SITE"); + condition.getCode().setText("CODE"); + condition.setVerificationStatus(ConditionVerificationStatusEnum.CONFIRMED); + conditions.add(new ResourceReferenceDt(condition)); + patient.setCondition(conditions); + patient.addIdentifier().setSystem("http://foo").setValue("123"); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + FhirValidator val = createFhirValidator(); + ValidationResult result = val.validateWithResult(encoded); + + String messageString = logOperationOutcome(result); + + assertTrue(result.isSuccessful()); + + assertThat(messageString, containsString("No issues")); + + } + + /** + * See + * https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= + * email&utm_source=footer + */ + @Test + public void testValidateWithExtensionsJson() { + PatientProfileDstu2 myPatient = new PatientProfileDstu2(); + myPatient.setColorPrimary(new CodeableConceptDt("http://example.com#animalColor", "furry-grey")); + myPatient.setColorSecondary(new CodeableConceptDt("http://example.com#animalColor", "furry-white")); + myPatient.setOwningOrganization(new ResourceReferenceDt("Organization/2.25.79433498044103547197447759549862032393")); + myPatient.addName().addFamily("FamilyName"); + myPatient.addUndeclaredExtension(new ExtensionDt().setUrl("http://foo.com/example").setValue(new StringDt("String Extension"))); + + IParser p = FhirContext.forDstu2().newJsonParser().setPrettyPrint(true); + String messageString = p.encodeResourceToString(myPatient); + ourLog.info(messageString); + + //@formatter:off + assertThat(messageString, stringContainsInOrder( + "meta", + "String Extension", + "Organization/2.25.79433498044103547197447759549862032393", + "furry-grey", + "furry-white", + "FamilyName" + )); + assertThat(messageString, not(stringContainsInOrder( + "extension", + "meta" + ))); + //@formatter:on + + FhirValidator val = ourCtx.newValidator(); + val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); + val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); + + ValidationResult result = val.validateWithResult(messageString); + + logOperationOutcome(result); + + assertTrue(result.isSuccessful()); + + assertThat(messageString, containsString("valueReference")); + assertThat(messageString, not(containsString("valueResource"))); + } + + /** + * See + * https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= + * email&utm_source=footer + */ + @Test + public void testValidateWithExtensionsXml() { + PatientProfileDstu2 myPatient = new PatientProfileDstu2(); + myPatient.setColorPrimary(new CodeableConceptDt("http://example.com#animalColor", "furry-grey")); + myPatient.setColorSecondary(new CodeableConceptDt("http://example.com#animalColor", "furry-white")); + myPatient.setOwningOrganization(new ResourceReferenceDt("Organization/2.25.79433498044103547197447759549862032393")); + myPatient.addName().addFamily("FamilyName"); + myPatient.addUndeclaredExtension(new ExtensionDt().setUrl("http://foo.com/example").setValue(new StringDt("String Extension"))); + + IParser p = FhirContext.forDstu2().newXmlParser().setPrettyPrint(true); + String messageString = p.encodeResourceToString(myPatient); + ourLog.info(messageString); + + //@formatter:off + assertThat(messageString, stringContainsInOrder( + "meta", + "Organization/2.25.79433498044103547197447759549862032393", + "furry-grey", + "furry-white", + "String Extension", + "FamilyName" + )); + assertThat(messageString, not(stringContainsInOrder( + "extension", + "meta" + ))); + assertThat(messageString, containsString("url=\"http://ahr.copa.inso.tuwien.ac.at/StructureDefinition/Patient#animal-colorSecondary\"")); + assertThat(messageString, containsString("url=\"http://foo.com/example\"")); + //@formatter:on + + FhirValidator val = ourCtx.newValidator(); + val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); + val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); + + ValidationResult result = val.validateWithResult(messageString); + + logOperationOutcome(result); + + assertTrue(result.isSuccessful()); + + assertThat(messageString, containsString("valueReference")); + assertThat(messageString, not(containsString("valueResource"))); + } + + @AfterClass + public static void afterClassClearContext() { + TestUtil.clearAllStaticFieldsForUnitTest(); + } } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index 266c8d228e3..8f3b2474a98 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.UUID; @@ -36,6 +37,7 @@ import org.hl7.fhir.dstu3.model.Address.AddressUse; import org.hl7.fhir.dstu3.model.Address.AddressUseEnumFactory; import org.hl7.fhir.dstu3.model.AllergyIntolerance; import org.hl7.fhir.dstu3.model.Annotation; +import org.hl7.fhir.dstu3.model.Appointment; import org.hl7.fhir.dstu3.model.Binary; import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; @@ -68,6 +70,7 @@ import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.Identifier; import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu3.model.InstantType; +import org.hl7.fhir.dstu3.model.Location; import org.hl7.fhir.dstu3.model.Medication; import org.hl7.fhir.dstu3.model.MedicationOrder; import org.hl7.fhir.dstu3.model.MedicationStatement; @@ -79,6 +82,7 @@ import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.PrimitiveType; import org.hl7.fhir.dstu3.model.Quantity; import org.hl7.fhir.dstu3.model.Reference; +import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.dstu3.model.SimpleQuantity; import org.hl7.fhir.dstu3.model.StringType; import org.hl7.fhir.dstu3.model.UriType; @@ -94,6 +98,8 @@ import org.mockito.ArgumentCaptor; import com.google.common.collect.Sets; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; import ca.uhn.fhir.rest.client.IGenericClient; @@ -109,12 +115,6 @@ public class XmlParserDstu3Test { ourCtx.setNarrativeGenerator(null); } - @AfterClass - public static void afterClassClearContext() { - TestUtil.clearAllStaticFieldsForUnitTest(); - } - - @Test public void testBundleWithBinary() { //@formatter:off @@ -146,8 +146,7 @@ public class XmlParserDstu3Test { assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); } - - + @Test public void testContainedResourceInExtensionUndeclared() { Patient p = new Patient(); @@ -170,7 +169,6 @@ public class XmlParserDstu3Test { assertEquals("ORG", o.getName()); } - @Test public void testDuration() { Encounter enc = new Encounter(); @@ -187,23 +185,23 @@ public class XmlParserDstu3Test { @Test public void testEncodeAndParseBundleWithResourceRefs() { - + Patient pt = new Patient(); pt.setId("patid"); pt.addName().addFamily("PATIENT"); - + Organization org = new Organization(); org.setId("orgid"); org.setName("ORG"); pt.getManagingOrganization().setResource(org); - + Bundle bundle = new Bundle(); bundle.addEntry().setResource(pt); bundle.addEntry().setResource(org); - + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(bundle); ourLog.info(encoded); - + //@formatter:off assertThat(encoded, stringContainsInOrder( "", @@ -212,11 +210,11 @@ public class XmlParserDstu3Test { "" )); //@formatter:on - + bundle = ourCtx.newXmlParser().parseResource(Bundle.class, encoded); pt = (Patient) bundle.getEntry().get(0).getResource(); org = (Organization) bundle.getEntry().get(1).getResource(); - + assertEquals("Organization/orgid", org.getIdElement().getValue()); assertEquals("Organization/orgid", pt.getManagingOrganization().getReferenceElement().getValue()); assertSame(org, pt.getManagingOrganization().getResource()); @@ -377,11 +375,9 @@ public class XmlParserDstu3Test { String enc = ourCtx.newXmlParser().encodeResourceToString(patient); assertThat(enc, containsString("")); assertThat(enc, containsString("")); - assertThat(enc, containsString( - "")); + assertThat(enc, containsString("")); assertThat(enc, containsString("")); - assertThat(enc, containsString( - "")); + assertThat(enc, containsString("")); /* * Now parse this back @@ -734,10 +730,10 @@ public class XmlParserDstu3Test { Patient patient = new Patient(); patient.getBirthDateElement().setValueAsString("2016-04-05"); patient.addExtension().setUrl("test").setValue(new Reference(new Condition())); - + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); ourLog.info(encoded); - + //@formatter:off assertThat(encoded, stringContainsInOrder( "", @@ -784,10 +780,8 @@ public class XmlParserDstu3Test { ourLog.info(encoded); // @formatter:on - assertThat(encoded, - stringContainsInOrder("", "", "", "", "", "", - "", "", "", "", "", "", "", "", - "", "", "")); + assertThat(encoded, stringContainsInOrder("", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "")); //@formatter:off } @@ -819,10 +813,8 @@ public class XmlParserDstu3Test { ourLog.info(encoded); //@formatter:on - assertThat(encoded, - stringContainsInOrder("", "", "", "", "", "", - "", "", "", "", "", "", "", "", - "", "", "")); + assertThat(encoded, stringContainsInOrder("", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "")); //@formatter:off } @@ -853,10 +845,8 @@ public class XmlParserDstu3Test { ourLog.info(encoded); //@formatter:on - assertThat(encoded, - stringContainsInOrder("", "", "", "", "", "", - "", "", "", "", "", "", "", "", - "", "", "")); + assertThat(encoded, stringContainsInOrder("", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "")); //@formatter:off } @@ -966,15 +956,13 @@ public class XmlParserDstu3Test { "" ))); //@formatter:on - + obs = parser.parseResource(Observation.class, output); assertEquals(1, obs.getExtension().size()); assertEquals("http://exturl", obs.getExtension().get(0).getUrl()); - assertEquals("ext_url_value", ((StringType)obs.getExtension().get(0).getValue()).getValue()); + assertEquals("ext_url_value", ((StringType) obs.getExtension().get(0).getValue()).getValue()); } - - @Test public void testEncodeExtensionUndeclaredNonModifierWithChildExtension() { Observation obs = new Observation(); @@ -982,14 +970,14 @@ public class XmlParserDstu3Test { obs.getMeta().addProfile("http://profile"); Extension ext = obs.addExtension(); ext.setUrl("http://exturl"); - + Extension subExt = ext.addExtension(); subExt.setUrl("http://subext").setValue(new StringType("sub_ext_value")); - + obs.getCode().setText("CODE"); - + IParser parser = ourCtx.newXmlParser(); - + String output = parser.setPrettyPrint(true).encodeResourceToString(obs); ourLog.info(output); @@ -1007,13 +995,53 @@ public class XmlParserDstu3Test { "" ))); //@formatter:on - + obs = parser.parseResource(Observation.class, output); assertEquals(1, obs.getExtension().size()); assertEquals("http://exturl", obs.getExtension().get(0).getUrl()); assertEquals(1, obs.getExtension().get(0).getExtension().size()); assertEquals("http://subext", obs.getExtension().get(0).getExtension().get(0).getUrl()); - assertEquals("sub_ext_value", ((StringType)obs.getExtension().get(0).getExtension().get(0).getValue()).getValue()); + assertEquals("sub_ext_value", ((StringType) obs.getExtension().get(0).getExtension().get(0).getValue()).getValue()); + } + + /** + * See #327 + */ + @Test + public void testEncodeExtensionWithContainedResource() { + + TestPatientFor327 patient = new TestPatientFor327(); + patient.setBirthDate(new Date()); + + List conditions = new ArrayList(); + Condition condition = new Condition(); + condition.addBodySite().setText("BODY SITE"); + conditions.add(new Reference(condition)); + patient.setCondition(conditions); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + )); + //@formatter:on } @Test @@ -1056,7 +1084,9 @@ public class XmlParserDstu3Test { assertThat(encoded, containsString("family")); assertThat(encoded, containsString("maritalStatus")); } + + @Test public void testEncodeNonContained() { // Create an organization @@ -1152,7 +1182,7 @@ public class XmlParserDstu3Test { assertThat(str, containsString("")); assertThat(str, containsString("")); } - + @Test public void testEncodeSummary() { Patient patient = new Patient(); @@ -1170,7 +1200,7 @@ public class XmlParserDstu3Test { assertThat(encoded, containsString("family")); assertThat(encoded, not(containsString("maritalStatus"))); } - + @Test public void testEncodeSummary2() { Patient patient = new Patient(); @@ -1192,7 +1222,6 @@ public class XmlParserDstu3Test { assertThat(encoded, not(containsString("maritalStatus"))); } - @Test public void testEncodeUndeclaredExtensionWithEnumerationContent() { IParser parser = ourCtx.newXmlParser(); @@ -1214,6 +1243,66 @@ public class XmlParserDstu3Test { } + @Test + public void testEncodeWithContained() { + List contained = new ArrayList(); + + // Will be added by reference + Patient p = new Patient(); + p.setId("#" + "1000"); + contained.add(p); + + // Will be added by direct resource object + Location l = new Location(); + l.setId("#" + "1001"); + contained.add(l); + + // Will not be referred to (and therefore shouldn't appear in output) + Location l2 = new Location(); + l2.setId("#1002"); + contained.add(l2); + + Appointment appointment = new Appointment(); + appointment.setId("1234"); + appointment.getContained().addAll(contained); + + appointment.addParticipant().getActor().setReference("#1000"); + appointment.addParticipant().getActor().setResource(l); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(appointment); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + )); + //@formatter:on + + assertThat(encoded, not(containsString("#1002"))); + } + @Test public void testEncodeWithDontEncodeElements() throws Exception { Patient patient = new Patient(); @@ -1221,7 +1310,7 @@ public class XmlParserDstu3Test { patient.getMeta().addProfile("http://profile"); patient.addName().addFamily("FAMILY").addGiven("GIVEN"); patient.addAddress().addLine("LINE1"); - + { IParser p = ourCtx.newXmlParser(); p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id")); @@ -1288,59 +1377,59 @@ public class XmlParserDstu3Test { patient.getMeta().addProfile("http://profile"); patient.addName().addFamily("FAMILY"); patient.addAddress().addLine("LINE1"); - + Bundle bundle = new Bundle(); bundle.setTotal(100); bundle.addEntry().setResource(patient); { - IParser p = ourCtx.newXmlParser(); - p.setEncodeElements(new HashSet(Arrays.asList("Patient.name", "Bundle.entry"))); - p.setPrettyPrint(true); - String out = p.encodeResourceToString(bundle); - ourLog.info(out); - assertThat(out, not(containsString("total"))); - assertThat(out, (containsString("Patient"))); - assertThat(out, (containsString("name"))); - assertThat(out, not(containsString("address"))); + IParser p = ourCtx.newXmlParser(); + p.setEncodeElements(new HashSet(Arrays.asList("Patient.name", "Bundle.entry"))); + p.setPrettyPrint(true); + String out = p.encodeResourceToString(bundle); + ourLog.info(out); + assertThat(out, not(containsString("total"))); + assertThat(out, (containsString("Patient"))); + assertThat(out, (containsString("name"))); + assertThat(out, not(containsString("address"))); } { - IParser p = ourCtx.newXmlParser(); - p.setEncodeElements(new HashSet(Arrays.asList("Patient.name"))); - p.setEncodeElementsAppliesToResourceTypes(new HashSet(Arrays.asList("Patient"))); - p.setPrettyPrint(true); - String out = p.encodeResourceToString(bundle); - ourLog.info(out); - assertThat(out, (containsString("total"))); - assertThat(out, (containsString("Patient"))); - assertThat(out, (containsString("name"))); - assertThat(out, not(containsString("address"))); + IParser p = ourCtx.newXmlParser(); + p.setEncodeElements(new HashSet(Arrays.asList("Patient.name"))); + p.setEncodeElementsAppliesToResourceTypes(new HashSet(Arrays.asList("Patient"))); + p.setPrettyPrint(true); + String out = p.encodeResourceToString(bundle); + ourLog.info(out); + assertThat(out, (containsString("total"))); + assertThat(out, (containsString("Patient"))); + assertThat(out, (containsString("name"))); + assertThat(out, not(containsString("address"))); } { - IParser p = ourCtx.newXmlParser(); - p.setEncodeElements(new HashSet(Arrays.asList("Patient"))); - p.setEncodeElementsAppliesToResourceTypes(new HashSet(Arrays.asList("Patient"))); - p.setPrettyPrint(true); - String out = p.encodeResourceToString(bundle); - ourLog.info(out); - assertThat(out, (containsString("total"))); - assertThat(out, (containsString("Patient"))); - assertThat(out, (containsString("name"))); - assertThat(out, (containsString("address"))); + IParser p = ourCtx.newXmlParser(); + p.setEncodeElements(new HashSet(Arrays.asList("Patient"))); + p.setEncodeElementsAppliesToResourceTypes(new HashSet(Arrays.asList("Patient"))); + p.setPrettyPrint(true); + String out = p.encodeResourceToString(bundle); + ourLog.info(out); + assertThat(out, (containsString("total"))); + assertThat(out, (containsString("Patient"))); + assertThat(out, (containsString("name"))); + assertThat(out, (containsString("address"))); } - + } - + @Test public void testEncodeWithNarrative() { Patient p = new Patient(); p.addName().addFamily("Smith").addGiven("John"); - + ourCtx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); - + String output = ourCtx.newXmlParser().encodeResourceToString(p); ourLog.info(output); - + assertThat(output, containsString("
John SMITH ")); } @@ -1391,26 +1480,24 @@ public class XmlParserDstu3Test { String enc = ourCtx.newXmlParser().encodeResourceToString(patient); assertThat(enc, containsString("")); - assertThat( - enc, - containsString("")); + assertThat(enc, containsString("")); assertThat(enc, containsString("")); assertThat(enc, containsString("")); } - @Test public void testOmitResourceId() { Patient p = new Patient(); p.setId("123"); p.addName().addFamily("ABC"); - + assertThat(ourCtx.newXmlParser().encodeResourceToString(p), stringContainsInOrder("123", "ABC")); assertThat(ourCtx.newXmlParser().setOmitResourceId(true).encodeResourceToString(p), containsString("ABC")); assertThat(ourCtx.newXmlParser().setOmitResourceId(true).encodeResourceToString(p), not(containsString("123"))); } - @Test @Ignore + @Test + @Ignore public void testParseAndEncodeBundle() throws Exception { String content = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle-example.xml")); @@ -1435,7 +1522,7 @@ public class XmlParserDstu3Test { Medication m = (Medication) parsed.getEntry().get(1).getResource(); assertEquals("http://example.com/base/Medication/example", m.getId()); - assertSame(((Reference)p.getMedication()).getResource(), m); + assertSame(((Reference) p.getMedication()).getResource(), m); String reencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(parsed); ourLog.info(reencoded); @@ -1445,7 +1532,8 @@ public class XmlParserDstu3Test { } - @Test @Ignore + @Test + @Ignore public void testParseAndEncodeBundleNewStyle() throws Exception { String content = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle-example.xml")); @@ -1468,11 +1556,11 @@ public class XmlParserDstu3Test { assertEquals("Patient/347", p.getPatient().getReference()); assertEquals("2014-08-16T05:31:17Z", p.getMeta().getLastUpdatedElement().getValueAsString()); assertEquals("http://example.com/base/MedicationOrder/3123/_history/1", p.getId()); -// assertEquals("3123", p.getId()); + // assertEquals("3123", p.getId()); Medication m = (Medication) parsed.getEntry().get(1).getResource(); assertEquals("http://example.com/base/Medication/example", m.getId()); - assertSame(((Reference)p.getMedication()).getResource(), m); + assertSame(((Reference) p.getMedication()).getResource(), m); String reencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(parsed); ourLog.info(reencoded); @@ -1482,7 +1570,6 @@ public class XmlParserDstu3Test { } - @Test public void testParseAndEncodeComments() throws IOException { //@formatter:off @@ -2196,7 +2283,8 @@ public class XmlParserDstu3Test { /* * If this fails, it's possibe the DocumentManifest structure is wrong: It should be * - * @Child(name = "p", type = {Attachment.class, ValueSet.class}, order=1, min=1, max=1, modifier=false, summary=true) + * @Child(name = "p", type = {Attachment.class, ValueSet.class}, order=1, min=1, max=1, modifier=false, + * summary=true) */ assertNotNull(((Reference) actual.getContent().get(0).getP()).getResource()); } @@ -2394,6 +2482,11 @@ public class XmlParserDstu3Test { assertEquals("Patient", reincarnatedPatient.getIdElement().getResourceType()); } + @AfterClass + public static void afterClassClearContext() { + TestUtil.clearAllStaticFieldsForUnitTest(); + } + @BeforeClass public static void beforeClass() { XMLUnit.setIgnoreAttributeOrder(true); @@ -2407,4 +2500,22 @@ public class XmlParserDstu3Test { c.read().resource("Patient").withId("324").execute(); } + @ResourceDef(name = "Patient") + public static class TestPatientFor327 extends Patient { + + private static final long serialVersionUID = 1L; + + @Child(name = "testCondition") + @ca.uhn.fhir.model.api.annotation.Extension(url = "testCondition", definedLocally = true, isModifier = false) + private List testConditions = null; + + public List getConditions() { + return this.testConditions; + } + + public void setCondition(List ref) { + this.testConditions = ref; + } + } + } diff --git a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java index db7fa3a7b62..6a251ec962d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java @@ -6,9 +6,15 @@ import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import org.apache.naming.StringManager; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.Coding; +import org.hl7.fhir.dstu3.model.Condition; +import org.hl7.fhir.dstu3.model.Condition.ConditionVerificationStatus; +import org.hl7.fhir.dstu3.model.Narrative.NarrativeStatus; import org.hl7.fhir.dstu3.model.OperationOutcome; import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.StringType; @@ -16,7 +22,9 @@ import org.junit.AfterClass; import org.junit.Test; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.parser.XmlParserDstu3Test; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.FhirValidator; import ca.uhn.fhir.validation.SchemaBaseValidator; @@ -32,6 +40,52 @@ public class ResourceValidatorDstu3Test { TestUtil.clearAllStaticFieldsForUnitTest(); } + + /** + * Make sure that the elements that appear in all resources (meta, language, extension, etc) + * all appear in the correct order + */ + @Test + public void testValidateResourceWithResourceElements() { + + XmlParserDstu3Test.TestPatientFor327 patient = new XmlParserDstu3Test.TestPatientFor327(); + patient.setBirthDate(new Date()); + patient.setId("123"); + patient.getText().setDivAsString("
FOO
"); + patient.getText().setStatus(NarrativeStatus.GENERATED); + patient.getLanguageElement().setValue("en"); + patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD")); + patient.getMeta().setLastUpdated(new Date()); + + List conditions = new ArrayList(); + Condition condition = new Condition(); + condition.getPatient().setReference("Patient/123"); + condition.addBodySite().setText("BODY SITE"); + condition.getCode().setText("CODE"); + condition.setVerificationStatus(ConditionVerificationStatus.CONFIRMED); + conditions.add(new Reference(condition)); + patient.setCondition(conditions); + patient.addIdentifier().setSystem("http://foo").setValue("123"); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + FhirValidator val = ourCtx.newValidator(); + val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); + val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); + val.registerValidatorModule(new FhirInstanceValidator()); + + ValidationResult result = val.validateWithResult(encoded); + + OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); + String ooencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); + ourLog.info(ooencoded); + + assertTrue(result.isSuccessful()); + + assertThat(ooencoded, containsString("No issues")); + } + /** * See * https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= 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 b7c23943453..fa73bf56e00 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 @@ -16,6 +16,7 @@ import java.io.StringReader; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -33,6 +34,7 @@ import org.hl7.fhir.instance.model.Bundle; import org.hl7.fhir.instance.model.Bundle.BundleEntryComponent; import org.hl7.fhir.instance.model.CodeableConcept; import org.hl7.fhir.instance.model.Composition; +import org.hl7.fhir.instance.model.Condition; import org.hl7.fhir.instance.model.DateTimeType; import org.hl7.fhir.instance.model.DateType; import org.hl7.fhir.instance.model.DecimalType; @@ -68,6 +70,8 @@ import org.xml.sax.SAXException; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.narrative.INarrativeGenerator; import ca.uhn.fhir.parser.JsonParserHl7OrgDstu2Test.MyPatientWithOneDeclaredAddressExtension; import ca.uhn.fhir.parser.JsonParserHl7OrgDstu2Test.MyPatientWithOneDeclaredExtension; @@ -78,494 +82,247 @@ import net.sf.json.JSONSerializer; public class XmlParserHl7OrgDstu2Test { - private static FhirContext ourCtx; - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserHl7OrgDstu2Test.class); + private static FhirContext ourCtx; + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParserHl7OrgDstu2Test.class); - @After - public void after() { - ourCtx.setAddProfileTagWhenEncoding(AddProfileTagEnum.ONLY_FOR_CUSTOM); - } + @After + public void after() { + ourCtx.setAddProfileTagWhenEncoding(AddProfileTagEnum.ONLY_FOR_CUSTOM); + } - private String fixDivNodeText(String htmlNoNs) { - return htmlNoNs.replace("
", "
"); - } - - private String fixDivNodeTextJson(String htmlNoNs) { - return htmlNoNs.replace("
", "
"); - } + private String fixDivNodeText(String htmlNoNs) { + return htmlNoNs.replace("
", "
"); + } - - @Test - public void testComposition() { + private String fixDivNodeTextJson(String htmlNoNs) { + return htmlNoNs.replace("
", "
"); + } - Composition comp = new Composition(); - comp.setId("1"); + @Test + public void testComposition() { - ourCtx.newXmlParser().encodeResourceToString(comp); - ourCtx.newXmlParser().encodeResourceToString(comp); - ourCtx.newXmlParser().encodeResourceToString(comp); - ourCtx.newXmlParser().encodeResourceToString(comp); + Composition comp = new Composition(); + comp.setId("1"); - // comp. + ourCtx.newXmlParser().encodeResourceToString(comp); + ourCtx.newXmlParser().encodeResourceToString(comp); + ourCtx.newXmlParser().encodeResourceToString(comp); + ourCtx.newXmlParser().encodeResourceToString(comp); - } + // comp. - @Test - public void testContainedResourceInExtensionUndeclared() { - Patient p = new Patient(); - p.addName().addFamily("PATIENT"); - - Organization o = new Organization(); - o.setName("ORG"); - p.addExtension().setUrl("urn:foo").setValue(new Reference(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.getExtension(); - assertEquals(1, exts.size()); - Reference rr = (Reference)exts.get(0).getValue(); - o = (Organization) rr.getResource(); - assertEquals("ORG", o.getName()); - } + } - - @Test - public void testDuplicateContainedResources() { + @Test + public void testContainedResourceInExtensionUndeclared() { + Patient p = new Patient(); + p.addName().addFamily("PATIENT"); - Observation resA = new Observation(); - resA.getCode().setText("A"); + Organization o = new Organization(); + o.setName("ORG"); + p.addExtension().setUrl("urn:foo").setValue(new Reference(o)); - Observation resB = new Observation(); - resB.getCode().setText("B"); - resB.addRelated().setTarget(new Reference(resA)); - resB.addRelated().setTarget(new Reference(resA)); + String str = ourCtx.newXmlParser().encodeResourceToString(p); + ourLog.info(str); - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(resB); - ourLog.info(encoded); + p = ourCtx.newXmlParser().parseResource(Patient.class, str); + assertEquals("PATIENT", p.getName().get(0).getFamily().get(0).getValue()); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", ""))); - assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "", "")))); + List exts = p.getExtension(); + assertEquals(1, exts.size()); + Reference rr = (Reference) exts.get(0).getValue(); + o = (Organization) rr.getResource(); + assertEquals("ORG", o.getName()); + } - } + @Test + public void testDuplicateContainedResources() { - @Test - public void testEncodeAndParseContained() { - IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); + Observation resA = new Observation(); + resA.getCode().setText("A"); - // Create an organization, note that the organization does not have an ID - Organization org = new Organization(); - org.getNameElement().setValue("Contained Test Organization"); + Observation resB = new Observation(); + resB.getCode().setText("B"); + resB.addRelated().setTarget(new Reference(resA)); + resB.addRelated().setTarget(new Reference(resA)); - // Create a patient - Patient patient = new Patient(); - patient.setId("Patient/1333"); - patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(resB); + ourLog.info(encoded); - // Put the organization as a reference in the patient resource - patient.getManagingOrganization().setResource(org); + assertThat(encoded, + stringContainsInOrder(Arrays.asList("", "", ""))); + assertThat(encoded, not(stringContainsInOrder( + Arrays.asList("", "", "")))); - String encoded = xmlParser.encodeResourceToString(patient); - ourLog.info(encoded); - assertThat(encoded, containsString("")); - assertThat(encoded, containsString("")); + } - // Create a bundle with just the patient resource - Bundle b = new Bundle(); - b.addEntry().setResource(patient); + @Test + public void testEncodeAndParseContained() { + IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); - // Encode the bundle - encoded = xmlParser.encodeResourceToString(b); - ourLog.info(encoded); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", ""))); - assertThat(encoded, containsString("")); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", ""))); - assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "", "")))); + // Create an organization, note that the organization does not have an ID + Organization org = new Organization(); + org.getNameElement().setValue("Contained Test Organization"); - // Re-parse the bundle - patient = (Patient) xmlParser.parseResource(xmlParser.encodeResourceToString(patient)); - assertEquals("#1", patient.getManagingOrganization().getReferenceElement().getValue()); + // Create a patient + Patient patient = new Patient(); + patient.setId("Patient/1333"); + patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); - assertNotNull(patient.getManagingOrganization().getResource()); - org = (Organization) patient.getManagingOrganization().getResource(); - assertEquals("#1", org.getIdElement().getValue()); - assertEquals("Contained Test Organization", org.getName()); + // Put the organization as a reference in the patient resource + patient.getManagingOrganization().setResource(org); - // And re-encode a second time - encoded = xmlParser.encodeResourceToString(patient); - ourLog.info(encoded); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", "", ""))); - assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); - assertThat(encoded, containsString("")); + String encoded = xmlParser.encodeResourceToString(patient); + ourLog.info(encoded); + assertThat(encoded, containsString("")); + assertThat(encoded, containsString("")); - // And re-encode once more, with the references cleared - patient.getContained().clear(); - patient.getManagingOrganization().setReference(null); - encoded = xmlParser.encodeResourceToString(patient); - ourLog.info(encoded); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", "", ""))); - assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); - assertThat(encoded, containsString("")); + // Create a bundle with just the patient resource + Bundle b = new Bundle(); + b.addEntry().setResource(patient); - // And re-encode once more, with the references cleared and a manually set local ID - patient.getContained().clear(); - patient.getManagingOrganization().setReference(null); - patient.getManagingOrganization().getResource().setId(("#333")); - encoded = xmlParser.encodeResourceToString(patient); - ourLog.info(encoded); - assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", "", ""))); - assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); + // Encode the bundle + encoded = xmlParser.encodeResourceToString(b); + ourLog.info(encoded); + assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", ""))); + assertThat(encoded, containsString("")); + assertThat(encoded, stringContainsInOrder(Arrays.asList("", ""))); + assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "", "")))); - } + // Re-parse the bundle + patient = (Patient) xmlParser.parseResource(xmlParser.encodeResourceToString(patient)); + assertEquals("#1", patient.getManagingOrganization().getReferenceElement().getValue()); - @Test - public void testEncodeAndParseExtensions() throws Exception { + assertNotNull(patient.getManagingOrganization().getResource()); + org = (Organization) patient.getManagingOrganization().getResource(); + assertEquals("#1", org.getIdElement().getValue()); + assertEquals("Contained Test Organization", org.getName()); - Patient patient = new Patient(); - patient.addIdentifier().setUse(IdentifierUse.OFFICIAL).setSystem("urn:example").setValue("7000135"); + // And re-encode a second time + encoded = xmlParser.encodeResourceToString(patient); + ourLog.info(encoded); + assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", + "", ""))); + assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); + assertThat(encoded, containsString("")); - Extension ext = new Extension(); - ext.setUrl("http://example.com/extensions#someext"); - ext.setValue(new DateTimeType("2011-01-02T11:13:15")); - patient.getExtension().add(ext); + // And re-encode once more, with the references cleared + patient.getContained().clear(); + patient.getManagingOrganization().setReference(null); + encoded = xmlParser.encodeResourceToString(patient); + ourLog.info(encoded); + assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", + "", ""))); + assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); + assertThat(encoded, containsString("")); - Extension parent = new Extension().setUrl("http://example.com#parent"); - patient.getExtension().add(parent); - Extension child1 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); - parent.getExtension().add(child1); - Extension child2 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value2")); - parent.getExtension().add(child2); + // And re-encode once more, with the references cleared and a manually set + // local ID + patient.getContained().clear(); + patient.getManagingOrganization().setReference(null); + patient.getManagingOrganization().getResource().setId(("#333")); + encoded = xmlParser.encodeResourceToString(patient); + ourLog.info(encoded); + assertThat(encoded, stringContainsInOrder(Arrays.asList("", "", + "", ""))); + assertThat(encoded, not(stringContainsInOrder(Arrays.asList("", "")))); - Extension modExt = new Extension(); - modExt.setUrl("http://example.com/extensions#modext"); - modExt.setValue(new DateType("1995-01-02")); - patient.getModifierExtension().add(modExt); + } - HumanName name = patient.addName(); - name.addFamily("Blah"); - StringType given = name.addGivenElement(); - given.setValue("Joe"); - Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("given")); - given.getExtension().add(ext2); + @Test + public void testEncodeAndParseExtensions() throws Exception { - StringType given2 = name.addGivenElement(); - given2.setValue("Shmoe"); - Extension given2ext = new Extension().setUrl("http://examples.com#givenext_parent"); - given2.getExtension().add(given2ext); - given2ext.addExtension().setUrl("http://examples.com#givenext_child").setValue(new StringType("CHILD")); + Patient patient = new Patient(); + patient.addIdentifier().setUse(IdentifierUse.OFFICIAL).setSystem("urn:example").setValue("7000135"); - String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); - ourLog.info(output); + Extension ext = new Extension(); + ext.setUrl("http://example.com/extensions#someext"); + ext.setValue(new DateTimeType("2011-01-02T11:13:15")); + patient.getExtension().add(ext); - String enc = ourCtx.newXmlParser().encodeResourceToString(patient); - assertThat(enc, containsString("")); - assertThat(enc, containsString("")); - assertThat(enc, containsString("")); - assertThat(enc, containsString("")); - assertThat(enc, containsString("")); + Extension parent = new Extension().setUrl("http://example.com#parent"); + patient.getExtension().add(parent); + Extension child1 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); + parent.getExtension().add(child1); + Extension child2 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value2")); + parent.getExtension().add(child2); - /* - * Now parse this back - */ + Extension modExt = new Extension(); + modExt.setUrl("http://example.com/extensions#modext"); + modExt.setValue(new DateType("1995-01-02")); + patient.getModifierExtension().add(modExt); - Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, enc); - ext = parsed.getExtension().get(0); - assertEquals("http://example.com/extensions#someext", ext.getUrl()); - assertEquals("2011-01-02T11:13:15", ((DateTimeType) ext.getValue()).getValueAsString()); + HumanName name = patient.addName(); + name.addFamily("Blah"); + StringType given = name.addGivenElement(); + given.setValue("Joe"); + Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("given")); + given.getExtension().add(ext2); - parent = patient.getExtension().get(1); - assertEquals("http://example.com#parent", parent.getUrl()); - assertNull(parent.getValue()); - child1 = parent.getExtension().get(0); - assertEquals("http://example.com#child", child1.getUrl()); - assertEquals("value1", ((StringType) child1.getValue()).getValueAsString()); - child2 = parent.getExtension().get(1); - assertEquals("http://example.com#child", child2.getUrl()); - assertEquals("value2", ((StringType) child2.getValue()).getValueAsString()); + StringType given2 = name.addGivenElement(); + given2.setValue("Shmoe"); + Extension given2ext = new Extension().setUrl("http://examples.com#givenext_parent"); + given2.getExtension().add(given2ext); + given2ext.addExtension().setUrl("http://examples.com#givenext_child").setValue(new StringType("CHILD")); - modExt = parsed.getModifierExtension().get(0); - assertEquals("http://example.com/extensions#modext", modExt.getUrl()); - assertEquals("1995-01-02", ((DateType) modExt.getValue()).getValueAsString()); + String output = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(output); - name = parsed.getName().get(0); + String enc = ourCtx.newXmlParser().encodeResourceToString(patient); + assertThat(enc, containsString( + "")); + assertThat(enc, containsString( + "")); + assertThat(enc, containsString( + "")); + assertThat(enc, containsString( + "")); + assertThat(enc, containsString( + "")); - ext2 = name.getGiven().get(0).getExtension().get(0); - assertEquals("http://examples.com#givenext", ext2.getUrl()); - assertEquals("given", ((StringType) ext2.getValue()).getValueAsString()); + /* + * Now parse this back + */ - given2ext = name.getGiven().get(1).getExtension().get(0); - assertEquals("http://examples.com#givenext_parent", given2ext.getUrl()); - assertNull(given2ext.getValue()); - Extension given2ext2 = given2ext.getExtension().get(0); - assertEquals("http://examples.com#givenext_child", given2ext2.getUrl()); - assertEquals("CHILD", ((StringType) given2ext2.getValue()).getValue()); + Patient parsed = ourCtx.newXmlParser().parseResource(Patient.class, enc); + ext = parsed.getExtension().get(0); + assertEquals("http://example.com/extensions#someext", ext.getUrl()); + assertEquals("2011-01-02T11:13:15", ((DateTimeType) ext.getValue()).getValueAsString()); - } + parent = patient.getExtension().get(1); + assertEquals("http://example.com#parent", parent.getUrl()); + assertNull(parent.getValue()); + child1 = parent.getExtension().get(0); + assertEquals("http://example.com#child", child1.getUrl()); + assertEquals("value1", ((StringType) child1.getValue()).getValueAsString()); + child2 = parent.getExtension().get(1); + assertEquals("http://example.com#child", child2.getUrl()); + assertEquals("value2", ((StringType) child2.getValue()).getValueAsString()); - // TODO: uncomment with new model updates -// @Test -// public void testEncodeAndParseExtensionOnResourceReference() { -// DataElement de = new DataElement(); -// Binding b = de.addElement().getBinding(); -// b.setName("BINDING"); -// -// Organization o = new Organization(); -// o.setName("ORG"); -// b.addUndeclaredExtension(new ExtensionDt(false, "urn:foo", new ResourceReferenceDt(o))); -// -// 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(); -// o = (Organization) rr.getResource(); -// assertEquals("ORG", o.getName()); -// -// } -// -// @Test -// public void testParseAndEncodeExtensionOnResourceReference() { -// //@formatter:off -// String input = "" + -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""+ -// ""; -// //@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()); -// -// 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); -// } - + modExt = parsed.getModifierExtension().get(0); + assertEquals("http://example.com/extensions#modext", modExt.getUrl()); + assertEquals("1995-01-02", ((DateType) modExt.getValue()).getValueAsString()); - - /** - * See #216 - Profiled datatypes should use their unprofiled parent type as the choice[x] name + name = parsed.getName().get(0); + + ext2 = name.getGiven().get(0).getExtension().get(0); + assertEquals("http://examples.com#givenext", ext2.getUrl()); + assertEquals("given", ((StringType) ext2.getValue()).getValueAsString()); + + given2ext = name.getGiven().get(1).getExtension().get(0); + assertEquals("http://examples.com#givenext_parent", given2ext.getUrl()); + assertNull(given2ext.getValue()); + Extension given2ext2 = given2ext.getExtension().get(0); + assertEquals("http://examples.com#givenext_child", given2ext2.getUrl()); + assertEquals("CHILD", ((StringType) given2ext2.getValue()).getValue()); + + } + + /** + * See #216 - Profiled datatypes should use their unprofiled parent type as + * the choice[x] name * - * Disabled after conversation with Grahame + * Disabled after conversation with Grahame */ @Test @Ignore @@ -576,895 +333,1164 @@ public class XmlParserHl7OrgDstu2Test { MedicationStatement ms = xmlParser.parseResource(MedicationStatement.class, input); SimpleQuantity q = (SimpleQuantity) ms.getDosage().get(0).getQuantity(); assertEquals("1", q.getValueElement().getValueAsString()); - + String output = xmlParser.encodeResourceToString(ms); assertThat(output, containsString("")); } - @Test - public void testEncodeBinaryResource() { + @Test + public void testEncodeBinaryResource() { - Binary patient = new Binary(); - patient.setContentType("foo"); - patient.setContent(new byte[] { 1, 2, 3, 4 }); + Binary patient = new Binary(); + patient.setContentType("foo"); + patient.setContent(new byte[] { 1, 2, 3, 4 }); - String val = ourCtx.newXmlParser().encodeResourceToString(patient); - assertEquals("", val); + String val = ourCtx.newXmlParser().encodeResourceToString(patient); + assertEquals( + "", + val); - } + } - @Test - public void testEncodeBinaryWithNoContentType() { - Binary b = new Binary(); - b.setContent(new byte[] { 1, 2, 3, 4 }); + // TODO: uncomment with new model updates + // @Test + // public void testEncodeAndParseExtensionOnResourceReference() { + // DataElement de = new DataElement(); + // Binding b = de.addElement().getBinding(); + // b.setName("BINDING"); + // + // Organization o = new Organization(); + // o.setName("ORG"); + // b.addUndeclaredExtension(new ExtensionDt(false, "urn:foo", new + // ResourceReferenceDt(o))); + // + // 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(); + // o = (Organization) rr.getResource(); + // assertEquals("ORG", o.getName()); + // + // } + // + // @Test + // public void testParseAndEncodeExtensionOnResourceReference() { + // //@formatter:off + // String input = "" + + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""+ + // ""; + // //@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()); + // + // 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); + // } - String output = ourCtx.newXmlParser().encodeResourceToString(b); - ourLog.info(output); + @Test + public void testEncodeBinaryWithNoContentType() { + Binary b = new Binary(); + b.setContent(new byte[] { 1, 2, 3, 4 }); - assertEquals("", output); - } + String output = ourCtx.newXmlParser().encodeResourceToString(b); + ourLog.info(output); - - @Test - public void testEncodeBoundCode() { + assertEquals("", output); + } - Patient patient = new Patient(); - patient.addAddress().setUse(AddressUse.HOME); + @Test + public void testEncodeBoundCode() { - patient.getGenderElement().setValue(AdministrativeGender.MALE); + Patient patient = new Patient(); + patient.addAddress().setUse(AddressUse.HOME); - String val = ourCtx.newXmlParser().encodeResourceToString(patient); - ourLog.info(val); + patient.getGenderElement().setValue(AdministrativeGender.MALE); - assertThat(val, containsString("home")); - assertThat(val, containsString("male")); - } + String val = ourCtx.newXmlParser().encodeResourceToString(patient); + ourLog.info(val); - @Test - public void testEncodeBundle() throws InterruptedException { - Bundle b = new Bundle(); - b.getMeta().addTag().setSystem("http://hl7.org/fhir/tag").setCode("http://hl7.org/fhir/tag/message").setDisplay("Message"); + assertThat(val, containsString("home")); + assertThat(val, containsString("male")); + } - InstantType pub = InstantType.withCurrentTime(); - b.getMeta().setLastUpdatedElement(pub); + @Test + public void testEncodeBundle() throws InterruptedException { + Bundle b = new Bundle(); + b.getMeta().addTag().setSystem("http://hl7.org/fhir/tag").setCode("http://hl7.org/fhir/tag/message") + .setDisplay("Message"); - Patient p1 = new Patient(); - p1.addName().addFamily("Family1"); - BundleEntryComponent entry = b.addEntry(); - p1.getIdElement().setValue("1"); - entry.setResource(p1); + InstantType pub = InstantType.withCurrentTime(); + b.getMeta().setLastUpdatedElement(pub); - Patient p2 = new Patient(); - p2.addName().addFamily("Family2"); - entry = b.addEntry(); - p2.getIdElement().setValue("2"); - entry.setResource(p2); + Patient p1 = new Patient(); + p1.addName().addFamily("Family1"); + BundleEntryComponent entry = b.addEntry(); + p1.getIdElement().setValue("1"); + entry.setResource(p1); - String bundleString = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(bundleString); + Patient p2 = new Patient(); + p2.addName().addFamily("Family2"); + entry = b.addEntry(); + p2.getIdElement().setValue("2"); + entry.setResource(p2); - //@formatter:on - String[] strings = { - "", - "", - "", - "", - "", - "" - }; - //@formatter:off - - assertThat(bundleString, StringContainsInOrder.stringContainsInOrder(strings)); - } + String bundleString = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); + ourLog.info(bundleString); - @Test - public void testEncodeBundleCategory() { + // @formatter:on + String[] strings = { "", + "", "", + "", "", "" }; + // @formatter:off - Bundle b = new Bundle(); - BundleEntryComponent e = b.addEntry(); - e.setResource(new Patient()); - e.getResource().getMeta().addTag().setSystem("scheme").setCode("term").setDisplay("label"); + assertThat(bundleString, StringContainsInOrder.stringContainsInOrder(strings)); + } - String val = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(val); + @Test + public void testEncodeBundleCategory() { - //@formatter:off - assertThat(val, stringContainsInOrder("", - "", - "", - "", - "")); - //@formatter:on + Bundle b = new Bundle(); + BundleEntryComponent e = b.addEntry(); + e.setResource(new Patient()); + e.getResource().getMeta().addTag().setSystem("scheme").setCode("term").setDisplay("label"); - b = ourCtx.newXmlParser().parseResource(Bundle.class, val); - assertEquals(1, b.getEntry().size()); - assertEquals(1, b.getEntry().get(0).getResource().getMeta().getTag().size()); - assertEquals("scheme", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getSystem()); - assertEquals("term", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getCode()); - assertEquals("label", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getDisplay()); - } - @Test - public void testEncodeContainedAndIncludedResources() { + String val = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); + ourLog.info(val); - DiagnosticReport rpt = new DiagnosticReport(); - rpt.getCode().setText("Report"); + // @formatter:off + assertThat(val, stringContainsInOrder("", "", "", + "", "")); + // @formatter:on - Specimen spm = new Specimen(); - spm.addIdentifier().setValue("Report1ContainedSpecimen1"); - rpt.addSpecimen().setResource(spm); + b = ourCtx.newXmlParser().parseResource(Bundle.class, val); + assertEquals(1, b.getEntry().size()); + assertEquals(1, b.getEntry().get(0).getResource().getMeta().getTag().size()); + assertEquals("scheme", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getSystem()); + assertEquals("term", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getCode()); + assertEquals("label", b.getEntry().get(0).getResource().getMeta().getTag().get(0).getDisplay()); + } - IParser p = ourCtx.newXmlParser().setPrettyPrint(true); - String str = p.encodeResourceToString(rpt); + @Test + public void testEncodeContainedAndIncludedResources() { - ourLog.info(str); + DiagnosticReport rpt = new DiagnosticReport(); + rpt.getCode().setText("Report"); - } - - @Test - public void testEncodeContainedResources() throws Exception { + Specimen spm = new Specimen(); + spm.addIdentifier().setValue("Report1ContainedSpecimen1"); + rpt.addSpecimen().setResource(spm); - DiagnosticReport rpt = new DiagnosticReport(); - Specimen spm = new Specimen(); - spm.addIdentifier().setSystem("urn").setValue("123"); - rpt.getText().setDivAsString("AAA"); - rpt.addSpecimen().setResource(spm); + IParser p = ourCtx.newXmlParser().setPrettyPrint(true); + String str = p.encodeResourceToString(rpt); - IParser p = ourCtx.newXmlParser().setPrettyPrint(true); - String str = p.encodeResourceToString(rpt); + ourLog.info(str); - 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, stringContainsInOrder("", "")); - assertThat(str, IsNot.not(StringContains.containsString(""))); + @Test + public void testEncodeContainedResources() throws Exception { - } + DiagnosticReport rpt = new DiagnosticReport(); + Specimen spm = new Specimen(); + spm.addIdentifier().setSystem("urn").setValue("123"); + rpt.getText().setDivAsString("AAA"); + rpt.addSpecimen().setResource(spm); - @Test - public void testEncodeContainedWithNarrativeIsSuppresed() throws Exception { - IParser parser = ourCtx.newXmlParser().setPrettyPrint(true); + IParser p = ourCtx.newXmlParser().setPrettyPrint(true); + String str = p.encodeResourceToString(rpt); - // Create an organization, note that the organization does not have an ID - Organization org = new Organization(); - org.getNameElement().setValue("Contained Test Organization"); - org.getText().setDivAsString("
FOOBAR
"); + ourLog.info(str); + assertThat(str, StringContains.containsString("
AAA
")); + assertThat(str, StringContains.containsString("reference value=\"#")); - // Create a patient - Patient patient = new Patient(); - patient.setId("Patient/1333"); - patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); - patient.getText().setDivAsString("
BARFOO
"); - patient.getManagingOrganization().setResource(org); + int idx = str.indexOf("reference value=\"#") + "reference value=\"#".length(); + int idx2 = str.indexOf('"', idx + 1); + String id = str.substring(idx, idx2); + assertThat(str, stringContainsInOrder("", "")); + assertThat(str, IsNot.not(StringContains.containsString(""))); - String encoded = parser.encodeResourceToString(patient); - ourLog.info(encoded); - - assertThat(encoded, stringContainsInOrder("", "
BARFOO
", "", "", "", "FOOBAR
"); - MyPatientWithOneDeclaredAddressExtension patient = new MyPatientWithOneDeclaredAddressExtension(); - patient.addAddress().setUse(AddressUse.HOME); - patient.setFoo(new Address().addLine("line1")); + // Create a patient + Patient patient = new Patient(); + patient.setId("Patient/1333"); + patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); + patient.getText().setDivAsString("
BARFOO
"); + patient.getManagingOrganization().setResource(org); - String val = parser.encodeResourceToString(patient); - ourLog.info(val); - assertThat(val, StringContains.containsString("")); + String encoded = parser.encodeResourceToString(patient); + ourLog.info(encoded); - MyPatientWithOneDeclaredAddressExtension actual = parser.parseResource(MyPatientWithOneDeclaredAddressExtension.class, val); - assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); - Address ref = actual.getFoo(); - assertEquals("line1", ref.getLine().get(0).getValue()); + assertThat(encoded, stringContainsInOrder("", + "
BARFOO
", "", "", "", "")); + MyPatientWithOneDeclaredAddressExtension patient = new MyPatientWithOneDeclaredAddressExtension(); + patient.addAddress().setUse(AddressUse.HOME); + patient.setFoo(new Address().addLine("line1")); - MyPatientWithOneDeclaredExtension actual = parser.parseResource(MyPatientWithOneDeclaredExtension.class, val); - assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); - Reference ref = actual.getFoo(); - assertEquals("Organization/123", ref.getReferenceElement().getValue()); + String val = parser.encodeResourceToString(patient); + ourLog.info(val); + assertThat(val, StringContains + .containsString("")); - } + MyPatientWithOneDeclaredAddressExtension actual = parser + .parseResource(MyPatientWithOneDeclaredAddressExtension.class, val); + assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); + Address ref = actual.getFoo(); + assertEquals("line1", ref.getLine().get(0).getValue()); - /** - * #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"))); - } + @Test + public void testEncodeDeclaredExtensionWithResourceContent() { + IParser parser = ourCtx.newXmlParser(); - @Test - public void testEncodeEscapedChars() { + MyPatientWithOneDeclaredExtension patient = new MyPatientWithOneDeclaredExtension(); + patient.addAddress().setUse(AddressUse.HOME); + patient.setFoo(new Reference("Organization/123")); - Patient p = new Patient(); - p.addName().addFamily("and <>&ü"); + String val = parser.encodeResourceToString(patient); + ourLog.info(val); + assertThat(val, StringContains.containsString( + "")); - String enc = ourCtx.newXmlParser().encodeResourceToString(p); - ourLog.info(enc); + MyPatientWithOneDeclaredExtension actual = parser.parseResource(MyPatientWithOneDeclaredExtension.class, val); + assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); + Reference ref = actual.getFoo(); + assertEquals("Organization/123", ref.getReferenceElement().getValue()); - p = ourCtx.newXmlParser().parseResource(Patient.class, enc); - assertEquals("and <>&ü", p.getName().get(0).getFamily().get(0).getValue()); + } - p = ourCtx.newXmlParser().parseResource(Patient.class, ""); - assertEquals("quot \"", p.getName().get(0).getFamily().get(0).getValue()); + /** + * #158 + */ + @Test + public void testEncodeEmptyTag() { + Patient p = new Patient(); + p.getMeta().addTag(); - } + String encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, not(containsString("tag"))); - @Test - public void testEncodeEscapedExtendedChars() { - Patient p = ourCtx.newXmlParser().parseResource(Patient.class, ""); - assertEquals("uuml ü", p.getName().get(0).getFamily().get(0).getValue()); - } + // With tag - @Test - public void testEncodeExtensionUndeclaredNonModifier() { - Observation obs = new Observation(); - obs.setId("1"); - obs.getMeta().addProfile("http://profile"); - Extension ext = obs.addExtension(); - ext.setUrl("http://exturl").setValue(new StringType("ext_url_value")); - - obs.getCode().setText("CODE"); - - IParser parser = ourCtx.newXmlParser(); - - String output = parser.setPrettyPrint(true).encodeResourceToString(obs); - ourLog.info(output); + p = new Patient(); + p.getMeta().addTag().setSystem("sys").setCode("code"); - //@formatter:off - assertThat(output, stringContainsInOrder( - "", - "", - "", - "", - "", - "" - )); - assertThat(output, not(stringContainsInOrder( - "" - ))); - //@formatter:on - - obs = parser.parseResource(Observation.class, output); - assertEquals(1, obs.getExtension().size()); - assertEquals("http://exturl", obs.getExtension().get(0).getUrl()); - assertEquals("ext_url_value", ((StringType)obs.getExtension().get(0).getValue()).getValue()); - } + encoded = ourCtx.newXmlParser().encodeResourceToString(p); + assertThat(encoded, (containsString("tag"))); + } - @Test + @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.getName().get(0).getFamily().get(0).getValue()); + + p = ourCtx.newXmlParser().parseResource(Patient.class, + ""); + assertEquals("quot \"", p.getName().get(0).getFamily().get(0).getValue()); + + } + + @Test + public void testEncodeEscapedExtendedChars() { + Patient p = ourCtx.newXmlParser().parseResource(Patient.class, + ""); + assertEquals("uuml ü", p.getName().get(0).getFamily().get(0).getValue()); + } + + @Test + public void testEncodeExtensionUndeclaredNonModifier() { + Observation obs = new Observation(); + obs.setId("1"); + obs.getMeta().addProfile("http://profile"); + Extension ext = obs.addExtension(); + ext.setUrl("http://exturl").setValue(new StringType("ext_url_value")); + + obs.getCode().setText("CODE"); + + IParser parser = ourCtx.newXmlParser(); + + String output = parser.setPrettyPrint(true).encodeResourceToString(obs); + ourLog.info(output); + + // @formatter:off + assertThat(output, stringContainsInOrder("", "", "", + "", "", "")); + assertThat(output, not(stringContainsInOrder(""))); + // @formatter:on + + obs = parser.parseResource(Observation.class, output); + assertEquals(1, obs.getExtension().size()); + assertEquals("http://exturl", obs.getExtension().get(0).getUrl()); + assertEquals("ext_url_value", ((StringType) obs.getExtension().get(0).getValue()).getValue()); + } + + @Test public void testEncodeExtensionUndeclaredNonModifierWithChildExtension() { Observation obs = new Observation(); obs.setId("1"); obs.getMeta().addProfile("http://profile"); Extension ext = obs.addExtension(); ext.setUrl("http://exturl"); - + Extension subExt = ext.addExtension(); subExt.setUrl("http://subext").setValue(new StringType("sub_ext_value")); - + obs.getCode().setText("CODE"); - + IParser parser = ourCtx.newXmlParser(); - + String output = parser.setPrettyPrint(true).encodeResourceToString(obs); ourLog.info(output); - //@formatter:off - assertThat(output, stringContainsInOrder( - "", - "", - "", - "", - "", - "", - "" - )); - assertThat(output, not(stringContainsInOrder( - "" - ))); - //@formatter:on - + // @formatter:off + assertThat(output, + stringContainsInOrder("", "", "", + "", "", + "", "")); + assertThat(output, not(stringContainsInOrder(""))); + // @formatter:on + obs = parser.parseResource(Observation.class, output); assertEquals(1, obs.getExtension().size()); assertEquals("http://exturl", obs.getExtension().get(0).getUrl()); assertEquals(1, obs.getExtension().get(0).getExtension().size()); assertEquals("http://subext", obs.getExtension().get(0).getExtension().get(0).getUrl()); - assertEquals("sub_ext_value", ((StringType)obs.getExtension().get(0).getExtension().get(0).getValue()).getValue()); + assertEquals("sub_ext_value", ((StringType) obs.getExtension().get(0).getExtension().get(0).getValue()).getValue()); } - @Test - public void testEncodeExtensionWithResourceContent() { - IParser parser = ourCtx.newXmlParser(); - - Patient patient = new Patient(); - patient.addAddress().setUse(AddressUse.HOME); - patient.addExtension().setUrl("urn:foo").setValue(new Reference().setReference("Organization/123")); - - String val = parser.encodeResourceToString(patient); - ourLog.info(val); - assertThat(val, StringContains.containsString("")); - - Patient actual = parser.parseResource(Patient.class, val); - assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); - List ext = actual.getExtension(); - assertEquals(1, ext.size()); - Reference ref = (Reference) ext.get(0).getValue(); - assertEquals("Organization/123", ref.getReferenceElement().getValue()); - - } - - @Test - public void testEncodeInvalidChildGoodException() { - Observation obs = new Observation(); - obs.setValue(new DecimalType(112.22)); - - IParser p = ourCtx.newJsonParser(); - - try { - p.encodeResourceToString(obs); - } catch (DataFormatException e) { - assertThat(e.getMessage(), StringContains.containsString("DecimalType")); - } - } - - @Test - @Ignore - public void testEncodeNarrativeBlockInBundle() throws Exception { - Patient p = new Patient(); - p.addIdentifier().setSystem("foo").setValue("bar"); - p.getText().setStatus(NarrativeStatus.GENERATED); - p.getText().setDivAsString("
hello
"); - - Bundle b = new Bundle(); - b.setTotal(123); - b.addEntry().setResource(p); - - String out = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(out); - assertThat(out, containsString("
hello
")); - - p.getText().setDivAsString("hello"); - out = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(out); - assertThat(out, containsString("hello")); - - } - - @Test - public void testEncodeNarrativeSuppressed() throws Exception { - Patient patient = new Patient(); - patient.setId("Patient/1/_history/1"); - patient.getText().setDivAsString("
THE DIV
"); - patient.addName().addFamily("FAMILY"); - patient.getMaritalStatus().addCoding().setCode("D"); - - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSuppressNarratives(true).encodeResourceToString(patient); - ourLog.info(encoded); - - assertThat(encoded, containsString("", "", "", "")); - assertThat(encoded, not(containsString("text"))); - assertThat(encoded, not(containsString("THE DIV"))); - assertThat(encoded, containsString("family")); - assertThat(encoded, containsString("maritalStatus")); - } - - @Test - public void testEncodeNonContained() { - // Create an organization - Organization org = new Organization(); - org.setId("Organization/65546"); - org.getNameElement().setValue("Contained Test Organization"); - - // Create a patient - Patient patient = new Patient(); - patient.setId("Patient/1333"); - patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); - patient.getManagingOrganization().setResource(org); - - // Create a list containing both resources. In a server method, you might just - // return this list, but here we will create a bundle to encode. - List resources = new ArrayList(); - resources.add(org); - resources.add(patient); - - // Create a bundle with both - Bundle b = new Bundle(); - b.addEntry().setResource(org); - b.addEntry().setResource(patient); - - // Encode the buntdle - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); - ourLog.info(encoded); - assertThat(encoded, not(containsString(""))); - assertThat(encoded, stringContainsInOrder("", "")); - assertThat(encoded, containsString("")); - assertThat(encoded, stringContainsInOrder("", "")); - - encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); - ourLog.info(encoded); - assertThat(encoded, not(containsString(""))); - assertThat(encoded, containsString("")); - - } - - @Test - public void testEncodePrettyPrint() throws Exception { - - Patient patient = new Patient(); - patient.getText().setDivAsString("
\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 testEncodeResourceRef() throws DataFormatException { - - Patient patient = new Patient(); - patient.setManagingOrganization(new Reference()); - - IParser p = ourCtx.newXmlParser(); - String str = p.encodeResourceToString(patient); - assertThat(str, IsNot.not(StringContains.containsString("managingOrganization"))); - - Reference ref = new Reference(); - 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 Reference(org)); - str = p.encodeResourceToString(patient); - assertThat(str, StringContains.containsString("THE DIV
"); - patient.addName().addFamily("FAMILY"); - patient.getMaritalStatus().addCoding().setCode("D"); - - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSummaryMode(true).encodeResourceToString(patient); - ourLog.info(encoded); - - assertThat(encoded, containsString("", "", "", "")); - assertThat(encoded, not(containsString("THE DIV"))); - assertThat(encoded, containsString("family")); - assertThat(encoded, not(containsString("maritalStatus"))); - } - - @Test - public void testEncodeSummary2() throws Exception { - Patient patient = new Patient(); - patient.setId("Patient/1/_history/1"); - patient.getText().setDivAsString("
THE DIV
"); - patient.addName().addFamily("FAMILY"); - patient.setMaritalStatus(new CodeableConcept().setText("D")); - - patient.getMeta().addTag().setSystem("foo").setCode("bar"); - - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSummaryMode(true).encodeResourceToString(patient); - ourLog.info(encoded); - - assertThat(encoded, containsString("", "", "", "")); - assertThat(encoded, stringContainsInOrder("", "", "", "")); - assertThat(encoded, not(containsString("THE DIV"))); - assertThat(encoded, containsString("family")); - assertThat(encoded, not(containsString("maritalStatus"))); - } - - @Test - public void testEncodeUndeclaredExtensionWithAddressContent() { - IParser parser = ourCtx.newXmlParser(); - - Patient patient = new Patient(); - patient.addAddress().setUse(AddressUse.HOME); - patient.addExtension().setUrl("urn:foo").setValue(new Address().addLine("line1")); - - String val = parser.encodeResourceToString(patient); - ourLog.info(val); - assertThat(val, StringContains.containsString("")); - - MyPatientWithOneDeclaredAddressExtension actual = parser.parseResource(MyPatientWithOneDeclaredAddressExtension.class, val); - assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); - Address ref = actual.getFoo(); - assertEquals("line1", ref.getLine().get(0).getValue()); - - } - - @Test - public void testEncodeUndeclaredExtensionWithEnumerationContent() { - IParser parser = ourCtx.newXmlParser(); - - Patient patient = new Patient(); - patient.addAddress().setUse(AddressUse.HOME); - EnumFactory fact = new AddressUseEnumFactory(); - PrimitiveType enumeration = new Enumeration(fact).setValue(AddressUse.HOME); - patient.addExtension().setUrl("urn:foo").setValue(enumeration); - - String val = parser.encodeResourceToString(patient); - ourLog.info(val); - assertThat(val, StringContains.containsString("")); - - MyPatientWithOneDeclaredEnumerationExtension actual = parser.parseResource(MyPatientWithOneDeclaredEnumerationExtension.class, val); - assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); - Enumeration ref = actual.getFoo(); - assertEquals("home", ref.getValue().toCode()); - - } - - @Test - public void testEncodingNullExtension() { - Patient p = new Patient(); - Extension extension = new Extension().setUrl("http://foo#bar"); - p.getExtension().add(extension); - String str = ourCtx.newXmlParser().encodeResourceToString(p); - - assertEquals("", str); - - extension.setValue(new StringType()); - - str = ourCtx.newXmlParser().encodeResourceToString(p); - assertEquals("", str); - - extension.setValue(new StringType("")); - - str = ourCtx.newXmlParser().encodeResourceToString(p); - assertEquals("", str); - - } - - - @Test - public void testExtensionOnComposite() throws Exception { - - Patient patient = new Patient(); - - HumanName name = patient.addName(); - name.addFamily("Shmoe"); - HumanName given = name.addGiven("Joe"); - Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("Hello")); - given.getExtension().add(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.getName().get(0).getExtension().size()); - Extension ext = parsed.getName().get(0).getExtension().get(0); - assertEquals("Hello", ((IPrimitiveType) ext.getValue()).getValue()); - - } - - @Test - public void testExtensionOnPrimitive() throws Exception { - - Patient patient = new Patient(); - - HumanName name = patient.addName(); - StringType family = name.addFamilyElement(); - family.setValue("Shmoe"); - - Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("Hello")); - family.getExtension().add(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.getName().get(0).getFamily().get(0).getExtension().size()); - Extension ext = parsed.getName().get(0).getFamily().get(0).getExtension().get(0); - assertEquals("Hello", ((IPrimitiveType) ext.getValue()).getValue()); - - } - - @Test - public void testExtensions() throws DataFormatException { - - MyPatientHl7Org patient = new MyPatientHl7Org(); - patient.setPetName(new StringType("Fido")); - patient.getImportantDates().add(new DateTimeType("2010-01-02")); - patient.getImportantDates().add(new DateTimeType("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 Exception { - - //@formatter:off - String msg = "" - + "
John Cardinal: 444333333
" - + "" - + "" - + "" - + "" - + "" - + "" - + "
" - + "
"; - //@formatter:on - - Patient patient = ourCtx.newXmlParser().parseResource(Patient.class, msg); - - assertEquals(NarrativeStatus.GENERATED, patient.getText().getStatus()); - assertThat(patient.getText().getDiv().getValueAsString(), containsString(">John Cardinal: 444333333 <")); - assertEquals("PRP1660", patient.getIdentifier().get(0).getValue()); - - String encoded = ourCtx.newXmlParser().encodeResourceToString(patient); - - ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient)); - - Diff d = new Diff(new StringReader(msg), new StringReader(encoded)); - - ourLog.info("Expected: {}", msg); - ourLog.info("Actual: {}", encoded); - - assertTrue(d.toString(), d.identical()); - - } - - @Test - public void testLoadAndEncodeDeclaredExtensions() throws ConfigurationException, DataFormatException, SAXException, IOException { - IParser p = ourCtx.newXmlParser(); - ourCtx.setAddProfileTagWhenEncoding(AddProfileTagEnum.NEVER); - - //@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(ResourceWithExtensionsA.class, msg); - assertEquals("IdentifierLabel", resource.getIdentifier().get(0).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.setPrettyPrint(true).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).getValue()); - assertEquals("Foo1Value", ((IPrimitiveType) resource.getExtension().get(0).getValue()).getValueAsString()); - assertEquals("Foo1Value2", ((IPrimitiveType) resource.getExtension().get(1).getValue()).getValueAsString()); - assertEquals("Foo2Value1", ((IPrimitiveType) resource.getModifierExtension().get(0).getValue()).getValueAsString()); - - assertEquals("2013-01-01", ((IPrimitiveType) resource.getExtension().get(2).getExtension().get(0).getValue()).getValueAsString()); - assertEquals("2013-01-02", ((IPrimitiveType) resource.getExtension().get(2).getExtension().get(1).getExtension().get(0).getValue()).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 testMoreExtensions() throws Exception { - - Patient patient = new Patient(); - patient.addIdentifier().setUse(IdentifierUse.OFFICIAL).setSystem("urn:example").setValue("7000135"); - - Extension ext = new Extension(); - ext.setUrl("http://example.com/extensions#someext"); - ext.setValue(new DateTimeType("2011-01-02T11:13:15")); - - // Add the extension to the resource - patient.getExtension().add(ext); - // END SNIPPET: resourceExtension - - // START SNIPPET: resourceStringExtension - HumanName name = patient.addName(); - name.addFamily("Shmoe"); - StringType given = name.addGivenElement(); - given.setValue("Joe"); - Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("given")); - given.getExtension().add(ext2); - - StringType given2 = name.addGivenElement(); - given2.setValue("Shmoe"); - Extension given2ext = new Extension().setUrl("http://examples.com#givenext_parent"); - given2.getExtension().add(given2ext); - given2ext.addExtension().setUrl("http://examples.com#givenext_child").setValue(new StringType("CHILD")); - // END SNIPPET: resourceStringExtension - - // START SNIPPET: subExtension - Extension parent = new Extension().setUrl("http://example.com#parent"); - patient.getExtension().add(parent); - - Extension child1 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); - parent.getExtension().add(child1); - - Extension child2 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); - parent.getExtension().add(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("")); - assertThat(enc, containsString("")); - } - - // Narrative generation not currently supported for HL7org structures - public void testNarrativeGeneration() throws DataFormatException, IOException { - - Patient patient = new Patient(); - patient.addName().addFamily("Smith"); - Organization org = new Organization(); - patient.getManagingOrganization().setResource(org); + /** + * See #327 + */ + @Test + public void testEncodeExtensionWithContainedResource() { + + TestPatientFor327 patient = new TestPatientFor327(); + patient.setBirthDate(new Date()); + + List conditions = new ArrayList(); + Condition condition = new Condition(); + condition.addBodySite().setText("BODY SITE"); + conditions.add(new Reference(condition)); + patient.setCondition(conditions); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + //@formatter:off + assertThat(encoded, stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + )); + //@formatter:on + } + + @Test + public void testEncodeExtensionWithResourceContent() { + IParser parser = ourCtx.newXmlParser(); + + Patient patient = new Patient(); + patient.addAddress().setUse(AddressUse.HOME); + patient.addExtension().setUrl("urn:foo").setValue(new Reference().setReference("Organization/123")); + + String val = parser.encodeResourceToString(patient); + ourLog.info(val); + assertThat(val, StringContains.containsString( + "")); + + Patient actual = parser.parseResource(Patient.class, val); + assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); + List ext = actual.getExtension(); + assertEquals(1, ext.size()); + Reference ref = (Reference) ext.get(0).getValue(); + assertEquals("Organization/123", ref.getReferenceElement().getValue()); + + } + + @Test + public void testEncodeInvalidChildGoodException() { + Observation obs = new Observation(); + obs.setValue(new DecimalType(112.22)); + + IParser p = ourCtx.newJsonParser(); + + try { + p.encodeResourceToString(obs); + } catch (DataFormatException e) { + assertThat(e.getMessage(), StringContains.containsString("DecimalType")); + } + } + + @Test + @Ignore + public void testEncodeNarrativeBlockInBundle() throws Exception { + Patient p = new Patient(); + p.addIdentifier().setSystem("foo").setValue("bar"); + p.getText().setStatus(NarrativeStatus.GENERATED); + p.getText().setDivAsString("
hello
"); + + Bundle b = new Bundle(); + b.setTotal(123); + b.addEntry().setResource(p); + + String out = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); + ourLog.info(out); + assertThat(out, containsString("
hello
")); + + p.getText().setDivAsString("hello"); + out = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); + ourLog.info(out); + assertThat(out, containsString("hello")); + + } + + @Test + public void testEncodeNarrativeSuppressed() throws Exception { + Patient patient = new Patient(); + patient.setId("Patient/1/_history/1"); + patient.getText().setDivAsString("
THE DIV
"); + patient.addName().addFamily("FAMILY"); + patient.getMaritalStatus().addCoding().setCode("D"); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSuppressNarratives(true) + .encodeResourceToString(patient); + ourLog.info(encoded); + + assertThat(encoded, containsString("", "", + "", "")); + assertThat(encoded, not(containsString("text"))); + assertThat(encoded, not(containsString("THE DIV"))); + assertThat(encoded, containsString("family")); + assertThat(encoded, containsString("maritalStatus")); + } + + @Test + public void testEncodeNonContained() { + // Create an organization + Organization org = new Organization(); + org.setId("Organization/65546"); + org.getNameElement().setValue("Contained Test Organization"); + + // Create a patient + Patient patient = new Patient(); + patient.setId("Patient/1333"); + patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); + patient.getManagingOrganization().setResource(org); + + // Create a list containing both resources. In a server method, you might + // just + // return this list, but here we will create a bundle to encode. + List resources = new ArrayList(); + resources.add(org); + resources.add(patient); + + // Create a bundle with both + Bundle b = new Bundle(); + b.addEntry().setResource(org); + b.addEntry().setResource(patient); + + // Encode the buntdle + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); + ourLog.info(encoded); + assertThat(encoded, not(containsString(""))); + assertThat(encoded, stringContainsInOrder("", "")); + assertThat(encoded, containsString("")); + assertThat(encoded, stringContainsInOrder("", "
")); + + encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + assertThat(encoded, not(containsString(""))); + assertThat(encoded, containsString("")); + + } + + @Test + public void testEncodePrettyPrint() throws Exception { + + Patient patient = new Patient(); + patient.getText().setDivAsString("
\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 testEncodeResourceRef() throws DataFormatException { + + Patient patient = new Patient(); + patient.setManagingOrganization(new Reference()); + + IParser p = ourCtx.newXmlParser(); + String str = p.encodeResourceToString(patient); + assertThat(str, IsNot.not(StringContains.containsString("managingOrganization"))); + + Reference ref = new Reference(); + 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 Reference(org)); + str = p.encodeResourceToString(patient); + assertThat(str, StringContains.containsString("THE DIV
"); + patient.addName().addFamily("FAMILY"); + patient.getMaritalStatus().addCoding().setCode("D"); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSummaryMode(true).encodeResourceToString(patient); + ourLog.info(encoded); + + assertThat(encoded, containsString("", "", + "", "")); + assertThat(encoded, not(containsString("THE DIV"))); + assertThat(encoded, containsString("family")); + assertThat(encoded, not(containsString("maritalStatus"))); + } + + @Test + public void testEncodeSummary2() throws Exception { + Patient patient = new Patient(); + patient.setId("Patient/1/_history/1"); + patient.getText().setDivAsString("
THE DIV
"); + patient.addName().addFamily("FAMILY"); + patient.setMaritalStatus(new CodeableConcept().setText("D")); + + patient.getMeta().addTag().setSystem("foo").setCode("bar"); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).setSummaryMode(true).encodeResourceToString(patient); + ourLog.info(encoded); + + assertThat(encoded, containsString("", "", "", "")); + assertThat(encoded, stringContainsInOrder("", "", + "", "")); + assertThat(encoded, not(containsString("THE DIV"))); + assertThat(encoded, containsString("family")); + assertThat(encoded, not(containsString("maritalStatus"))); + } + + @Test + public void testEncodeUndeclaredExtensionWithAddressContent() { + IParser parser = ourCtx.newXmlParser(); + + Patient patient = new Patient(); + patient.addAddress().setUse(AddressUse.HOME); + patient.addExtension().setUrl("urn:foo").setValue(new Address().addLine("line1")); + + String val = parser.encodeResourceToString(patient); + ourLog.info(val); + assertThat(val, StringContains + .containsString("")); + + MyPatientWithOneDeclaredAddressExtension actual = parser + .parseResource(MyPatientWithOneDeclaredAddressExtension.class, val); + assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); + Address ref = actual.getFoo(); + assertEquals("line1", ref.getLine().get(0).getValue()); + + } + + @Test + public void testEncodeUndeclaredExtensionWithEnumerationContent() { + IParser parser = ourCtx.newXmlParser(); + + Patient patient = new Patient(); + patient.addAddress().setUse(AddressUse.HOME); + EnumFactory fact = new AddressUseEnumFactory(); + PrimitiveType enumeration = new Enumeration(fact).setValue(AddressUse.HOME); + patient.addExtension().setUrl("urn:foo").setValue(enumeration); + + String val = parser.encodeResourceToString(patient); + ourLog.info(val); + assertThat(val, + StringContains.containsString("")); + + MyPatientWithOneDeclaredEnumerationExtension actual = parser + .parseResource(MyPatientWithOneDeclaredEnumerationExtension.class, val); + assertEquals(AddressUse.HOME, patient.getAddress().get(0).getUse()); + Enumeration ref = actual.getFoo(); + assertEquals("home", ref.getValue().toCode()); + + } + + @Test + public void testEncodingNullExtension() { + Patient p = new Patient(); + Extension extension = new Extension().setUrl("http://foo#bar"); + p.getExtension().add(extension); + String str = ourCtx.newXmlParser().encodeResourceToString(p); + + assertEquals("", str); + + extension.setValue(new StringType()); + + str = ourCtx.newXmlParser().encodeResourceToString(p); + assertEquals("", str); + + extension.setValue(new StringType("")); + + str = ourCtx.newXmlParser().encodeResourceToString(p); + assertEquals("", str); + + } + + @Test + public void testExtensionOnComposite() throws Exception { + + Patient patient = new Patient(); + + HumanName name = patient.addName(); + name.addFamily("Shmoe"); + HumanName given = name.addGiven("Joe"); + Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("Hello")); + given.getExtension().add(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.getName().get(0).getExtension().size()); + Extension ext = parsed.getName().get(0).getExtension().get(0); + assertEquals("Hello", ((IPrimitiveType) ext.getValue()).getValue()); + + } + + @Test + public void testExtensionOnPrimitive() throws Exception { + + Patient patient = new Patient(); + + HumanName name = patient.addName(); + StringType family = name.addFamilyElement(); + family.setValue("Shmoe"); + + Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("Hello")); + family.getExtension().add(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.getName().get(0).getFamily().get(0).getExtension().size()); + Extension ext = parsed.getName().get(0).getFamily().get(0).getExtension().get(0); + assertEquals("Hello", ((IPrimitiveType) ext.getValue()).getValue()); + + } + + @Test + public void testExtensions() throws DataFormatException { + + MyPatientHl7Org patient = new MyPatientHl7Org(); + patient.setPetName(new StringType("Fido")); + patient.getImportantDates().add(new DateTimeType("2010-01-02")); + patient.getImportantDates().add(new DateTimeType("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 Exception { + + // @formatter:off + String msg = "" + + "
John Cardinal: 444333333
" + + "" + + "" + + "" + + "" + + "" + + "" + "
" + + "
"; + // @formatter:on + + Patient patient = ourCtx.newXmlParser().parseResource(Patient.class, msg); + + assertEquals(NarrativeStatus.GENERATED, patient.getText().getStatus()); + assertThat(patient.getText().getDiv().getValueAsString(), containsString(">John Cardinal: 444333333 <")); + assertEquals("PRP1660", patient.getIdentifier().get(0).getValue()); + + String encoded = ourCtx.newXmlParser().encodeResourceToString(patient); + + ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient)); + + Diff d = new Diff(new StringReader(msg), new StringReader(encoded)); + + ourLog.info("Expected: {}", msg); + ourLog.info("Actual: {}", encoded); + + assertTrue(d.toString(), d.identical()); + + } + + @Test + public void testLoadAndEncodeDeclaredExtensions() + throws ConfigurationException, DataFormatException, SAXException, IOException { + IParser p = ourCtx.newXmlParser(); + ourCtx.setAddProfileTagWhenEncoding(AddProfileTagEnum.NEVER); + + // @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(ResourceWithExtensionsA.class, msg); + assertEquals("IdentifierLabel", resource.getIdentifier().get(0).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.setPrettyPrint(true).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).getValue()); + assertEquals("Foo1Value", ((IPrimitiveType) resource.getExtension().get(0).getValue()).getValueAsString()); + assertEquals("Foo1Value2", ((IPrimitiveType) resource.getExtension().get(1).getValue()).getValueAsString()); + assertEquals("Foo2Value1", + ((IPrimitiveType) resource.getModifierExtension().get(0).getValue()).getValueAsString()); + + assertEquals("2013-01-01", + ((IPrimitiveType) resource.getExtension().get(2).getExtension().get(0).getValue()).getValueAsString()); + assertEquals("2013-01-02", + ((IPrimitiveType) resource.getExtension().get(2).getExtension().get(1).getExtension().get(0).getValue()) + .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 testMoreExtensions() throws Exception { + + Patient patient = new Patient(); + patient.addIdentifier().setUse(IdentifierUse.OFFICIAL).setSystem("urn:example").setValue("7000135"); + + Extension ext = new Extension(); + ext.setUrl("http://example.com/extensions#someext"); + ext.setValue(new DateTimeType("2011-01-02T11:13:15")); + + // Add the extension to the resource + patient.getExtension().add(ext); + // END SNIPPET: resourceExtension + + // START SNIPPET: resourceStringExtension + HumanName name = patient.addName(); + name.addFamily("Shmoe"); + StringType given = name.addGivenElement(); + given.setValue("Joe"); + Extension ext2 = new Extension().setUrl("http://examples.com#givenext").setValue(new StringType("given")); + given.getExtension().add(ext2); + + StringType given2 = name.addGivenElement(); + given2.setValue("Shmoe"); + Extension given2ext = new Extension().setUrl("http://examples.com#givenext_parent"); + given2.getExtension().add(given2ext); + given2ext.addExtension().setUrl("http://examples.com#givenext_child").setValue(new StringType("CHILD")); + // END SNIPPET: resourceStringExtension + + // START SNIPPET: subExtension + Extension parent = new Extension().setUrl("http://example.com#parent"); + patient.getExtension().add(parent); + + Extension child1 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); + parent.getExtension().add(child1); + + Extension child2 = new Extension().setUrl("http://example.com#child").setValue(new StringType("value1")); + parent.getExtension().add(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( + "")); + assertThat(enc, containsString( + "")); + } + + // Narrative generation not currently supported for HL7org structures + public void testNarrativeGeneration() throws DataFormatException, IOException { + + Patient patient = new Patient(); + patient.addName().addFamily("Smith"); + Organization org = new Organization(); + patient.getManagingOrganization().setResource(org); INarrativeGenerator gen = new INarrativeGenerator() { @@ -1480,255 +1506,271 @@ public class XmlParserHl7OrgDstu2Test { }; - FhirContext context = ourCtx; - context.setNarrativeGenerator(gen); - IParser p = context.newXmlParser(); - String str = p.encodeResourceToString(patient); + FhirContext context = ourCtx; + context.setNarrativeGenerator(gen); + IParser p = context.newXmlParser(); + String str = p.encodeResourceToString(patient); - ourLog.info(str); + ourLog.info(str); - assertThat(str, StringContains.containsString(",\"text\":{\"status\":\"generated\",\"div\":\"
help
\"},")); - } + assertThat(str, StringContains.containsString(",\"text\":{\"status\":\"generated\",\"div\":\"
help
\"},")); + } - + @Test + public void testNestedContainedResources() { - + Observation A = new Observation(); + A.getCode().setText("A"); - @Test - public void testNestedContainedResources() { + Observation B = new Observation(); + B.getCode().setText("B"); + A.addRelated().setTarget(new Reference(B)); - Observation A = new Observation(); - A.getCode().setText("A"); + Observation C = new Observation(); + C.getCode().setText("C"); + B.addRelated().setTarget(new Reference(C)); - Observation B = new Observation(); - B.getCode().setText("B"); - A.addRelated().setTarget(new Reference(B)); + String str = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(A); + ourLog.info(str); - Observation C = new Observation(); - C.getCode().setText("C"); - B.addRelated().setTarget(new Reference(C)); + assertThat(str, + stringContainsInOrder(Arrays.asList("", "", ""))); + assertThat(str, stringContainsInOrder(Arrays.asList("", "", "", ""))); - String str = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(A); - ourLog.info(str); + Observation obs = ourCtx.newXmlParser().parseResource(Observation.class, str); + assertEquals("A", obs.getCode().getText()); - assertThat(str, stringContainsInOrder(Arrays.asList("", "", ""))); - assertThat(str, stringContainsInOrder(Arrays.asList("", "", "", ""))); + Observation obsB = (Observation) obs.getRelated().get(0).getTarget().getResource(); + assertEquals("B", obsB.getCode().getText()); - Observation obs = ourCtx.newXmlParser().parseResource(Observation.class, str); - assertEquals("A", obs.getCode().getText()); + Observation obsC = (Observation) obsB.getRelated().get(0).getTarget().getResource(); + assertEquals("C", obsC.getCode().getText()); - Observation obsB = (Observation) obs.getRelated().get(0).getTarget().getResource(); - assertEquals("B", obsB.getCode().getText()); + } - Observation obsC = (Observation) obsB.getRelated().get(0).getTarget().getResource(); - assertEquals("C", obsC.getCode().getText()); + @Test + public void testParseBinaryResource() { - } + Binary val = ourCtx.newXmlParser().parseResource(Binary.class, + ""); + assertEquals("foo", val.getContentType()); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, val.getContent()); - @Test - public void testParseBinaryResource() { + } - Binary val = ourCtx.newXmlParser().parseResource(Binary.class, ""); - assertEquals("foo", val.getContentType()); - assertArrayEquals(new byte[] { 1, 2, 3, 4 }, val.getContent()); + /** + * Thanks to Alexander Kley! + */ + @Test + public void testParseContainedBinaryResource() throws Exception { + byte[] bin = new byte[] { 0, 1, 2, 3, 4 }; + final Binary binary = new Binary(); + binary.setContentType("PatientConsent").setContent(bin); + // binary.setId(UUID.randomUUID().toString()); - } + DocumentManifest manifest = new DocumentManifest(); + // manifest.setId(UUID.randomUUID().toString()); + CodeableConcept cc = new CodeableConcept(); + cc.addCoding().setSystem("mySystem").setCode("PatientDocument"); + manifest.setType(cc); + manifest.setMasterIdentifier(new Identifier().setSystem("mySystem").setValue(UUID.randomUUID().toString())); + manifest.addContent().setP(new Reference(binary)); + manifest.setStatus(org.hl7.fhir.instance.model.Enumerations.DocumentReferenceStatus.CURRENT); + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(manifest); + ourLog.info(encoded); + assertThat(encoded, + StringContainsInOrder.stringContainsInOrder(Arrays.asList("contained>", ""))); - /** - * Thanks to Alexander Kley! - */ - @Test - public void testParseContainedBinaryResource() throws Exception { - byte[] bin = new byte[] { 0, 1, 2, 3, 4 }; - final Binary binary = new Binary(); - binary.setContentType("PatientConsent").setContent(bin); - // binary.setId(UUID.randomUUID().toString()); + DocumentManifest actual = ourCtx.newXmlParser().parseResource(DocumentManifest.class, encoded); + assertEquals(1, actual.getContained().size()); + assertEquals(1, actual.getContent().size()); - DocumentManifest manifest = new DocumentManifest(); - // manifest.setId(UUID.randomUUID().toString()); - CodeableConcept cc = new CodeableConcept(); - cc.addCoding().setSystem("mySystem").setCode("PatientDocument"); - manifest.setType(cc); - manifest.setMasterIdentifier(new Identifier().setSystem("mySystem").setValue(UUID.randomUUID().toString())); - manifest.addContent().setP(new Reference(binary)); - manifest.setStatus(org.hl7.fhir.instance.model.Enumerations.DocumentReferenceStatus.CURRENT); + /* + * If this fails, the child named "p" in DocumentManifest is missing the + * type IBaseResource in its definition... This isn't being auto added right + * now, need to figure out why + * + * @Child(name = "p", type = {Attachment.class, IBaseResource.class}, + * order=1, min=1, max=1, modifier=false, summary=true) + */ + assertNotNull(actual.getContent().get(0).getPReference().getResource()); - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(manifest); - ourLog.info(encoded); - assertThat(encoded, StringContainsInOrder.stringContainsInOrder(Arrays.asList("contained>", ""))); + } - DocumentManifest actual = ourCtx.newXmlParser().parseResource(DocumentManifest.class, encoded); - assertEquals(1, actual.getContained().size()); - assertEquals(1, actual.getContent().size()); - - /* - * If this fails, the child named "p" in DocumentManifest is missing the - * type IBaseResource in its definition... This isn't being auto added right now, - * need to figure out why - * - * @Child(name = "p", type = {Attachment.class, IBaseResource.class}, order=1, min=1, max=1, modifier=false, summary=true) - */ - assertNotNull(actual.getContent().get(0).getPReference().getResource()); + @Test + public void testParseEncodeNarrative() { - } + String input = "
Donald null DUCK
Identifier7000135
Address10 Duxon Street
VICTORIA BC Can
Date of birth01 June 1980
"; + IBaseResource res = ourCtx.newXmlParser().parseResource(input); - @Test - public void testParseEncodeNarrative() { + String output = ourCtx.newXmlParser().encodeResourceToString(res); - String input = "
Donald null DUCK
Identifier7000135
Address10 Duxon Street
VICTORIA BC Can
Date of birth01 June 1980
"; - IBaseResource res = ourCtx.newXmlParser().parseResource(input); + // 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")))); - String output = ourCtx.newXmlParser().encodeResourceToString(res); + 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")))); + // 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")))); + @Test + public void testParseLanguage() { + String input = "
海生
IdentifierURNo
Address99 Houston Road
BENTLEIGH Victoria
Date of birth01 January 1997
"; + Patient pt = ourCtx.newXmlParser().parseResource(Patient.class, input); - } + assertEquals("zh-CN", pt.getLanguage()); + } - @Test - public void testParseLanguage() { - String input = "
海生
IdentifierURNo
Address99 Houston Road
BENTLEIGH Victoria
Date of birth01 January 1997
"; - Patient pt = ourCtx.newXmlParser().parseResource(Patient.class, input); + @Test + @Ignore + public void testParseNarrative() throws Exception { + // @formatter:off + String htmlNoNs = "
AAABBBCCC
"; + String htmlNs = fixDivNodeText(htmlNoNs); + String res = "\n" + " \n" + " \n" + " " + + htmlNs + "\n" + " \n" + ""; + // @formatter:on - assertEquals("zh-CN", pt.getLanguage()); - } + Patient p = ourCtx.newXmlParser().parseResource(Patient.class, res); + assertEquals(htmlNs, p.getText().getDivAsString()); + } - @Test - @Ignore - public void testParseNarrative() throws Exception { - //@formatter:off - String htmlNoNs = "
AAABBBCCC
"; - String htmlNs = fixDivNodeText(htmlNoNs); - String res= "\n" + - " \n" + - " \n" + - " " + htmlNs + "\n" + - " \n" + - ""; - //@formatter:on - - Patient p = ourCtx.newXmlParser().parseResource(Patient.class, res); - assertEquals(htmlNs, p.getText().getDivAsString()); - } + @Test + public void testParseWithXmlHeader() throws ConfigurationException, DataFormatException { + IParser p = ourCtx.newXmlParser(); + // @formatter:off + String msg = "" + "\n" + + " \n" + " \n" + " \n" + ""; + // @formatter:on - @Test - public void testParseWithXmlHeader() throws ConfigurationException, DataFormatException { - IParser p = ourCtx.newXmlParser(); + Patient resource = (Patient) p.parseResource(msg); + assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getValue()); + } - //@formatter:off - String msg = "" + - "\n" + - " \n" + - " \n" + - " \n" + - ""; - //@formatter:on + @Test + public void testReEncode() throws SAXException, IOException { - Patient resource = (Patient) p.parseResource(msg); - assertEquals("IdentifierLabel", resource.getIdentifier().get(0).getValue()); - } + // @formatter:off + String msg = "" + + "" + + ""; + // @formatter:on + Patient patient1 = ourCtx.newXmlParser().parseResource(Patient.class, msg); + String encoded1 = ourCtx.newXmlParser().encodeResourceToString(patient1); - @Test - public void testReEncode() throws SAXException, IOException { + ourLog.info("Expected: {}", msg); + ourLog.info("Actual: {}", encoded1); - //@formatter:off - String msg = "" - + "" - + ""; - //@formatter:on + Diff d = new Diff(new StringReader(msg), new StringReader(encoded1)); + assertTrue(d.toString(), d.identical()); - Patient patient1 = ourCtx.newXmlParser().parseResource(Patient.class, msg); - String encoded1 = ourCtx.newXmlParser().encodeResourceToString(patient1); + } - ourLog.info("Expected: {}", msg); - ourLog.info("Actual: {}", encoded1); - - Diff d = new Diff(new StringReader(msg), new StringReader(encoded1)); - assertTrue(d.toString(), d.identical()); + @Test + public void testSimpleResourceEncode() throws IOException, SAXException { - } + String xmlString = IOUtils.toString( + XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), + Charset.forName("UTF-8")); + Patient obs = ourCtx.newJsonParser().parseResource(Patient.class, xmlString); - @Test - public void testSimpleResourceEncode() throws IOException, SAXException { + List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getExtension(); + Extension undeclaredExtension = undeclaredExtensions.get(0); + assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl()); - String xmlString = IOUtils.toString(XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), Charset.forName("UTF-8")); - Patient obs = ourCtx.newJsonParser().parseResource(Patient.class, xmlString); + ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out)); - List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getExtension(); - Extension undeclaredExtension = undeclaredExtensions.get(0); - assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl()); + IParser jsonParser = ourCtx.newXmlParser(); + String encoded = jsonParser.encodeResourceToString(obs); + ourLog.info(encoded); - ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToWriter(obs, new OutputStreamWriter(System.out)); + String jsonString = IOUtils.toString( + XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), + Charset.forName("UTF-8")); - IParser jsonParser = ourCtx.newXmlParser(); - String encoded = jsonParser.encodeResourceToString(obs); - ourLog.info(encoded); + String expected = (jsonString); + String actual = (encoded.trim()); - String jsonString = IOUtils.toString(XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), Charset.forName("UTF-8")); + Diff d = new Diff(new StringReader(expected), new StringReader(actual)); + assertTrue(d.toString(), d.identical()); - 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 { - } + String xmlString = IOUtils.toString( + XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), + Charset.forName("UTF-8")); + MyObservationWithExtensions obs = ourCtx.newXmlParser().parseResource(MyObservationWithExtensions.class, xmlString); - @Test - public void testSimpleResourceEncodeWithCustomType() throws IOException { + assertEquals(0, obs.getExtension().size()); + assertEquals("aaaa", obs.getExtAtt().getContentType()); + assertEquals("str1", obs.getMoreExt().getStr1().getValue()); + assertEquals("2011-01-02", obs.getModExt().getValueAsString()); - String xmlString = IOUtils.toString(XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), Charset.forName("UTF-8")); - MyObservationWithExtensions obs = ourCtx.newXmlParser().parseResource(MyObservationWithExtensions.class, xmlString); + List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getExtension(); + Extension undeclaredExtension = undeclaredExtensions.get(0); + assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl()); - assertEquals(0, obs.getExtension().size()); - assertEquals("aaaa", obs.getExtAtt().getContentType()); - assertEquals("str1", obs.getMoreExt().getStr1().getValue()); - assertEquals("2011-01-02", obs.getModExt().getValueAsString()); + IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); + String encoded = jsonParser.encodeResourceToString(obs); + ourLog.info(encoded); - List undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getExtension(); - Extension undeclaredExtension = undeclaredExtensions.get(0); - assertEquals("http://hl7.org/fhir/Profile/iso-21090#qualifier", undeclaredExtension.getUrl()); + String jsonString = IOUtils.toString( + XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), + Charset.forName("UTF-8")); - IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); - String encoded = jsonParser.encodeResourceToString(obs); - ourLog.info(encoded); + JSON expected = JSONSerializer.toJSON(jsonString); + JSON actual = JSONSerializer.toJSON(encoded.trim()); - String jsonString = IOUtils.toString(XmlParserHl7OrgDstu2Test.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), Charset.forName("UTF-8")); + // The encoded escapes quote marks using XML escaping instead of JSON + // escaping, which is probably nicer anyhow... + String exp = fixDivNodeTextJson(expected.toString().replace("\\\"Jim\\\"", ""Jim"")); + String act = fixDivNodeTextJson(actual.toString()); - JSON expected = JSONSerializer.toJSON(jsonString); - JSON actual = JSONSerializer.toJSON(encoded.trim()); + ourLog.info("Expected: {}", exp); + ourLog.info("Actual : {}", act); + assertEquals(exp, act); - // The encoded escapes quote marks using XML escaping instead of JSON escaping, which is probably nicer anyhow... - String exp = fixDivNodeTextJson(expected.toString().replace("\\\"Jim\\\"", ""Jim"")); - String act = fixDivNodeTextJson(actual.toString()); + } - ourLog.info("Expected: {}", exp); - ourLog.info("Actual : {}", act); - assertEquals(exp, act); + @BeforeClass + public static void beforeClass() { + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreComments(true); + XMLUnit.setIgnoreWhitespace(true); + ourCtx = FhirContext.forDstu2Hl7Org(); + } - } + @BeforeClass + public static void beforeClass2() { + System.setProperty("file.encoding", "ISO-8859-1"); + } - @BeforeClass - public static void beforeClass() { - XMLUnit.setIgnoreAttributeOrder(true); - XMLUnit.setIgnoreComments(true); - XMLUnit.setIgnoreWhitespace(true); - ourCtx = FhirContext.forDstu2Hl7Org(); - } + @ResourceDef(name = "Patient") + public static class TestPatientFor327 extends Patient { - @BeforeClass - public static void beforeClass2() { - System.setProperty("file.encoding", "ISO-8859-1"); - } + private static final long serialVersionUID = 1L; + + @Child(name = "testCondition") + @ca.uhn.fhir.model.api.annotation.Extension(url = "testCondition", definedLocally = true, isModifier = false) + private List testConditions = null; + + public List getConditions() { + return this.testConditions; + } + + public void setCondition(List ref) { + this.testConditions = ref; + } + } }