From da8abca1ffdaa4dcae3d931b2e4732e437951cac Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sun, 21 Aug 2016 10:24:42 -0700 Subject: [PATCH] Add custom extension example --- .../customtype/CustomCompositeExtension.java | 84 ++++ .../search/DatabaseBackedPagingProvider.java | 2 +- .../FhirResourceDaoDstu3SearchNoFtTest.java | 60 +-- .../dstu3/ResourceProviderDstu3Test.java | 44 ++ .../ca/uhn/fhirtest/TestRestfulServer.java | 8 +- .../PatientWithCustomCompositeExtension.java | 82 ++++ .../uhn/fhir/parser/XmlParserDstu3Test.java | 378 +++++++++--------- src/site/xdoc/doc_extensions.xml | 30 ++ 8 files changed, 447 insertions(+), 241 deletions(-) create mode 100644 examples/src/main/java/example/customtype/CustomCompositeExtension.java create mode 100644 hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/PatientWithCustomCompositeExtension.java diff --git a/examples/src/main/java/example/customtype/CustomCompositeExtension.java b/examples/src/main/java/example/customtype/CustomCompositeExtension.java new file mode 100644 index 00000000000..49a1ce5f5ce --- /dev/null +++ b/examples/src/main/java/example/customtype/CustomCompositeExtension.java @@ -0,0 +1,84 @@ +package example.customtype; + +import org.hl7.fhir.dstu3.model.BackboneElement; +import org.hl7.fhir.dstu3.model.Patient; +import org.hl7.fhir.dstu3.model.StringType; + +import ca.uhn.fhir.model.api.annotation.Block; +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.Extension; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import ca.uhn.fhir.util.ElementUtil; + +//START SNIPPET: resource +@ResourceDef(name = "Patient") +public class CustomCompositeExtension extends Patient { + + private static final long serialVersionUID = 1L; + + /** + * A custom extension + */ + @Child(name = "foo") + @Extension(url="http://acme.org/fooParent", definedLocally = false, isModifier = false) + protected FooParentExtension fooParentExtension; + + public FooParentExtension getFooParentExtension() { + return fooParentExtension; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() && ElementUtil.isEmpty(fooParentExtension); + } + + public void setFooParentExtension(FooParentExtension theFooParentExtension) { + fooParentExtension = theFooParentExtension; + } + + @Block + public static class FooParentExtension extends BackboneElement { + + private static final long serialVersionUID = 4522090347756045145L; + + @Child(name = "childA") + @Extension(url = "http://acme.org/fooChildA", definedLocally = false, isModifier = false) + private StringType myChildA; + + @Child(name = "childB") + @Extension(url = "http://acme.org/fooChildB", definedLocally = false, isModifier = false) + private StringType myChildB; + + @Override + public FooParentExtension copy() { + FooParentExtension copy = new FooParentExtension(); + copy.myChildA = myChildA; + copy.myChildB = myChildB; + return copy; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() && ElementUtil.isEmpty(myChildA, myChildB); + } + + public StringType getChildA() { + return myChildA; + } + + public StringType getChildB() { + return myChildB; + } + + public void setChildA(StringType theChildA) { + myChildA = theChildA; + } + + public void setChildB(StringType theChildB) { + myChildB = theChildB; + } + + } + +} +//END SNIPPET: resource diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/DatabaseBackedPagingProvider.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/DatabaseBackedPagingProvider.java index 3184454e192..6fc1ddba100 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/DatabaseBackedPagingProvider.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/DatabaseBackedPagingProvider.java @@ -58,7 +58,7 @@ public class DatabaseBackedPagingProvider extends FifoMemoryPagingProvider { if (!provider.ensureSearchEntityLoaded()) { return null; } - return provider; + retVal = provider; } return retVal; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java index 0ed2703f7ae..c28c06470a8 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java @@ -14,46 +14,16 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import java.math.BigDecimal; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; -import org.hl7.fhir.dstu3.model.Appointment; -import org.hl7.fhir.dstu3.model.CodeType; -import org.hl7.fhir.dstu3.model.CodeableConcept; -import org.hl7.fhir.dstu3.model.Coding; +import org.hl7.fhir.dstu3.model.*; import org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem; -import org.hl7.fhir.dstu3.model.DateTimeType; -import org.hl7.fhir.dstu3.model.DateType; -import org.hl7.fhir.dstu3.model.Device; -import org.hl7.fhir.dstu3.model.DiagnosticRequest; -import org.hl7.fhir.dstu3.model.DiagnosticReport; -import org.hl7.fhir.dstu3.model.Encounter; import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; -import org.hl7.fhir.dstu3.model.IdType; -import org.hl7.fhir.dstu3.model.Immunization; -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.Observation; -import org.hl7.fhir.dstu3.model.Organization; -import org.hl7.fhir.dstu3.model.Patient; -import org.hl7.fhir.dstu3.model.Period; -import org.hl7.fhir.dstu3.model.Practitioner; -import org.hl7.fhir.dstu3.model.Quantity; -import org.hl7.fhir.dstu3.model.Reference; -import org.hl7.fhir.dstu3.model.StringType; -import org.hl7.fhir.dstu3.model.Subscription; import org.hl7.fhir.dstu3.model.Subscription.SubscriptionChannelType; import org.hl7.fhir.dstu3.model.Subscription.SubscriptionStatus; -import org.hl7.fhir.dstu3.model.Substance; -import org.hl7.fhir.dstu3.model.TemporalPrecisionEnum; -import org.hl7.fhir.dstu3.model.ValueSet; import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; @@ -63,35 +33,13 @@ import org.junit.Test; import ca.uhn.fhir.jpa.dao.BaseHapiFhirDao; import ca.uhn.fhir.jpa.dao.SearchParameterMap; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamDate; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamNumber; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamQuantity; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamToken; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamUri; -import ca.uhn.fhir.jpa.entity.ResourceLink; +import ca.uhn.fhir.jpa.entity.*; import ca.uhn.fhir.model.api.IQueryParameterType; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; -import ca.uhn.fhir.rest.param.CompositeParam; -import ca.uhn.fhir.rest.param.DateParam; -import ca.uhn.fhir.rest.param.DateRangeParam; -import ca.uhn.fhir.rest.param.HasParam; -import ca.uhn.fhir.rest.param.NumberParam; -import ca.uhn.fhir.rest.param.ParamPrefixEnum; -import ca.uhn.fhir.rest.param.QuantityParam; -import ca.uhn.fhir.rest.param.ReferenceParam; -import ca.uhn.fhir.rest.param.StringAndListParam; -import ca.uhn.fhir.rest.param.StringOrListParam; -import ca.uhn.fhir.rest.param.StringParam; -import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.rest.param.TokenOrListParam; -import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.rest.param.TokenParamModifier; -import ca.uhn.fhir.rest.param.UriParam; -import ca.uhn.fhir.rest.param.UriParamQualifierEnum; +import ca.uhn.fhir.rest.param.*; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java index bfa7ae574e2..99986be7610 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java @@ -1,5 +1,6 @@ package ca.uhn.fhir.jpa.provider.dstu3; +import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInRelativeOrder; @@ -95,9 +96,11 @@ import org.hl7.fhir.instance.model.api.IIdType; import org.junit.AfterClass; import org.junit.Ignore; import org.junit.Test; +import org.springframework.core.NestedExceptionUtils; import com.google.common.collect.Lists; +import ca.uhn.fhir.jpa.dao.SearchParameterMap; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.parser.IParser; @@ -128,6 +131,47 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { } + @Test + public void testSearchPagingKeepsOldSearches() throws Exception { + String methodName = "testSearchPagingKeepsOldSearches"; + IIdType pid1; + { + Patient patient = new Patient(); + patient.addIdentifier().setSystem("urn:system").setValue("0"); + patient.addName().addFamily(methodName).addGiven("Joe"); + pid1 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless(); + } + + for (int i = 1; i <= 20; i++) { + Patient patient = new Patient(); + patient.addIdentifier().setSystem("urn:system").setValue(Integer.toString(i)); + patient.addName().addFamily(methodName).addGiven("Joe"); + myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless(); + } + + List linkNext = Lists.newArrayList(); + for (int i = 0 ; i < 100; i++) { + Bundle bundle = ourClient + .search() + .forResource(Patient.class) + .where(Patient.NAME.matches().value("testSearchPagingKeepsOldSearches")) + .count(5) + .returnBundle(Bundle.class) + .execute(); + assertTrue(isNotBlank(bundle.getLink("next").getUrl())); + assertEquals(5, bundle.getEntry().size()); + linkNext.add(bundle.getLink("next").getUrl()); + } + + int index = 0; + for (String nextLink : linkNext) { + ourLog.info("Fetching index {}", index++); + Bundle b = ourClient.fetchResourceFromUrl(Bundle.class, nextLink); + assertEquals(5, b.getEntry().size()); + } + } + + @Test public void testHasParameter() throws Exception { IIdType pid0; diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java index c78f90f983a..8b076a2d19b 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/TestRestfulServer.java @@ -25,6 +25,7 @@ import ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2; import ca.uhn.fhir.jpa.provider.dstu3.JpaConformanceProviderDstu3; import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3; import ca.uhn.fhir.jpa.provider.dstu3.TerminologyUploaderProviderDstu3; +import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.rest.server.ETagSupportEnum; import ca.uhn.fhir.rest.server.EncodingEnum; @@ -200,11 +201,10 @@ public class TestRestfulServer extends RestfulServer { setServerAddressStrategy(new MyHardcodedServerAddressStrategy(baseUrl)); /* - * This is a simple paging strategy that keeps the last 10 - * searches in memory + * Spool results to the database */ - setPagingProvider(new FifoMemoryPagingProvider(10).setMaximumPageSize(500)); - + setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class)); + /* * Load interceptors for the server from Spring */ diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/PatientWithCustomCompositeExtension.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/PatientWithCustomCompositeExtension.java new file mode 100644 index 00000000000..82ea6448407 --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/PatientWithCustomCompositeExtension.java @@ -0,0 +1,82 @@ +package ca.uhn.fhir.parser; + +import org.hl7.fhir.dstu3.model.BackboneElement; +import org.hl7.fhir.dstu3.model.Patient; +import org.hl7.fhir.dstu3.model.StringType; + +import ca.uhn.fhir.model.api.annotation.Block; +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.Extension; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import ca.uhn.fhir.util.ElementUtil; + +@ResourceDef(name = "Patient") +public class PatientWithCustomCompositeExtension extends Patient { + + private static final long serialVersionUID = 1L; + + /** + * A custom extension + */ + @Child(name = "foo") + @Extension(url="http://acme.org/fooParent", definedLocally = false, isModifier = false) + protected FooParentExtension fooParentExtension; + + public FooParentExtension getFooParentExtension() { + return fooParentExtension; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() && ElementUtil.isEmpty(fooParentExtension); + } + + public void setFooParentExtension(FooParentExtension theFooParentExtension) { + fooParentExtension = theFooParentExtension; + } + + @Block + public static class FooParentExtension extends BackboneElement { + + private static final long serialVersionUID = 4522090347756045145L; + + @Child(name = "childA") + @Extension(url = "http://acme.org/fooChildA", definedLocally = false, isModifier = false) + private StringType myChildA; + + @Child(name = "childB") + @Extension(url = "http://acme.org/fooChildB", definedLocally = false, isModifier = false) + private StringType myChildB; + + @Override + public FooParentExtension copy() { + FooParentExtension copy = new FooParentExtension(); + copy.myChildA = myChildA; + copy.myChildB = myChildB; + return copy; + } + + @Override + public boolean isEmpty() { + return super.isEmpty() && ElementUtil.isEmpty(myChildA, myChildB); + } + + public StringType getChildA() { + return myChildA; + } + + public StringType getChildB() { + return myChildB; + } + + public void setChildA(StringType theChildA) { + myChildA = theChildA; + } + + public void setChildB(StringType theChildB) { + myChildB = theChildB; + } + + } + +} \ No newline at end of file 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 b88e3678ec2..b6535883644 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 @@ -66,6 +66,7 @@ import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent; import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; +import ca.uhn.fhir.parser.PatientWithCustomCompositeExtension.FooParentExtension; import ca.uhn.fhir.rest.client.IGenericClient; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.util.TestUtil; @@ -82,159 +83,6 @@ public class XmlParserDstu3Test { ourCtx.setNarrativeGenerator(null); } - @Test - public void testEncodeReferenceWithUuid() { - - Practitioner pract = new Practitioner(); - pract.setId(IdType.newRandomUuid()); - pract.addName().addFamily("PRACT FAMILY"); - - Patient patient = new Patient(); - patient.addGeneralPractitioner().setResource(pract); - - String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); - ourLog.info(encoded); - - assertThat(pract.getId(), startsWith("urn:uuid:")); - assertThat(encoded, containsString("")); - } - - @Test - public void testEncodeAndParseContainedCustomTypes() { - ourCtx = FhirContext.forDstu3(); - ourCtx.setDefaultTypeForProfile(CustomObservation.PROFILE, CustomObservation.class); - ourCtx.setDefaultTypeForProfile(CustomDiagnosticReport.PROFILE, CustomDiagnosticReport.class); - - CustomObservation obs = new CustomObservation(); - obs.setStatus(ObservationStatus.FINAL); - - CustomDiagnosticReport dr = new CustomDiagnosticReport(); - dr.setStatus(DiagnosticReportStatus.FINAL); - dr.addResult().setResource(obs); - - IParser parser = ourCtx.newXmlParser(); - parser.setPrettyPrint(true); - - String output = parser.encodeResourceToString(dr); - ourLog.info(output); - - //@formatter:off - assertThat(output,stringContainsInOrder( - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "")); - //@formatter:on - - /* - * Now PARSE! - */ - - dr = (CustomDiagnosticReport) parser.parseResource(output); - assertEquals(DiagnosticReportStatus.FINAL, dr.getStatus()); - - assertEquals("#1", dr.getResult().get(0).getReference()); - obs = (CustomObservation) dr.getResult().get(0).getResource(); - assertEquals(ObservationStatus.FINAL, obs.getStatus()); - - ourCtx = null; - } - - @Test - public void testEncodeAndParseContainedNonCustomTypes() { - ourCtx = FhirContext.forDstu3(); - - Observation obs = new Observation(); - obs.setStatus(ObservationStatus.FINAL); - - DiagnosticReport dr = new DiagnosticReport(); - dr.setStatus(DiagnosticReportStatus.FINAL); - dr.addResult().setResource(obs); - - IParser parser = ourCtx.newXmlParser(); - parser.setPrettyPrint(true); - - String output = parser.encodeResourceToString(dr); - ourLog.info(output); - - //@formatter:off - assertThat(output,stringContainsInOrder( - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "")); - //@formatter:on - - /* - * Now PARSE! - */ - - dr = (DiagnosticReport) parser.parseResource(output); - assertEquals(DiagnosticReportStatus.FINAL, dr.getStatus()); - - assertEquals("#1", dr.getResult().get(0).getReference()); - obs = (Observation) dr.getResult().get(0).getResource(); - assertEquals(ObservationStatus.FINAL, obs.getStatus()); - - ourCtx = null; - } - - @Test - public void testEncodeHistoryEncodeVersionsAtPath3() { - ourCtx = FhirContext.forDstu3(); - - assertNull(ourCtx.newXmlParser().getStripVersionsFromReferences()); - - AuditEvent auditEvent = new AuditEvent(); - auditEvent.addEntity().setReference(new Reference("http://foo.com/Organization/2/_history/1")); - - IParser parser = ourCtx.newXmlParser(); - - parser.setDontStripVersionsFromReferencesAtPaths("AuditEvent.entity.reference"); - String enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); - ourLog.info(enc); - assertThat(enc, containsString("")); - - parser.setDontStripVersionsFromReferencesAtPaths(new ArrayList()); - enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); - ourLog.info(enc); - assertThat(enc, containsString("")); - - parser.setDontStripVersionsFromReferencesAtPaths((String[])null); - enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); - ourLog.info(enc); - assertThat(enc, containsString("")); - - parser.setDontStripVersionsFromReferencesAtPaths((List)null); - enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); - ourLog.info(enc); - assertThat(enc, containsString("")); - - } - - @Test public void testBundleWithBinary() { //@formatter:off @@ -265,7 +113,7 @@ public class XmlParserDstu3Test { assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); } - + @Test public void testContainedResourceInExtensionUndeclared() { Patient p = new Patient(); @@ -287,7 +135,7 @@ public class XmlParserDstu3Test { o = (Organization) rr.getResource(); assertEquals("ORG", o.getName()); } - + @Test public void testDuration() { Encounter enc = new Encounter(); @@ -301,7 +149,7 @@ public class XmlParserDstu3Test { assertThat(str, not(containsString("meta"))); assertThat(str, containsString("")); } - + @Test public void testEncodeAndParseBundleWithResourceRefs() { @@ -339,6 +187,24 @@ public class XmlParserDstu3Test { assertSame(org, pt.getManagingOrganization().getResource()); } + @Test + public void testEncodeAndParseCompositeExtension() { + PatientWithCustomCompositeExtension pat = new PatientWithCustomCompositeExtension(); + pat.setId("123"); + pat.setFooParentExtension(new FooParentExtension()); + pat.getFooParentExtension().setChildA(new StringType("ValueA")); + pat.getFooParentExtension().setChildB(new StringType("ValueB")); + + String enc = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pat); + ourLog.info(enc); + + pat = ourCtx.newXmlParser().parseResource(PatientWithCustomCompositeExtension.class, enc); + + assertEquals("ValueA", pat.getFooParentExtension().getChildA().getValue()); + assertEquals("ValueB", pat.getFooParentExtension().getChildB().getValue()); + } + + @Test public void testEncodeAndParseContained() { IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); @@ -408,6 +274,108 @@ public class XmlParserDstu3Test { } + @Test + public void testEncodeAndParseContainedCustomTypes() { + ourCtx = FhirContext.forDstu3(); + ourCtx.setDefaultTypeForProfile(CustomObservation.PROFILE, CustomObservation.class); + ourCtx.setDefaultTypeForProfile(CustomDiagnosticReport.PROFILE, CustomDiagnosticReport.class); + + CustomObservation obs = new CustomObservation(); + obs.setStatus(ObservationStatus.FINAL); + + CustomDiagnosticReport dr = new CustomDiagnosticReport(); + dr.setStatus(DiagnosticReportStatus.FINAL); + dr.addResult().setResource(obs); + + IParser parser = ourCtx.newXmlParser(); + parser.setPrettyPrint(true); + + String output = parser.encodeResourceToString(dr); + ourLog.info(output); + + //@formatter:off + assertThat(output,stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "")); + //@formatter:on + + /* + * Now PARSE! + */ + + dr = (CustomDiagnosticReport) parser.parseResource(output); + assertEquals(DiagnosticReportStatus.FINAL, dr.getStatus()); + + assertEquals("#1", dr.getResult().get(0).getReference()); + obs = (CustomObservation) dr.getResult().get(0).getResource(); + assertEquals(ObservationStatus.FINAL, obs.getStatus()); + + ourCtx = null; + } + + @Test + public void testEncodeAndParseContainedNonCustomTypes() { + ourCtx = FhirContext.forDstu3(); + + Observation obs = new Observation(); + obs.setStatus(ObservationStatus.FINAL); + + DiagnosticReport dr = new DiagnosticReport(); + dr.setStatus(DiagnosticReportStatus.FINAL); + dr.addResult().setResource(obs); + + IParser parser = ourCtx.newXmlParser(); + parser.setPrettyPrint(true); + + String output = parser.encodeResourceToString(dr); + ourLog.info(output); + + //@formatter:off + assertThat(output,stringContainsInOrder( + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "")); + //@formatter:on + + /* + * Now PARSE! + */ + + dr = (DiagnosticReport) parser.parseResource(output); + assertEquals(DiagnosticReportStatus.FINAL, dr.getStatus()); + + assertEquals("#1", dr.getResult().get(0).getReference()); + obs = (Observation) dr.getResult().get(0).getResource(); + assertEquals(ObservationStatus.FINAL, obs.getStatus()); + + ourCtx = null; + } + @Test public void testEncodeAndParseExtensionOnCode() { Organization o = new Organization(); @@ -1113,6 +1081,30 @@ public class XmlParserDstu3Test { ourLog.info(parser.encodeResourceToString(gr)); } + @Test + public void testEncodeDeclaredBlock() throws Exception { + FooMessageSourceComponent source = new FooMessageHeaderWithExplicitField.FooMessageSourceComponent(); + source.getMessageHeaderApplicationId().setValue("APPID"); + source.setName("NAME"); + + FooMessageHeaderWithExplicitField header = new FooMessageHeaderWithExplicitField(); + header.setSourceNew(source); + + header.addDestination().setName("DEST"); + + Bundle bundle = new Bundle(); + bundle.addEntry().setResource(header); + + IParser p = ourCtx.newXmlParser(); + p.setPrettyPrint(true); + + String encode = p.encodeResourceToString(bundle); + ourLog.info(encode); + + assertThat(encode, containsString("")); + assertThat(encode, stringContainsInOrder("")); + + parser.setDontStripVersionsFromReferencesAtPaths(new ArrayList()); + enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); + ourLog.info(enc); + assertThat(enc, containsString("")); + + parser.setDontStripVersionsFromReferencesAtPaths((String[])null); + enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); + ourLog.info(enc); + assertThat(enc, containsString("")); + + parser.setDontStripVersionsFromReferencesAtPaths((List)null); + enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent); + ourLog.info(enc); + assertThat(enc, containsString("")); + + } + @Test public void testEncodeNarrativeSuppressed() { Patient patient = new Patient(); @@ -1471,6 +1496,23 @@ public class XmlParserDstu3Test { assertThat(str, containsString("")); } + @Test + public void testEncodeReferenceWithUuid() { + + Practitioner pract = new Practitioner(); + pract.setId(IdType.newRandomUuid()); + pract.addName().addFamily("PRACT FAMILY"); + + Patient patient = new Patient(); + patient.addGeneralPractitioner().setResource(pract); + + String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + + assertThat(pract.getId(), startsWith("urn:uuid:")); + assertThat(encoded, containsString("")); + } + @Test public void testEncodeSummary() { Patient patient = new Patient(); @@ -1534,30 +1576,6 @@ public class XmlParserDstu3Test { assertThat(encode, stringContainsInOrder("")); - assertThat(encode, stringContainsInOrder(" + + + +

+ The following example shows a resource containing a composite + extension. +

+ + + + + + +

+ This could be used to create a resource such as the + following: +

+ + + + + + + + + + +]]> + +