diff --git a/exec/pom.xml b/exec/pom.xml index 94bbc95b..0b8ca535 100755 --- a/exec/pom.xml +++ b/exec/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 2.2.6 + 2.2.7 exec @@ -70,7 +70,7 @@ org.smartregister plugins - 2.2.6 + 2.2.7 diff --git a/plugins/pom.xml b/plugins/pom.xml index 1d95e9f6..0825cd1e 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 2.2.6 + 2.2.7 plugins @@ -45,7 +45,7 @@ org.smartregister fhir-common-utils - 1.0.1-SNAPSHOT + 1.0.2-SNAPSHOT compile diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java index d67eb89c..f5f746bd 100755 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java @@ -21,10 +21,12 @@ import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.ListResource; +import org.hl7.fhir.r4.model.Location; import org.hl7.fhir.r4.model.OperationOutcome; import org.hl7.fhir.r4.model.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.smartregister.helpers.LocationHelper; import com.google.common.annotations.VisibleForTesting; import com.google.fhir.gateway.ExceptionUtil; @@ -247,9 +249,35 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle); } + if (Constants.SyncStrategy.LOCATION.equals(request.getResourceName()) + && ("POST".equals(request.getRequestType().name()) + || "PUT".equals(request.getRequestType().name()))) { + resultContent = new BasicResponseHandler().handleResponse(response); + String requestPath = request.getRequestPath(); + String locationId = getLocationId(requestPath, resultContent); + if (StringUtils.isNotBlank(locationId)) { + Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); + resultContent = this.fhirR4JsonParser.encodeResourceToString(location); + } + } + return resultContent; } + @VisibleForTesting + protected String getLocationId(String requestPath, String resultContent) { + String locationId; + IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); + if (parsedResource instanceof Location) { + return ((Location) parsedResource).getIdElement().getIdPart(); + } + + String[] pathParts = requestPath.split("/"); + locationId = pathParts[pathParts.length - 1]; + + return locationId; + } + private IBaseResource processRelatedEntityLocationSyncStrategy( RequestDetailsReader request, HttpResponse response) throws IOException { String resultContent; diff --git a/plugins/src/test/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecisionTest.java b/plugins/src/test/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecisionTest.java index c3c7ef60..f0283061 100755 --- a/plugins/src/test/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecisionTest.java +++ b/plugins/src/test/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecisionTest.java @@ -1,6 +1,7 @@ package org.smartregister.fhir.gateway.plugins; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import java.io.IOException; @@ -20,6 +21,8 @@ import org.apache.http.HttpResponse; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.ListResource; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.Patient; import org.jetbrains.annotations.NotNull; import org.junit.After; import org.junit.Assert; @@ -30,6 +33,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.smartregister.helpers.LocationHelper; import com.google.common.collect.Maps; import com.google.common.io.Resources; @@ -997,6 +1001,74 @@ public void testPostProcessWithListModeHeaderSearchByTagPaginateEntriesBundle() resultContent); } + @Test + public void testPostProcessCallsUpdateLocationLineage() throws IOException { + testInstance = + Mockito.spy(createSyncAccessDecisionTestInstance(Constants.SyncStrategy.LOCATION)); + FhirContext fhirR4Context = mock(FhirContext.class); + IGenericClient iGenericClient = mock(IGenericClient.class); + + testInstance.setFhirR4Context(fhirR4Context); + testInstance.setFhirR4Client(iGenericClient); + + RequestDetailsReader requestDetailsSpy = Mockito.mock(RequestDetailsReader.class); + RequestTypeEnum requestTypeEnumMock = Mockito.mock(RequestTypeEnum.class); + + Mockito.when(requestDetailsSpy.getRequestPath()).thenReturn("Location/123"); + Mockito.when(requestDetailsSpy.getRequestType()).thenReturn(requestTypeEnumMock); + Mockito.when(requestTypeEnumMock.name()).thenReturn("POST"); + Mockito.when(requestDetailsSpy.getResourceName()) + .thenReturn(Constants.SyncStrategy.LOCATION); + + Location location = new Location(); + location.setId("Location/123"); + + String responseJson = FhirContext.forR4().newJsonParser().encodeResourceToString(location); + HttpResponse fhirResponseMock = + Mockito.mock(HttpResponse.class, Answers.RETURNS_DEEP_STUBS); + TestUtil.setUpFhirResponseMock(fhirResponseMock, responseJson); + Mockito.doReturn("123") + .when(testInstance) + .getLocationId(Mockito.anyString(), Mockito.any()); + + try (MockedStatic locationHelperMock = + Mockito.mockStatic(LocationHelper.class)) { + locationHelperMock + .when(() -> LocationHelper.updateLocationLineage(any(), any())) + .thenReturn(location); + + testInstance.postProcess(requestDetailsSpy, fhirResponseMock); + locationHelperMock.verify( + () -> LocationHelper.updateLocationLineage(eq(iGenericClient), eq("123"))); + } + } + + @Test + public void testGetLocationId() { + String requestPath = "Location/123"; + testInstance = + Mockito.spy(createSyncAccessDecisionTestInstance(Constants.SyncStrategy.LOCATION)); + FhirContext fhirR4Context = mock(FhirContext.class); + IGenericClient iGenericClient = mock(IGenericClient.class); + + testInstance.setFhirR4Context(fhirR4Context); + testInstance.setFhirR4Client(iGenericClient); + + Location location = new Location(); + location.setId("Location/123"); + String validJson = FhirContext.forR4().newJsonParser().encodeResourceToString(location); + + String locationId = testInstance.getLocationId(requestPath, validJson); + Assert.assertEquals("123", locationId); + + Patient patient = new Patient(); + patient.setId("Patient/345"); + String validPatientJson = + FhirContext.forR4().newJsonParser().encodeResourceToString(patient); + String locId = testInstance.getLocationId(requestPath, validPatientJson); + Assert.assertEquals("123", locId); + } + @Test public void preProcessWhenRequestIsAnOperationRequestShouldAddFilters() { userRoles.add(Constants.ROLE_ANDROID_CLIENT); diff --git a/pom.xml b/pom.xml index b23bd810..4f9a67d1 100755 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.smartregister opensrp-gateway-plugin - 2.2.6 + 2.2.7 pom