From b8760cc0224216a354fbbca7e2eece5db45efc3a Mon Sep 17 00:00:00 2001 From: KGJ-software <39975592+KGJ-software@users.noreply.github.com> Date: Thu, 8 Dec 2022 18:06:37 -0600 Subject: [PATCH] add tests (#4340) * add tests * add tests * clean up test file * device test Co-authored-by: kylejule --- ...entEverythingCompartmentExpansionTest.java | 183 ++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/PatientEverythingCompartmentExpansionTest.java diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/PatientEverythingCompartmentExpansionTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/PatientEverythingCompartmentExpansionTest.java new file mode 100644 index 00000000000..f267e70e109 --- /dev/null +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/PatientEverythingCompartmentExpansionTest.java @@ -0,0 +1,183 @@ +package ca.uhn.fhir.jpa.provider.r4; + +import ca.uhn.fhir.jpa.provider.BaseResourceProviderR4Test; +import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.EncodingEnum; +import com.google.common.base.Charsets; +import org.apache.commons.io.IOUtils; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; +import org.hl7.fhir.r4.model.Device; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.Medication; +import org.hl7.fhir.r4.model.MedicationAdministration; +import org.hl7.fhir.r4.model.Organization; +import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.Practitioner; +import org.hl7.fhir.r4.model.Reference; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class PatientEverythingCompartmentExpansionTest extends BaseResourceProviderR4Test { + + @Test + public void patientEverything_shouldReturnMedication_whenMedicationAdministrationExistsThatRefersToMedicationAndPatient() throws Exception { + + Patient patient = new Patient(); + String patientId = myClient.create().resource(patient).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToPatient = new Reference(); + referenceToPatient.setReference(patientId); + + Medication medication = new Medication(); + String medicationId = myClient.create().resource(medication).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToMedication = new Reference(); + referenceToMedication.setReference(medicationId); + + MedicationAdministration medicationAdministration = new MedicationAdministration(); + medicationAdministration.setSubject(referenceToPatient); + medicationAdministration.setMedication(referenceToMedication); + String medicationAdministrationId = myClient.create().resource(medicationAdministration).execute().getId().toUnqualifiedVersionless().getValue(); + + Bundle bundle = fetchBundle(myServerBase + "/" + patientId + "/$everything?_format=json&_count=100", EncodingEnum.JSON); + + assertNull(bundle.getLink("next")); + + Set actual = new TreeSet<>(); + for (BundleEntryComponent nextEntry : bundle.getEntry()) { + actual.add(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); + } + + assertThat(actual, hasItem(patientId)); + assertThat(actual, hasItem(medicationId)); + assertThat(actual, hasItem(medicationAdministrationId)); + } + + @Test + public void patientEverything_shouldReturnOrganization_whenPatientRefersToItAsManagingOrganization() throws Exception { + + Organization organization = new Organization(); + String organizationId = myClient.create().resource(organization).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToOrganization = new Reference(); + referenceToOrganization.setReference(organizationId); + + Patient patient = new Patient(); + patient.setManagingOrganization(referenceToOrganization); + String patientId = myClient.create().resource(patient).execute().getId().toUnqualifiedVersionless().getValue(); + + Bundle bundle = fetchBundle(myClient.getServerBase() + "/" + patientId + "/$everything?_format=json&_count=100", EncodingEnum.JSON); + + assertNull(bundle.getLink("next")); + + Set actual = new TreeSet<>(); + for (Bundle.BundleEntryComponent nextEntry : bundle.getEntry()) { + actual.add(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); + } + + assertThat(actual, hasItem(patientId)); + assertThat(actual, hasItem(organizationId)); + } + + @Test + public void patientEverything_shouldReturnOrganization_whenPatientRefersToItAsGeneralPractitioner() throws Exception { + + Organization organization = new Organization(); + String organizationId = myClient.create().resource(organization).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToOrganization = new Reference(); + referenceToOrganization.setReference(organizationId); + + Patient patient = new Patient(); + patient.setGeneralPractitioner(List.of(referenceToOrganization)); + String patientId = myClient.create().resource(patient).execute().getId().toUnqualifiedVersionless().getValue(); + + Bundle bundle = fetchBundle(myClient.getServerBase() + "/" + patientId + "/$everything?_format=json&_count=100", EncodingEnum.JSON); + + assertNull(bundle.getLink("next")); + + Set actual = new TreeSet<>(); + for (Bundle.BundleEntryComponent nextEntry : bundle.getEntry()) { + actual.add(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); + } + + assertThat(actual, hasItem(patientId)); + assertThat(actual, hasItem(organizationId)); + } + + @Test + public void patientEverything_shouldReturnPractitioner_whenPatientRefersToItAsGeneralPractitioner() throws Exception { + + Practitioner practitioner = new Practitioner(); + String practitionerId = myClient.create().resource(practitioner).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToPractitioner = new Reference(); + referenceToPractitioner.setReference(practitionerId); + + Patient patient = new Patient(); + patient.setGeneralPractitioner(List.of(referenceToPractitioner)); + String patientId = myClient.create().resource(patient).execute().getId().toUnqualifiedVersionless().getValue(); + + Bundle bundle = fetchBundle(myClient.getServerBase() + "/" + patientId + "/$everything?_format=json&_count=100", EncodingEnum.JSON); + + assertNull(bundle.getLink("next")); + + Set actual = new TreeSet<>(); + for (Bundle.BundleEntryComponent nextEntry : bundle.getEntry()) { + actual.add(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); + } + + assertThat(actual, hasItem(patientId)); + assertThat(actual, hasItem(practitionerId)); + } + + @Test + public void patientEverything_shouldReturnDevice_whenDeviceRefersToPatient() throws Exception { + + Patient patient = new Patient(); + String patientId = myClient.create().resource(patient).execute().getId().toUnqualifiedVersionless().getValue(); + Reference referenceToPatient = new Reference(); + referenceToPatient.setReference(patientId); + + Device device = new Device(); + device.setPatient(referenceToPatient); + String deviceId = myClient.create().resource(device).execute().getId().toUnqualifiedVersionless().getValue(); + + + Bundle bundle = fetchBundle(myClient.getServerBase() + "/" + patientId + "/$everything?_format=json&_count=100", EncodingEnum.JSON); + + assertNull(bundle.getLink("next")); + + Set actual = new TreeSet<>(); + for (Bundle.BundleEntryComponent nextEntry : bundle.getEntry()) { + actual.add(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); + } + + assertThat(actual, hasItem(patientId)); + assertThat(actual, hasItem(deviceId)); + } + + + private Bundle fetchBundle(String theUrl, EncodingEnum theEncoding) throws IOException { + Bundle bundle; + HttpGet get = new HttpGet(theUrl); + CloseableHttpResponse resp = ourHttpClient.execute(get); + try { + assertEquals(theEncoding.getResourceContentTypeNonLegacy(), resp.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue().replaceAll(";.*", "")); + bundle = theEncoding.newParser(myFhirContext).parseResource(Bundle.class, IOUtils.toString(resp.getEntity().getContent(), Charsets.UTF_8)); + } finally { + IOUtils.closeQuietly(resp); + } + + return bundle; + } + +} +