Skip to content

Commit

Permalink
FM2-416: Added integration tests for instance level $everything on Pa…
Browse files Browse the repository at this point in the history
…tients (#362)
  • Loading branch information
Medhavi-16 authored Jul 26, 2021
1 parent faab665 commit ccfbefa
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,55 @@ public void describeTo(Description description) {
.appendValue(max).appendText(" distinct observation times");
}
}

protected static Matcher<List<Bundle.BundleEntryComponent>> hasCorrectResources(Integer resourceCount,
Set<String> validResources) {
return new HasCorrectResources(resourceCount, validResources);
}

private static class HasCorrectResources extends TypeSafeDiagnosingMatcher<List<Bundle.BundleEntryComponent>> {

private int resourcesCount;

private Set<String> validResources;

HasCorrectResources(int resourcesCount, Set<String> validResources) {
this.resourcesCount = resourcesCount;
this.validResources = validResources;
}

@Override
protected boolean matchesSafely(List<Bundle.BundleEntryComponent> 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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Bundle.BundleEntryComponent> 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<Bundle.BundleEntryComponent> 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<Bundle.BundleEntryComponent> 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<Bundle.BundleEntryComponent> entries = result.getEntry();

assertThat(entries, everyItem(hasProperty("fullUrl", startsWith("http://localhost/ws/fhir2/R3/"))));

assertThat(entries, hasCorrectResources(5, getValidResources()));
}

private Set<String> getValidResources() {
Set<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,55 @@ public void describeTo(Description description) {
.appendValue(max).appendText(" distinct observation times");
}
}

protected static Matcher<List<Bundle.BundleEntryComponent>> hasCorrectResources(Integer resourceCount,
Set<String> validResources) {
return new HasCorrectResources(resourceCount, validResources);
}

private static class HasCorrectResources extends TypeSafeDiagnosingMatcher<List<Bundle.BundleEntryComponent>> {

private int resourcesCount;

private Set<String> validResources;

HasCorrectResources(int resourcesCount, Set<String> validResources) {
this.resourcesCount = resourcesCount;
this.validResources = validResources;
}

@Override
protected boolean matchesSafely(List<Bundle.BundleEntryComponent> 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.");
}
}
}
Loading

0 comments on commit ccfbefa

Please sign in to comment.