Fix unit tests

This commit is contained in:
James Agnew 2016-08-30 18:27:57 -04:00
parent 98e0753b93
commit 6b9f8ec487
2 changed files with 124 additions and 120 deletions

View File

@ -95,12 +95,13 @@ public class ResourceReferenceInfo {
int colonIndex = theInclude.getValue().indexOf(':'); int colonIndex = theInclude.getValue().indexOf(':');
if (colonIndex != -1) { if (colonIndex != -1) {
// DSTU2+ style // DSTU2+ style
String resourceName = theInclude.getValue().substring(0, colonIndex);
String paramName = theInclude.getValue().substring(colonIndex + 1); String paramName = theInclude.getValue().substring(colonIndex + 1);
RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(myOwningResource); RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(resourceName);
if (resourceDef != null) { if (resourceDef != null) {
RuntimeSearchParam searchParamDef = resourceDef.getSearchParam(paramName); RuntimeSearchParam searchParamDef = resourceDef.getSearchParam(paramName);
if (searchParamDef!=null) { if (searchParamDef!=null) {
if (searchParamDef.getPathsSplit().contains(resourceDef.getName() + "." + myName)) { if (searchParamDef.getPathsSplit().contains(myOwningResource + "." + myName)) {
return true; return true;
} }
} }

View File

@ -28,153 +28,156 @@ import ca.uhn.fhir.rest.server.BundleInclusionRule;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
public class Dstu2BundleFactoryTest { public class Dstu2BundleFactoryTest {
private static FhirContext ourCtx; private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Dstu2BundleFactoryTest.class);
private List<IBaseResource> myResourceList;
private Dstu2BundleFactory myBundleFactory;
@BeforeClass private static FhirContext ourCtx;
public static void beforeClass() throws Exception { private List<IBaseResource> myResourceList;
ourCtx = FhirContext.forDstu2(); private Dstu2BundleFactory myBundleFactory;
}
@AfterClass @BeforeClass
public static void afterClassClearContext() { public static void beforeClass() throws Exception {
TestUtil.clearAllStaticFieldsForUnitTest(); ourCtx = FhirContext.forDstu2();
} }
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// DiagnosticReport // DiagnosticReport
// Performer(practitioner) // Performer(practitioner)
// Subject(patient) // Subject(patient)
// Result(observation) // Result(observation)
// Specimen(specimen1) // Specimen(specimen1)
// Subject(patient) // Subject(patient)
// Collector(practitioner) // Collector(practitioner)
// Subject(patient) // Subject(patient)
// Performer(practitioner) // Performer(practitioner)
// Specimen(specimen2) // Specimen(specimen2)
DiagnosticReport diagnosticReport = new DiagnosticReport(); DiagnosticReport diagnosticReport = new DiagnosticReport();
diagnosticReport.setId("DiagnosticReport/1"); diagnosticReport.setId("DiagnosticReport/1");
Observation observation = new Observation(); Observation observation = new Observation();
observation.setId("Observation/1"); observation.setId("Observation/1");
Specimen specimen1 = new Specimen(); Specimen specimen1 = new Specimen();
specimen1.setId("Specimen/1"); specimen1.setId("Specimen/1");
Specimen specimen2 = new Specimen(); Specimen specimen2 = new Specimen();
specimen2.setId("Specimen/2"); specimen2.setId("Specimen/2");
Practitioner practitioner = new Practitioner(); Practitioner practitioner = new Practitioner();
practitioner.setId("Practitioner/1"); practitioner.setId("Practitioner/1");
Patient patient = new Patient(); Patient patient = new Patient();
patient.setId("Patient/1"); patient.setId("Patient/1");
diagnosticReport.setPerformer(new ResourceReferenceDt(practitioner)); diagnosticReport.setPerformer(new ResourceReferenceDt(practitioner));
diagnosticReport.setSubject(new ResourceReferenceDt(patient)); diagnosticReport.setSubject(new ResourceReferenceDt(patient));
diagnosticReport.setResult(Arrays.asList(new ResourceReferenceDt[]{new ResourceReferenceDt(observation)})); diagnosticReport.setResult(Arrays.asList(new ResourceReferenceDt[] { new ResourceReferenceDt(observation) }));
diagnosticReport.setSpecimen(Arrays.asList(new ResourceReferenceDt(specimen2))); diagnosticReport.setSpecimen(Arrays.asList(new ResourceReferenceDt(specimen2)));
observation.setSpecimen(new ResourceReferenceDt(specimen1)); observation.setSpecimen(new ResourceReferenceDt(specimen1));
observation.setSubject(new ResourceReferenceDt(patient)); observation.setSubject(new ResourceReferenceDt(patient));
observation.setPerformer(Arrays.asList(new ResourceReferenceDt[]{new ResourceReferenceDt(practitioner)})); observation.setPerformer(Arrays.asList(new ResourceReferenceDt[] { new ResourceReferenceDt(practitioner) }));
specimen1.setSubject(new ResourceReferenceDt(patient)); specimen1.setSubject(new ResourceReferenceDt(patient));
specimen1.getCollection().setCollector(new ResourceReferenceDt(practitioner)); specimen1.getCollection().setCollector(new ResourceReferenceDt(practitioner));
myResourceList = Arrays.asList(new IBaseResource[]{diagnosticReport}); myResourceList = Arrays.asList(new IBaseResource[] { diagnosticReport });
myBundleFactory = new Dstu2BundleFactory(ourCtx); myBundleFactory = new Dstu2BundleFactory(ourCtx);
} }
@Test @Test
public void whenIncludeIsAsterisk_bundle_shouldContainAllReferencedResources() throws Exception { public void whenIncludeIsAsterisk_bundle_shouldContainAllReferencedResources() throws Exception {
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(new String("*"))); Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(new String("*")));
assertEquals(6, bundle.getEntry().size()); assertEquals(6, bundle.getEntry().size());
assertEquals(2, numberOfEntriesOfType(bundle, Specimen.class)); assertEquals(2, numberOfEntriesOfType(bundle, Specimen.class));
} }
@Test @Test
public void whenIncludeIsNull_bundle_shouldOnlyContainPrimaryResource() throws Exception { public void whenIncludeIsNull_bundle_shouldOnlyContainPrimaryResource() throws Exception {
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, null); Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, null);
assertEquals(1, bundle.getEntry().size()); assertEquals(1, bundle.getEntry().size());
assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class)); assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class));
} }
@Test @Test
public void whenIncludeIsDiagnosticReportSubject_bundle_shouldIncludePatient() throws Exception { public void whenIncludeIsDiagnosticReportSubject_bundle_shouldIncludePatient() throws Exception {
Set<Include> includes = includes(DiagnosticReport.INCLUDE_SUBJECT.getValue()); Set<Include> includes = includes(DiagnosticReport.INCLUDE_SUBJECT.getValue());
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes); Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes);
assertEquals(2, bundle.getEntry().size()); assertEquals(2, bundle.getEntry().size());
assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class)); assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class));
assertEquals(1, numberOfEntriesOfType(bundle, Patient.class)); assertEquals(1, numberOfEntriesOfType(bundle, Patient.class));
} }
@Test
public void whenAChainedResourceIsIncludedAndItsParentIsAlsoIncluded_bundle_shouldContainTheChainedResource() throws Exception {
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(DiagnosticReport.INCLUDE_RESULT.getValue(), Observation.INCLUDE_SPECIMEN.getValue()));
@Test ourLog.info(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(bundle));
public void whenAChainedResourceIsIncludedAndItsParentIsAlsoIncluded_bundle_shouldContainTheChainedResource() throws Exception {
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(DiagnosticReport.INCLUDE_RESULT.getValue(), Observation.INCLUDE_SPECIMEN.getValue())); assertEquals(3, bundle.getEntry().size());
assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class));
assertEquals(1, numberOfEntriesOfType(bundle, Observation.class));
assertEquals(1, numberOfEntriesOfType(bundle, Specimen.class));
List<Specimen> specimens = getResourcesOfType(bundle, Specimen.class);
assertEquals(1, specimens.size());
assertEquals("1", specimens.get(0).getId().getIdPart());
}
assertEquals(3, bundle.getEntry().size()); @Test
assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class)); public void whenAChainedResourceIsIncludedButItsParentIsNot_bundle_shouldNotContainTheChainedResource() throws Exception {
assertEquals(1, numberOfEntriesOfType(bundle, Observation.class)); Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(Observation.INCLUDE_SPECIMEN.getValue()));
assertEquals(1, numberOfEntriesOfType(bundle, Specimen.class));
List<Specimen> specimens = getResourcesOfType(bundle, Specimen.class);
assertEquals(1, specimens.size());
assertEquals("1", specimens.get(0).getId().getIdPart());
}
@Test assertEquals(1, bundle.getEntry().size());
public void whenAChainedResourceIsIncludedButItsParentIsNot_bundle_shouldNotContainTheChainedResource() throws Exception { assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class));
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_INCLUDES, includes(Observation.INCLUDE_SPECIMEN.getValue())); }
assertEquals(1, bundle.getEntry().size()); @Test
assertEquals(1, numberOfEntriesOfType(bundle, DiagnosticReport.class)); public void whenBundleInclusionRuleSetToResourcePresence_bundle_shouldContainAllResources() throws Exception {
} Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_RESOURCE_PRESENCE, null);
@Test assertEquals(6, bundle.getEntry().size());
public void whenBundleInclusionRuleSetToResourcePresence_bundle_shouldContainAllResources() throws Exception { assertEquals(2, numberOfEntriesOfType(bundle, Specimen.class));
Bundle bundle = makeBundle(BundleInclusionRule.BASED_ON_RESOURCE_PRESENCE, null); }
assertEquals(6, bundle.getEntry().size()); Bundle makeBundle(BundleInclusionRule theBundleInclusionRule, Set<Include> theIncludes) {
assertEquals(2, numberOfEntriesOfType(bundle, Specimen.class)); myBundleFactory.addResourcesToBundle(myResourceList, null, "http://foo", theBundleInclusionRule, theIncludes);
} return (Bundle) myBundleFactory.getResourceBundle();
}
Bundle makeBundle(BundleInclusionRule theBundleInclusionRule, Set<Include> theIncludes) { private Set<Include> includes(String... includes) {
myBundleFactory.addResourcesToBundle(myResourceList, null, "http://foo", theBundleInclusionRule, theIncludes); Set<Include> includeSet = new HashSet<Include>();
return (Bundle) myBundleFactory.getResourceBundle(); for (String include : includes) {
} includeSet.add(new Include(include));
}
return includeSet;
}
private Set<Include> includes(String... includes) { private <T extends IResource> int numberOfEntriesOfType(Bundle theBundle, Class<T> theResourceClass) {
Set<Include> includeSet = new HashSet<Include>(); int count = 0;
for (String include : includes) { for (Bundle.Entry entry : theBundle.getEntry()) {
includeSet.add(new Include(include)); if (theResourceClass.isAssignableFrom(entry.getResource().getClass()))
} count++;
return includeSet; }
} return count;
}
private <T extends IResource> int numberOfEntriesOfType(Bundle theBundle, Class<T> theResourceClass) { @SuppressWarnings("unchecked")
int count = 0;
for (Bundle.Entry entry : theBundle.getEntry()) {
if (theResourceClass.isAssignableFrom(entry.getResource().getClass()))
count++;
}
return count;
}
@SuppressWarnings("unchecked")
private <T extends IResource> List<T> getResourcesOfType(Bundle theBundle, Class<T> theResourceClass) { private <T extends IResource> List<T> getResourcesOfType(Bundle theBundle, Class<T> theResourceClass) {
List<T> resources = new ArrayList<T>(); List<T> resources = new ArrayList<T>();
for (Bundle.Entry entry : theBundle.getEntry()) { for (Bundle.Entry entry : theBundle.getEntry()) {
if (theResourceClass.isAssignableFrom(entry.getResource().getClass())) { if (theResourceClass.isAssignableFrom(entry.getResource().getClass())) {
resources.add((T) entry.getResource()); resources.add((T) entry.getResource());
} }
} }
return resources; return resources;
} }
} }