Add custom extension example

This commit is contained in:
James Agnew 2016-08-21 10:24:42 -07:00
parent 379abb8e47
commit da8abca1ff
8 changed files with 447 additions and 241 deletions

View File

@ -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

View File

@ -58,7 +58,7 @@ public class DatabaseBackedPagingProvider extends FifoMemoryPagingProvider {
if (!provider.ensureSearchEntityLoaded()) { if (!provider.ensureSearchEntityLoaded()) {
return null; return null;
} }
return provider; retVal = provider;
} }
return retVal; return retVal;
} }

View File

@ -14,46 +14,16 @@ import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.dstu3.model.Appointment; import org.hl7.fhir.dstu3.model.*;
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.ContactPoint.ContactPointSystem; 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.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.SubscriptionChannelType;
import org.hl7.fhir.dstu3.model.Subscription.SubscriptionStatus; 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.IAnyResource;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; 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.BaseHapiFhirDao;
import ca.uhn.fhir.jpa.dao.SearchParameterMap; import ca.uhn.fhir.jpa.dao.SearchParameterMap;
import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamDate; import ca.uhn.fhir.jpa.entity.*;
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.model.api.IQueryParameterType; import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortOrderEnum;
import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.param.CompositeParam; import ca.uhn.fhir.rest.param.*;
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.server.Constants; import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;

View File

@ -1,5 +1,6 @@
package ca.uhn.fhir.jpa.provider.dstu3; 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.contains;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsInRelativeOrder; 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.AfterClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.NestedExceptionUtils;
import com.google.common.collect.Lists; 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.InstantDt;
import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.IParser; 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<String> 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 @Test
public void testHasParameter() throws Exception { public void testHasParameter() throws Exception {
IIdType pid0; IIdType pid0;

View File

@ -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.JpaConformanceProviderDstu3;
import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3; import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3;
import ca.uhn.fhir.jpa.provider.dstu3.TerminologyUploaderProviderDstu3; 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.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.server.ETagSupportEnum; import ca.uhn.fhir.rest.server.ETagSupportEnum;
import ca.uhn.fhir.rest.server.EncodingEnum; import ca.uhn.fhir.rest.server.EncodingEnum;
@ -200,11 +201,10 @@ public class TestRestfulServer extends RestfulServer {
setServerAddressStrategy(new MyHardcodedServerAddressStrategy(baseUrl)); setServerAddressStrategy(new MyHardcodedServerAddressStrategy(baseUrl));
/* /*
* This is a simple paging strategy that keeps the last 10 * Spool results to the database
* searches in memory
*/ */
setPagingProvider(new FifoMemoryPagingProvider(10).setMaximumPageSize(500)); setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class));
/* /*
* Load interceptors for the server from Spring * Load interceptors for the server from Spring
*/ */

View File

@ -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;
}
}
}

View File

@ -66,6 +66,7 @@ import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent; import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation; 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.client.IGenericClient;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
@ -82,159 +83,6 @@ public class XmlParserDstu3Test {
ourCtx.setNarrativeGenerator(null); 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("<reference value=\"" + pract.getId() + "\"/>"));
}
@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(
"<DiagnosticReport xmlns=\"http://hl7.org/fhir\">",
"<meta>",
"<profile value=\"http://custom_DiagnosticReport\"/>",
"</meta>",
"<contained>",
"<Observation xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<meta>",
"<profile value=\"http://custom_Observation\"/>",
"</meta>",
"<status value=\"final\"/>",
"</Observation>",
"</contained>",
"<status value=\"final\"/>",
"<result>",
"<reference value=\"#1\"/>",
"</result>",
"</DiagnosticReport>"));
//@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(
"<DiagnosticReport xmlns=\"http://hl7.org/fhir\">",
"<contained>",
"<Observation xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<status value=\"final\"/>",
"</Observation>",
"</contained>",
"<status value=\"final\"/>",
"<result>",
"<reference value=\"#1\"/>",
"</result>",
"</DiagnosticReport>"));
//@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("<reference value=\"http://foo.com/Organization/2/_history/1\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths(new ArrayList<String>());
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths((String[])null);
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths((List<String>)null);
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
}
@Test @Test
public void testBundleWithBinary() { public void testBundleWithBinary() {
//@formatter:off //@formatter:off
@ -265,7 +113,7 @@ public class XmlParserDstu3Test {
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent()); assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
} }
@Test @Test
public void testContainedResourceInExtensionUndeclared() { public void testContainedResourceInExtensionUndeclared() {
Patient p = new Patient(); Patient p = new Patient();
@ -287,7 +135,7 @@ public class XmlParserDstu3Test {
o = (Organization) rr.getResource(); o = (Organization) rr.getResource();
assertEquals("ORG", o.getName()); assertEquals("ORG", o.getName());
} }
@Test @Test
public void testDuration() { public void testDuration() {
Encounter enc = new Encounter(); Encounter enc = new Encounter();
@ -301,7 +149,7 @@ public class XmlParserDstu3Test {
assertThat(str, not(containsString("meta"))); assertThat(str, not(containsString("meta")));
assertThat(str, containsString("<length><value value=\"123\"/><unit value=\"day\"/></length>")); assertThat(str, containsString("<length><value value=\"123\"/><unit value=\"day\"/></length>"));
} }
@Test @Test
public void testEncodeAndParseBundleWithResourceRefs() { public void testEncodeAndParseBundleWithResourceRefs() {
@ -339,6 +187,24 @@ public class XmlParserDstu3Test {
assertSame(org, pt.getManagingOrganization().getResource()); 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 @Test
public void testEncodeAndParseContained() { public void testEncodeAndParseContained() {
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); 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(
"<DiagnosticReport xmlns=\"http://hl7.org/fhir\">",
"<meta>",
"<profile value=\"http://custom_DiagnosticReport\"/>",
"</meta>",
"<contained>",
"<Observation xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<meta>",
"<profile value=\"http://custom_Observation\"/>",
"</meta>",
"<status value=\"final\"/>",
"</Observation>",
"</contained>",
"<status value=\"final\"/>",
"<result>",
"<reference value=\"#1\"/>",
"</result>",
"</DiagnosticReport>"));
//@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(
"<DiagnosticReport xmlns=\"http://hl7.org/fhir\">",
"<contained>",
"<Observation xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<status value=\"final\"/>",
"</Observation>",
"</contained>",
"<status value=\"final\"/>",
"<result>",
"<reference value=\"#1\"/>",
"</result>",
"</DiagnosticReport>"));
//@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 @Test
public void testEncodeAndParseExtensionOnCode() { public void testEncodeAndParseExtensionOnCode() {
Organization o = new Organization(); Organization o = new Organization();
@ -1113,6 +1081,30 @@ public class XmlParserDstu3Test {
ourLog.info(parser.encodeResourceToString(gr)); 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("<value value=\"APPID\"/>"));
assertThat(encode, stringContainsInOrder("<source", "<dest"));
}
/** /**
* Make sure whitespace is preserved for pre tags * Make sure whitespace is preserved for pre tags
*/ */
@ -1341,6 +1333,39 @@ public class XmlParserDstu3Test {
} }
@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("<reference value=\"http://foo.com/Organization/2/_history/1\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths(new ArrayList<String>());
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths((String[])null);
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
parser.setDontStripVersionsFromReferencesAtPaths((List<String>)null);
enc = parser.setPrettyPrint(true).encodeResourceToString(auditEvent);
ourLog.info(enc);
assertThat(enc, containsString("<reference value=\"http://foo.com/Organization/2\"/>"));
}
@Test @Test
public void testEncodeNarrativeSuppressed() { public void testEncodeNarrativeSuppressed() {
Patient patient = new Patient(); Patient patient = new Patient();
@ -1471,6 +1496,23 @@ public class XmlParserDstu3Test {
assertThat(str, containsString("<reference value=\"Observation/phitcc_obs_bp_dia\"/>")); assertThat(str, containsString("<reference value=\"Observation/phitcc_obs_bp_dia\"/>"));
} }
@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("<reference value=\"" + pract.getId() + "\"/>"));
}
@Test @Test
public void testEncodeSummary() { public void testEncodeSummary() {
Patient patient = new Patient(); Patient patient = new Patient();
@ -1534,30 +1576,6 @@ public class XmlParserDstu3Test {
assertThat(encode, stringContainsInOrder("<source", "<dest")); assertThat(encode, stringContainsInOrder("<source", "<dest"));
} }
@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("<value value=\"APPID\"/>"));
assertThat(encode, stringContainsInOrder("<source", "<dest"));
}
@Test @Test
public void testEncodeUndeclaredExtensionWithEnumerationContent() { public void testEncodeUndeclaredExtensionWithEnumerationContent() {
IParser parser = ourCtx.newXmlParser(); IParser parser = ourCtx.newXmlParser();

View File

@ -240,6 +240,36 @@
</macro> </macro>
</subsection> </subsection>
<subsection name="Custom Type Examples: Composite Extensions">
<p>
The following example shows a resource containing a composite
extension.
</p>
<macro name="snippet">
<param name="id" value="resource" />
<param name="file" value="examples/src/main/java/example/customtype/CustomCompositeExtension.java" />
</macro>
<p>
This could be used to create a resource such as the
following:
</p>
<source><![CDATA[<Patient xmlns="http://hl7.org/fhir">
<id value="123"/>
<extension url="http://acme.org/fooParent">
<extension url="http://acme.org/fooChildA">
<valueString value="ValueA"/>
</extension>
<extension url="http://acme.org/fooChildB">
<valueString value="ValueB"/>
</extension>
</extension>
</Patient>]]></source>
</subsection>
</section> </section>