From ccfbefa41078f5d1fdc5e78ca33441de0e8f692e Mon Sep 17 00:00:00 2001 From: Medhavi Srivastava <43985601+Medhavi-16@users.noreply.github.com> Date: Tue, 27 Jul 2021 00:00:37 +0530 Subject: [PATCH] FM2-416: Added integration tests for instance level $everything on Patients (#362) --- .../r3/BaseFhirR3IntegrationTest.java | 51 ++++++ ...ntFhirResourceProviderIntegrationTest.java | 146 +++++++++++++++++ .../r4/BaseFhirR4IntegrationTest.java | 51 ++++++ ...ntFhirResourceProviderIntegrationTest.java | 147 ++++++++++++++++++ 4 files changed, 395 insertions(+) diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3IntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3IntegrationTest.java index 2ec935155..4538944a4 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3IntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3IntegrationTest.java @@ -215,4 +215,55 @@ public void describeTo(Description description) { .appendValue(max).appendText(" distinct observation times"); } } + + protected static Matcher> hasCorrectResources(Integer resourceCount, + Set validResources) { + return new HasCorrectResources(resourceCount, validResources); + } + + private static class HasCorrectResources extends TypeSafeDiagnosingMatcher> { + + private int resourcesCount; + + private Set validResources; + + HasCorrectResources(int resourcesCount, Set validResources) { + this.resourcesCount = resourcesCount; + this.validResources = validResources; + } + + @Override + protected boolean matchesSafely(List entries, Description mismatchDescription) { + int count = 0; + for (Bundle.BundleEntryComponent entry : entries) { + if (validResources.contains(entry.getResource().getIdElement().getIdPart())) { + count++; + } else { + mismatchDescription.appendText("Result contains an incorrect resource"); + return false; + } + } + + if (entries.size() < resourcesCount) { + if (count != entries.size()) { + mismatchDescription.appendText("Expected ").appendValue(entries.size()) + .appendText(" resources, but result has ").appendValue(count).appendText(" resources."); + return false; + } + return true; + } + + if (count != resourcesCount) { + mismatchDescription.appendText("Expected ").appendValue(resourcesCount) + .appendText(" resources, but result has ").appendValue(count).appendText(" resources."); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("Result all valid resources."); + } + } } diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProviderIntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProviderIntegrationTest.java index 2c579ac71..0c3070f6a 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProviderIntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProviderIntegrationTest.java @@ -16,6 +16,7 @@ import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -27,8 +28,10 @@ import java.time.ZoneId; import java.util.Calendar; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import lombok.AccessLevel; import lombok.Getter; @@ -57,6 +60,36 @@ public class PatientFhirResourceProviderIntegrationTest extends BaseFhirR3Integr private static final String PATIENT_UUID = "30e2aa2a-4ed1-415d-84c5-ba29016c14b7"; private static final String WRONG_PATIENT_UUID = "f090747b-459b-4a13-8c1b-c0567d8aeb63"; + + private static final String PATIENT_UUID_2 = "ca17fcc5-ec96-487f-b9ea-42973c8973e3"; + + private static final String OBSERVATION_UUID_1 = "99b92980-db62-40cd-8bca-733357c48126"; + + private static final String OBSERVATION_UUID_2 = "f6ec1267-8eac-415f-a3f0-e47be2c8bb67"; + + private static final String OBSERVATION_UUID_3 = "be48cdcb-6a76-47e3-9f2e-2635032f3a9a"; + + private static final String OBSERVATION_UUID_4 = "1ce473c8-3fac-440d-9f92-e10facab194f"; + + private static final String OBSERVATION_UUID_5 = "b6521c32-47b6-47da-9c6f-3673ddfb74f9"; + + private static final String OBSERVATION_UUID_6 = "2ed1e57d-9f18-41d3-b067-2eeaf4b30fb0"; + + private static final String OBSERVATION_UUID_7 = "2f616900-5e7c-4667-9a7f-dcb260abf1de"; + + private static final String OBSERVATION_UUID_8 = "39fb7f47-e80a-4056-9285-bd798be13c63"; + + private static final String OBSERVATION_UUID_9 = "e26cea2c-1b9f-4afe-b211-f3ef6c88af6f"; + + private static final String ENCOUNTER_UUID_1 = "e403fafb-e5e4-42d0-9d11-4f52e89d148c"; + + private static final String ENCOUNTER_UUID_2 = "6519d653-393b-4118-9c83-a3715b82d4ac"; + + private static final String ENCOUNTER_UUID_3 = "eec646cb-c847-45a7-98bc-91c8c4f70add"; + + private static final String MEDICATION_REQUEST_UUID_1 = "e1f95924-697a-11e3-bd76-0800271c1b75"; + + private static final String MEDICATION_REQUEST_UUID_2 = "921de0a3-05c4-444a-be03-e01b4c4b9142"; @Getter(AccessLevel.PUBLIC) @Autowired @@ -516,4 +549,117 @@ public void shouldReturnCountForPatientAsXml() throws Exception { assertThat(result, hasProperty("total", equalTo(3))); } + + @Test + public void shouldReturnPatientEverythingAsJson() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything") + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(15)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R3/")))); + + assertThat(entries, hasCorrectResources(15, getValidResources())); + } + + @Test + public void shouldReturnForPatientEverythingWhenCountIsSpecifiedAsJson() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything?_count=5") + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(5)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R3/")))); + + assertThat(entries, hasCorrectResources(5, getValidResources())); + } + + @Test + public void shouldReturnPatientEverythingAsXml() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything") + .accept(FhirMediaTypes.XML).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(15)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R3/")))); + + assertThat(entries, hasCorrectResources(15, getValidResources())); + } + + @Test + public void shouldReturnForPatientEverythingWhenCountIsSpecifiedAsXml() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything?_count=5") + .accept(FhirMediaTypes.XML).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(5)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R3/")))); + + assertThat(entries, hasCorrectResources(5, getValidResources())); + } + + private Set getValidResources() { + Set validResources = new HashSet<>(); + validResources.add(PATIENT_UUID_2); + validResources.add(OBSERVATION_UUID_1); + validResources.add(OBSERVATION_UUID_2); + validResources.add(OBSERVATION_UUID_3); + validResources.add(OBSERVATION_UUID_4); + validResources.add(OBSERVATION_UUID_5); + validResources.add(OBSERVATION_UUID_6); + validResources.add(OBSERVATION_UUID_7); + validResources.add(OBSERVATION_UUID_8); + validResources.add(OBSERVATION_UUID_9); + validResources.add(ENCOUNTER_UUID_1); + validResources.add(ENCOUNTER_UUID_2); + validResources.add(ENCOUNTER_UUID_3); + validResources.add(MEDICATION_REQUEST_UUID_1); + validResources.add(MEDICATION_REQUEST_UUID_2); + + return validResources; + } } diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4IntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4IntegrationTest.java index 3b60b6544..ce7dccad6 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4IntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4IntegrationTest.java @@ -221,4 +221,55 @@ public void describeTo(Description description) { .appendValue(max).appendText(" distinct observation times"); } } + + protected static Matcher> hasCorrectResources(Integer resourceCount, + Set validResources) { + return new HasCorrectResources(resourceCount, validResources); + } + + private static class HasCorrectResources extends TypeSafeDiagnosingMatcher> { + + private int resourcesCount; + + private Set validResources; + + HasCorrectResources(int resourcesCount, Set validResources) { + this.resourcesCount = resourcesCount; + this.validResources = validResources; + } + + @Override + protected boolean matchesSafely(List entries, Description mismatchDescription) { + int count = 0; + for (Bundle.BundleEntryComponent entry : entries) { + if (validResources.contains(entry.getResource().getIdElement().getIdPart())) { + count++; + } else { + mismatchDescription.appendText("Result contains an incorrect resource"); + return false; + } + } + + if (entries.size() < resourcesCount) { + if (count != entries.size()) { + mismatchDescription.appendText("Expected ").appendValue(entries.size()) + .appendText(" resources, but result has ").appendValue(count).appendText(" resources."); + return false; + } + return true; + } + + if (count != resourcesCount) { + mismatchDescription.appendText("Expected ").appendValue(resourcesCount) + .appendText(" resources, but result has ").appendValue(count).appendText(" resources."); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("Result all valid resources."); + } + } } diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderIntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderIntegrationTest.java index 2b3f6d016..f42008dbf 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderIntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderIntegrationTest.java @@ -16,6 +16,7 @@ import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -28,8 +29,10 @@ import java.time.ZoneId; import java.util.Calendar; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import lombok.AccessLevel; import lombok.Getter; @@ -59,6 +62,36 @@ public class PatientFhirResourceProviderIntegrationTest extends BaseFhirR4Integr private static final String WRONG_PATIENT_UUID = "f090747b-459b-4a13-8c1b-c0567d8aeb63"; + private static final String PATIENT_UUID_2 = "ca17fcc5-ec96-487f-b9ea-42973c8973e3"; + + private static final String OBSERVATION_UUID_1 = "99b92980-db62-40cd-8bca-733357c48126"; + + private static final String OBSERVATION_UUID_2 = "f6ec1267-8eac-415f-a3f0-e47be2c8bb67"; + + private static final String OBSERVATION_UUID_3 = "be48cdcb-6a76-47e3-9f2e-2635032f3a9a"; + + private static final String OBSERVATION_UUID_4 = "1ce473c8-3fac-440d-9f92-e10facab194f"; + + private static final String OBSERVATION_UUID_5 = "b6521c32-47b6-47da-9c6f-3673ddfb74f9"; + + private static final String OBSERVATION_UUID_6 = "2ed1e57d-9f18-41d3-b067-2eeaf4b30fb0"; + + private static final String OBSERVATION_UUID_7 = "2f616900-5e7c-4667-9a7f-dcb260abf1de"; + + private static final String OBSERVATION_UUID_8 = "39fb7f47-e80a-4056-9285-bd798be13c63"; + + private static final String OBSERVATION_UUID_9 = "e26cea2c-1b9f-4afe-b211-f3ef6c88af6f"; + + private static final String ENCOUNTER_UUID_1 = "e403fafb-e5e4-42d0-9d11-4f52e89d148c"; + + private static final String ENCOUNTER_UUID_2 = "6519d653-393b-4118-9c83-a3715b82d4ac"; + + private static final String ENCOUNTER_UUID_3 = "eec646cb-c847-45a7-98bc-91c8c4f70add"; + + private static final String MEDICATION_REQUEST_UUID_1 = "e1f95924-697a-11e3-bd76-0800271c1b75"; + + private static final String MEDICATION_REQUEST_UUID_2 = "921de0a3-05c4-444a-be03-e01b4c4b9142"; + @Getter(AccessLevel.PUBLIC) @Autowired private PatientFhirResourceProvider resourceProvider; @@ -523,4 +556,118 @@ public void shouldReturnCountForPatientAsXml() throws Exception { assertThat(result, hasProperty("total", equalTo(3))); } + + @Test + public void shouldReturnPatientEverythingAsJson() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything") + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(15)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R4/")))); + + assertThat(entries, hasCorrectResources(15, getValidResources())); + } + + @Test + public void shouldReturnForPatientEverythingWhenCountIsSpecifiedAsJson() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything?_count=5") + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(5)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R4/")))); + + assertThat(entries, hasCorrectResources(5, getValidResources())); + } + + @Test + public void shouldReturnPatientEverythingAsXml() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything") + .accept(FhirMediaTypes.XML).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(15)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R4/")))); + + assertThat(entries, hasCorrectResources(15, getValidResources())); + } + + @Test + public void shouldReturnForPatientEverythingWhenCountIsSpecifiedAsXml() throws Exception { + MockHttpServletResponse response = get("/Patient/ca17fcc5-ec96-487f-b9ea-42973c8973e3/$everything?_count=5") + .accept(FhirMediaTypes.XML).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Bundle result = readBundleResponse(response); + + assertThat(result, notNullValue()); + assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); + assertThat(result, hasProperty("total", equalTo(15))); + assertThat(result.getEntry(), hasSize(5)); + + List entries = result.getEntry(); + + assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R4/")))); + + assertThat(entries, hasCorrectResources(5, getValidResources())); + } + + private Set getValidResources() { + Set validResources = new HashSet<>(); + validResources.add(PATIENT_UUID_2); + validResources.add(OBSERVATION_UUID_1); + validResources.add(OBSERVATION_UUID_2); + validResources.add(OBSERVATION_UUID_3); + validResources.add(OBSERVATION_UUID_4); + validResources.add(OBSERVATION_UUID_5); + validResources.add(OBSERVATION_UUID_6); + validResources.add(OBSERVATION_UUID_7); + validResources.add(OBSERVATION_UUID_8); + validResources.add(OBSERVATION_UUID_9); + validResources.add(ENCOUNTER_UUID_1); + validResources.add(ENCOUNTER_UUID_2); + validResources.add(ENCOUNTER_UUID_3); + validResources.add(MEDICATION_REQUEST_UUID_1); + validResources.add(MEDICATION_REQUEST_UUID_2); + + return validResources; + + } }