Skip to content

Commit

Permalink
FM2-412: Support for instance level $everything operation on Patients…
Browse files Browse the repository at this point in the history
… in R3 (#361)
  • Loading branch information
Medhavi-16 authored Jul 23, 2021
1 parent 4be5f0e commit faab665
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import java.util.List;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.model.valueset.BundleTypeEnum;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.Delete;
import ca.uhn.fhir.rest.annotation.History;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.IncludeParam;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
Expand All @@ -34,6 +36,7 @@
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
Expand Down Expand Up @@ -150,4 +153,22 @@ public IBundleProvider searchPatients(@OptionalParam(name = Patient.SP_NAME) Str
gender, birthDate, deathDate, deceased, city, state, postalCode, country, id, lastUpdated, sort, revIncludes));
}

/**
* The $everything operation fetches all the information related the specified patient
*
* @param patientId The id of the patient
* @return a bundle of resources which reference to or are referenced from the patient
*/
@Operation(name = "everything", idempotent = true, type = Patient.class, bundleType = BundleTypeEnum.SEARCHSET)
public IBundleProvider getPatientEverything(@IdParam IdType patientId) {

if (patientId == null || patientId.getIdPart() == null || patientId.getIdPart().isEmpty()) {
return null;
}

TokenParam patientReference = new TokenParam().setValue(patientId.getIdPart());

return new SearchQueryBundleProviderR3Wrapper(patientService.getPatientEverything(patientReference));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,46 @@ public void searchForPatients_shouldNotAddResourcesForEmptyRevInclude() {
assertThat(((Patient) resultList.iterator().next()).getId(), equalTo(PATIENT_UUID));
}

@Test
public void searchForPatients_shouldReturnPatientEverything() {
when(patientService.getPatientEverything(any()))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(patient), 10, 1));

IBundleProvider results = patientFhirResourceProvider.getPatientEverything(new IdType(PATIENT_UUID));

List<IBaseResource> resultList = getAllResources(results);

assertThat(resultList, notNullValue());
assertThat(resultList.size(), equalTo(1));
assertThat(resultList.get(0).fhirType(), equalTo(FhirConstants.PATIENT));
assertThat(((Patient) resultList.iterator().next()).getId(), equalTo(PATIENT_UUID));
}

@Test
public void searchForPatients_shouldReturnNullForPatientEverythingWhenIdParamIsMissing() {
IBundleProvider results = patientFhirResourceProvider.getPatientEverything(null);

assertThat(results, nullValue());
}

@Test
public void searchForPatients_shouldReturnNullForPatientEverythingWhenIdPartIsMissingInIdParam() {
IBundleProvider results = patientFhirResourceProvider.getPatientEverything(new IdType());

assertThat(results, nullValue());
}

@Test
public void searchForPatients_shouldReturnNullPatientEverythingWhenIdPartIsEmptyInIdParam() {
IBundleProvider results = patientFhirResourceProvider.getPatientEverything(new IdType(""));

assertThat(results, nullValue());
}

private List<IBaseResource> getAllResources(IBundleProvider result) {
return result.getAllResources();
}

private List<IBaseResource> getResources(IBundleProvider result) {
return result.getResources(0, 10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.hamcrest.MatcherAssert;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.OperationOutcome;
Expand Down Expand Up @@ -104,6 +106,9 @@ public class PatientFhirR3ResourceProviderWebTest extends BaseFhirR3ResourceProv

@Captor
private ArgumentCaptor<HashSet<Include>> includeArgumentCaptor;

@Captor
private ArgumentCaptor<TokenParam> tokenCaptor;

@Override
@Before
Expand Down Expand Up @@ -772,5 +777,34 @@ public void deletePatient_shouldDeletePatient() throws Exception {
assertThat(response, isOk());
assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}

@Test
public void getPatientEverything_shouldHandlePatientId() throws Exception {
verifyEverythingOperation("/Patient/" + PATIENT_UUID + "/$everything?");

verify(patientService).getPatientEverything(tokenCaptor.capture());

assertThat(tokenCaptor.getValue(), notNullValue());
assertThat(tokenCaptor.getValue().getValue(), equalTo(PATIENT_UUID));
}

private void verifyEverythingOperation(String uri) throws Exception {
Patient patient = new Patient();
patient.setId(PATIENT_UUID);

when(patientService.getPatientEverything(any()))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(patient), 10, 1));

MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();

MatcherAssert.assertThat(response, isOk());
MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));

Bundle results = readBundleResponse(response);
assertThat(results.getEntry(), notNullValue());
assertThat(results.getEntry(), not(empty()));
assertThat(results.getEntry().get(0).getResource(), notNullValue());
assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(PATIENT_UUID));
}

}

0 comments on commit faab665

Please sign in to comment.