Correct encoding order for DSTU2 resources

This commit is contained in:
jamesagnew 2016-04-14 07:53:37 -04:00
parent 81dec23faf
commit 72640dc720
6 changed files with 2011 additions and 1661 deletions
hapi-fhir-base/src/main/java/ca/uhn/fhir/context
hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir
hapi-fhir-structures-dstu3/src/test/java
ca/uhn/fhir/parser
org/hl7/fhir/dstu3/hapi/validation
hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser

View File

@ -101,6 +101,34 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> ext
List<BaseRuntimeChildDefinition> children = new ArrayList<BaseRuntimeChildDefinition>();
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());
}

View File

@ -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<ResourceReferenceDt> testConditions = null;
public void setCondition(List<ResourceReferenceDt> ref)
{
this.testConditions = ref;
}
public List<ResourceReferenceDt> getConditions()
{
return this.testConditions;
}
}
/**
* See #327
*/
@Test
public void testEncodeExtensionWithContainedResource() {
TestPatientFor327 patient = new TestPatientFor327();
patient.setBirthDate(new Date(), TemporalPrecisionEnum.DAY);
List<ResourceReferenceDt> conditions = new ArrayList<ResourceReferenceDt>();
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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<extension url=\"testCondition\">",
"<valueReference>",
"<reference value=\"#1\"/>",
"</valueReference>",
"</extension>",
"<contained>",
"<Condition xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<bodySite>",
"<text value=\"BODY SITE\"/>",
"</bodySite>",
"</Condition>",
"</contained>",
"<birthDate value=\"2016-04-14\"/>",
"</Patient>"
));
//@formatter:on
}
@Test
public void testBundleWithBinary() {
//@formatter:off

View File

@ -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("<div>FOO</div>");
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<ResourceReferenceDt> conditions = new ArrayList<ResourceReferenceDt>();
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();
}
}

View File

@ -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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
@ -212,11 +210,11 @@ public class XmlParserDstu3Test {
"</managingOrganization>"
));
//@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("<Patient xmlns=\"http://hl7.org/fhir\"><extension url=\"http://example.com/extensions#someext\"><valueDateTime value=\"2011-01-02T11:13:15\"/></extension>"));
assertThat(enc, containsString("<modifierExtension url=\"http://example.com/extensions#modext\"><valueDate value=\"1995-01-02\"/></modifierExtension>"));
assertThat(enc, containsString(
"<extension url=\"http://example.com#parent\"><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension><extension url=\"http://example.com#child\"><valueString value=\"value2\"/></extension></extension>"));
assertThat(enc, containsString("<extension url=\"http://example.com#parent\"><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension><extension url=\"http://example.com#child\"><valueString value=\"value2\"/></extension></extension>"));
assertThat(enc, containsString("<given value=\"Joe\"><extension url=\"http://examples.com#givenext\"><valueString value=\"given\"/></extension></given>"));
assertThat(enc, containsString(
"<given value=\"Shmoe\"><extension url=\"http://examples.com#givenext_parent\"><extension url=\"http://examples.com#givenext_child\"><valueString value=\"CHILD\"/></extension></extension></given>"));
assertThat(enc, containsString("<given value=\"Shmoe\"><extension url=\"http://examples.com#givenext_parent\"><extension url=\"http://examples.com#givenext_child\"><valueString value=\"CHILD\"/></extension></extension></given>"));
/*
* 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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
@ -784,10 +780,8 @@ public class XmlParserDstu3Test {
ourLog.info(encoded);
// @formatter:on
assertThat(encoded,
stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"123\"/>", "<code>", "<coding>",
"<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>", "</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#123\"/>",
"<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
assertThat(encoded, stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"123\"/>", "<code>", "<coding>", "<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>",
"</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#123\"/>", "<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
//@formatter:off
}
@ -819,10 +813,8 @@ public class XmlParserDstu3Test {
ourLog.info(encoded);
//@formatter:on
assertThat(encoded,
stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"1\"/>", "<code>", "<coding>",
"<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>", "</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#1\"/>",
"<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
assertThat(encoded, stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"1\"/>", "<code>", "<coding>", "<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>",
"</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#1\"/>", "<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
//@formatter:off
}
@ -853,10 +845,8 @@ public class XmlParserDstu3Test {
ourLog.info(encoded);
//@formatter:on
assertThat(encoded,
stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"123\"/>", "<code>", "<coding>",
"<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>", "</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#123\"/>",
"<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
assertThat(encoded, stringContainsInOrder("<MedicationOrder xmlns=\"http://hl7.org/fhir\">", "<contained>", "<Medication xmlns=\"http://hl7.org/fhir\">", "<id value=\"123\"/>", "<code>", "<coding>", "<system value=\"urn:sys\"/>", "<code value=\"code1\"/>", "</coding>", "</code>",
"</Medication>", "</contained>", "<medicationReference>", "<reference value=\"#123\"/>", "<display value=\"MedRef\"/>", "</medicationReference>", "</MedicationOrder>"));
//@formatter:off
}
@ -966,15 +956,13 @@ public class XmlParserDstu3Test {
"<url value=\"http://exturl\"/>"
)));
//@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 {
"<url value=\"http://exturl\"/>"
)));
//@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<Reference> conditions = new ArrayList<Reference>();
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(
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<contained>",
"<Condition xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<bodySite>",
"<text value=\"BODY SITE\"/>",
"</bodySite>",
"</Condition>",
"</contained>",
"<extension url=\"testCondition\">",
"<valueReference>",
"<reference value=\"#1\"/>",
"</valueReference>",
"</extension>",
"<birthDate value=\"2016-04-14\"/>",
"</Patient>"
));
//@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("<reference value=\"Patient/phitcc_pat_normal\"/>"));
assertThat(str, containsString("<reference value=\"Observation/phitcc_obs_bp_dia\"/>"));
}
@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<Resource> contained = new ArrayList<Resource>();
// 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(
"<Appointment xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1234\"/>",
"<contained>",
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1000\"/>",
"</Patient>",
"</contained>",
"<contained>",
"<Location xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1001\"/>",
"</Location>",
"</contained>",
"<participant>",
"<actor>",
"<reference value=\"#1000\"/>",
"</actor>",
"</participant>",
"<participant>",
"<actor>",
"<reference value=\"#1001\"/>",
"</actor>",
"</participant>",
"</Appointment>"
));
//@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<String>(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<String>(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<String>(Arrays.asList("Patient.name")));
p.setEncodeElementsAppliesToResourceTypes(new HashSet<String>(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<String>(Arrays.asList("Patient.name")));
p.setEncodeElementsAppliesToResourceTypes(new HashSet<String>(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<String>(Arrays.asList("Patient")));
p.setEncodeElementsAppliesToResourceTypes(new HashSet<String>(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<String>(Arrays.asList("Patient")));
p.setEncodeElementsAppliesToResourceTypes(new HashSet<String>(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("<text><status value=\"generated\"/><div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"> John <b>SMITH </b>"));
}
@ -1391,26 +1480,24 @@ public class XmlParserDstu3Test {
String enc = ourCtx.newXmlParser().encodeResourceToString(patient);
assertThat(enc, containsString("<Patient xmlns=\"http://hl7.org/fhir\"><extension url=\"http://example.com/extensions#someext\"><valueDateTime value=\"2011-01-02T11:13:15\"/></extension>"));
assertThat(
enc,
containsString("<extension url=\"http://example.com#parent\"><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension></extension>"));
assertThat(enc, containsString("<extension url=\"http://example.com#parent\"><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension><extension url=\"http://example.com#child\"><valueString value=\"value1\"/></extension></extension>"));
assertThat(enc, containsString("<given value=\"Joe\"><extension url=\"http://examples.com#givenext\"><valueString value=\"given\"/></extension></given>"));
assertThat(enc, containsString("<given value=\"Shmoe\"><extension url=\"http://examples.com#givenext_parent\"><extension url=\"http://examples.com#givenext_child\"><valueString value=\"CHILD\"/></extension></extension></given>"));
}
@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<Reference> testConditions = null;
public List<Reference> getConditions() {
return this.testConditions;
}
public void setCondition(List<Reference> ref) {
this.testConditions = ref;
}
}
}

View File

@ -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("<div>FOO</div>");
patient.getText().setStatus(NarrativeStatus.GENERATED);
patient.getLanguageElement().setValue("en");
patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD"));
patient.getMeta().setLastUpdated(new Date());
List<Reference> conditions = new ArrayList<Reference>();
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=