Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-429 : Added support for Instance level $everything operation in R3 #379

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -32,6 +34,7 @@
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
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 @@ -158,4 +161,21 @@ public IBundleProvider searchEncounter(@OptionalParam(name = Encounter.SP_DATE)
participantReference, subjectReference, encounterType, id, lastUpdated, includes, revIncludes));
}

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

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

TokenParam encounterReference = new TokenParam().setValue(encounterId.getIdPart());

return new SearchQueryBundleProviderR3Wrapper(encounterService.getEncounterEverything(encounterReference));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -333,4 +334,41 @@ public void deleteEncounter_shouldThrowResourceNotFoundExceptionWhenIdRefersToNo
when(encounterService.delete(WRONG_ENCOUNTER_UUID)).thenReturn(null);
resourceProvider.deleteEncounter(new IdType().setValue(WRONG_ENCOUNTER_UUID));
}

@Test
public void getEncounterEverything_shouldReturnEncounterEverything() {
when(encounterService.getEncounterEverything(any()))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(encounter), 10, 1));

IBundleProvider results = resourceProvider.getEncounterEverything(new IdType(ENCOUNTER_UUID));

List<IBaseResource> resultList = getAllResources(results);

assertThat(resultList, notNullValue());
assertThat(resultList.size(), equalTo(1));
assertThat(resultList.get(0).fhirType(), equalTo(FhirConstants.ENCOUNTER));
assertThat(((Encounter) resultList.iterator().next()).getId(), equalTo(ENCOUNTER_UUID));
}

@Test
public void getEncounterEverything_shouldReturnNullForEncounterEverythingWhenIdParamIsMissing() {
IBundleProvider results = resourceProvider.getEncounterEverything(null);
assertThat(results, nullValue());
}

@Test
public void getEncounterEverything_shouldReturnNullForEncounterEverythingWhenIdPartIsMissingInIdParam() {
IBundleProvider results = resourceProvider.getEncounterEverything(new IdType());
assertThat(results, nullValue());
}

@Test
public void getEncounterEverything_shouldReturnNullEncounterEverythingWhenIdPartIsEmptyInIdParam() {
IBundleProvider results = resourceProvider.getEncounterEverything(new IdType(""));
assertThat(results, nullValue());
}

private List<IBaseResource> getAllResources(IBundleProvider result) {
return result.getAllResources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import ca.uhn.fhir.rest.param.ReferenceOrListParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
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;
Expand Down Expand Up @@ -132,6 +133,9 @@ public class EncounterFhirResourceProviderWebTest extends BaseFhirR3ResourceProv
@Captor
private ArgumentCaptor<HashSet<Include>> includeArgumentCaptor;

@Captor
private ArgumentCaptor<TokenParam> tokenCaptor;

@Before
@Override
public void setup() throws ServletException {
Expand Down Expand Up @@ -871,4 +875,33 @@ public void deleteEncounter_shouldReturn404ForNonExistingEncounter() throws Exce

assertThat(response, isNotFound());
}

@Test
public void getEncounterEverything_shouldHandleEncounterId() throws Exception {
verifyEverythingOperation("/Encounter/" + ENCOUNTER_UUID + "/$everything?");

verify(encounterService).getEncounterEverything(tokenCaptor.capture());

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

private void verifyEverythingOperation(String uri) throws Exception {
Encounter encounter = new Encounter();
encounter.setId(ENCOUNTER_UUID);

when(encounterService.getEncounterEverything(any()))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(encounter), 10, 1));

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

assertThat(response, isOk());
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(ENCOUNTER_UUID));
}
}