diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java index a3f9bd525..7c52b0538 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java @@ -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; @@ -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; @@ -156,5 +159,22 @@ public IBundleProvider searchEncounter(@OptionalParam(name = Encounter.SP_DATE) return new SearchQueryBundleProviderR3Wrapper(encounterService.searchForEncounters(date, location, participantReference, subjectReference, id, lastUpdated, includes, revIncludes)); } - + + /** + * The $everything operation fetches all the information related 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)); + } } diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java index 8fa8a5d42..e033ec192 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java @@ -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; @@ -331,4 +332,37 @@ public void deleteEncounter_shouldThrowResourceNotFoundExceptionWhenIdRefersToNo when(encounterService.delete(WRONG_ENCOUNTER_UUID)).thenReturn(null); resourceProvider.deleteEncounter(new IdType().setValue(WRONG_ENCOUNTER_UUID)); } + + @Test + public void searchForEncounters_shouldReturnEncounterEverything() { + when(encounterService.getEncounterEverything(any())) + .thenReturn(new MockIBundleProvider<>(Collections.singletonList(encounter), 10, 1)); + + IBundleProvider results = resourceProvider.getEncounterEverything(new IdType(ENCOUNTER_UUID)); + + List resultList = results.getAllResources(); + + 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 searchForEncounters_shouldReturnNullForEncounterEverythingWhenIdParamIsMissing() { + IBundleProvider results = resourceProvider.getEncounterEverything(null); + assertThat(results, nullValue()); + } + + @Test + public void searchForEncounters_shouldReturnNullForEncounterEverythingWhenIdPartIsMissingInIdParam() { + IBundleProvider results = resourceProvider.getEncounterEverything(new IdType()); + assertThat(results, nullValue()); + } + + @Test + public void searchForEncounters_shouldReturnNullEncounterEverythingWhenIdPartIsEmptyInIdParam() { + IBundleProvider results = resourceProvider.getEncounterEverything(new IdType("")); + assertThat(results, nullValue()); + } }