diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu2Test.java new file mode 100644 index 00000000000..646fd0c3bd4 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu2Test.java @@ -0,0 +1,108 @@ +package ca.uhn.fhir.parser; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.primitive.StringDt; +import ca.uhn.fhir.util.TestUtil; +import org.junit.AfterClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Sébastien Rivière 12/04/2017 + */ +public class ElementWithExtensionDstu2Test { + + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ca.uhn.fhir.parser.ElementWithExtensionDstu2Test.class); + private static final FhirContext ctx = FhirContext.forDstu2(); + + @AfterClass + public static void afterClassClearContext() { + TestUtil.clearAllStaticFieldsForUnitTest(); + } + + @Test + public void testExtensionOnPrimitiveExtensionJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getPetName().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getPetName().getUndeclaredExtensions().size()); + } + + @Test + public void testExtensionOnPrimitiveExtensionXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getPetName().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getPetName().getUndeclaredExtensions().size()); + } + + @Test + public void testExtensionOnIDDatatypeJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getId().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getId().getUndeclaredExtensions().size()); + } + + @Test + public void testExtensionOnIDDatatypeXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getId().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getId().getUndeclaredExtensions().size()); + } + + @Test + public void testExtensionOnIDDatatypeExtensionJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getCustomId().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getCustomId().getUndeclaredExtensions().size()); + } + + @Test + public void testExtensionOnIDDatatypeExtensionXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getCustomId().addUndeclaredExtension(false, "http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringDt("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getCustomId().getUndeclaredExtensions().size()); + } +} + diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java index 4b62f4ea575..d6aa14058ab 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java @@ -5,6 +5,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.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.StringDt; @ResourceDef() @@ -17,11 +18,19 @@ public class MyPatientWithCustomUrlExtension extends Patient { @Description(shortDefinition = "The name of the patient's favourite pet") private StringDt myPetName; + @Child(name = "customid") + @Extension(url = "/customid", definedLocally = false, isModifier = false) + @Description(shortDefinition = "The customid of the patient's ") + private IdDt myCustomId; + public StringDt getPetName() { + if (myPetName == null) { + myPetName = new StringDt(); + } return myPetName; } - public void setPetName(StringDt thePetName) { + public void setPetName(final StringDt thePetName) { myPetName = thePetName; } @@ -30,4 +39,14 @@ public class MyPatientWithCustomUrlExtension extends Patient { return super.isEmpty() && myPetName.isEmpty(); } + public IdDt getCustomId() { + if (myCustomId == null) { + myCustomId = new IdDt(); + } + return myCustomId; + } + + public void setCustomId(final IdDt myCustomId) { + this.myCustomId = myCustomId; + } } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu3Test.java new file mode 100644 index 00000000000..516a459b564 --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/ElementWithExtensionDstu3Test.java @@ -0,0 +1,108 @@ +package ca.uhn.fhir.parser; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.util.TestUtil; +import org.hl7.fhir.dstu3.model.StringType; +import org.junit.AfterClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Sébastien Rivière 12/04/2017 + */ +public class ElementWithExtensionDstu3Test { + + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ca.uhn.fhir.parser.ElementWithExtensionDstu3Test.class); + private static final FhirContext ctx = FhirContext.forDstu3(); + + @AfterClass + public static void afterClassClearContext() { + TestUtil.clearAllStaticFieldsForUnitTest(); + } + + @Test + public void testNullFlavorPrimitiveExtensionJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getPetName().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getPetName().getExtension().size()); + } + + @Test + public void testNullFlavorPrimitiveExtensionXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getPetName().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getPetName().getExtension().size()); + } + + @Test + public void testNullFlavorIDDatatypeJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getIdElement().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getIdElement().getExtension().size()); + } + + @Test + public void testNullFlavorIDDatatypeXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getIdElement().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getIdElement().getExtension().size()); + } + + @Test + public void testNullFlavorExtensionIDDatatypeJson() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getCustomId().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newJsonParser().setPrettyPrint(true); + final String json = parser.encodeResourceToString(patient); + + ourLog.info(json); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, json); + assertEquals(1, patient.getCustomId().getExtension().size()); + } + + @Test + public void testNullFlavorExtensionIDDatatypeXml() throws Exception { + MyPatientWithCustomUrlExtension patient = new MyPatientWithCustomUrlExtension(); + patient.setId("1"); + patient.getCustomId().addExtension("http://hl7.org/fhir/StructureDefinition/iso21090-nullFlavor", new StringType("UNK")); + final IParser parser = ctx.newXmlParser().setPrettyPrint(true); + final String xml = parser.encodeResourceToString(patient); + + ourLog.info(xml); + + patient = parser.parseResource(MyPatientWithCustomUrlExtension.class, xml); + assertEquals(1, patient.getCustomId().getExtension().size()); + } +} + diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java index d668ecf7e08..cca4b7ede6d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/MyPatientWithCustomUrlExtension.java @@ -4,30 +4,49 @@ import ca.uhn.fhir.model.api.annotation.Child; 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 org.hl7.fhir.dstu3.model.StringType; +import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.Patient; +import org.hl7.fhir.dstu3.model.StringType; @ResourceDef() public class MyPatientWithCustomUrlExtension extends Patient { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - @Child(name = "petName") - @Extension(url = "/petname", definedLocally = false, isModifier = false) - @Description(shortDefinition = "The name of the patient's favourite pet") - private StringType myPetName; + @Child(name = "petName") + @Extension(url = "/petname", definedLocally = false, isModifier = false) + @Description(shortDefinition = "The name of the patient's favourite pet") + private StringType myPetName; - public StringType getPetName() { - return myPetName; + @Child(name = "customid") + @Extension(url = "/customid", definedLocally = false, isModifier = false) + @Description(shortDefinition = "The customid of the patient's ") + private IdType myCustomId; + + public StringType getPetName() { + if (myPetName == null) { + myPetName = new StringType(); } + return myPetName; + } - public void setPetName(final StringType thePetName) { - myPetName = thePetName; - } - - @Override - public boolean isEmpty() { - return super.isEmpty() && myPetName.isEmpty(); + public void setPetName(final StringType thePetName) { + myPetName = thePetName; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() && myPetName.isEmpty(); + } + + public IdType getCustomId() { + if (myCustomId == null) { + myCustomId = new IdType(); } + return myCustomId; + } + public void setCustomId(final IdType myCustomId) { + this.myCustomId = myCustomId; + } }