diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomMedicationOrderDstu2.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomMedicationOrderDstu2.java new file mode 100644 index 00000000000..bc1ca425775 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomMedicationOrderDstu2.java @@ -0,0 +1,28 @@ +package ca.uhn.fhir.parser; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt; +import ca.uhn.fhir.model.dstu2.resource.Medication; +import ca.uhn.fhir.model.dstu2.resource.MedicationOrder; + +/** + * See #368 + */ +@ResourceDef(/*name="MedicationOrder"*/) +public class CustomMedicationOrderDstu2 extends MedicationOrder { + + private static final long serialVersionUID = 1L; + + @Child(name = "medication", order = Child.REPLACE_PARENT, min = 1, max = 1, summary = false, modifier = false, type = { Medication.class }) + private ResourceReferenceDt myMedication; + + public ResourceReferenceDt getMedication() { + return myMedication; + } + + public void setMedication(ResourceReferenceDt theMedication) { + myMedication = theMedication; + } + +} diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomTypeDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomTypeDstu2Test.java index 27921bd7f85..742fd929af8 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomTypeDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomTypeDstu2Test.java @@ -5,8 +5,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.stringContainsInOrder; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; @@ -22,6 +21,7 @@ import ca.uhn.fhir.model.api.annotation.Description; import ca.uhn.fhir.model.api.annotation.Extension; import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.dstu2.composite.QuantityDt; +import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.Medication; import ca.uhn.fhir.model.dstu2.resource.MedicationOrder; @@ -43,6 +43,47 @@ public class CustomTypeDstu2Test { TestUtil.clearAllStaticFieldsForUnitTest(); } + /** + * See #368 + */ + @Test + public void testConstrainedFieldContainedResource() { + Medication medication = new Medication(); + medication.getCode().setText("MED TEXT"); + + CustomMedicationOrderDstu2 mo = new CustomMedicationOrderDstu2(); + mo.setMedication(new ResourceReferenceDt()); + mo.getMedication().setResource(medication); + + String string = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(mo); + ourLog.info(string); + + //@formatter:on + assertThat(string, stringContainsInOrder( + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + "")); + //@formatter:on + + mo = ourCtx.newXmlParser().parseResource(CustomMedicationOrderDstu2.class, string); + + medication = (Medication) mo.getMedication().getResource(); + assertNotNull(medication); + assertEquals("#1", medication.getId().getValue()); + assertEquals("MED TEXT", medication.getCode().getText()); + + } + /** * See #364 */