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

Add support for location lineage post processing #119

Merged
merged 14 commits into from
Feb 5, 2025
4 changes: 2 additions & 2 deletions exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>
</parent>

<artifactId>exec</artifactId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>org.smartregister</groupId>
<artifactId>plugins</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>
</parent>

<artifactId>plugins</artifactId>
Expand Down Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.smartregister</groupId>
<artifactId>fhir-common-utils</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<LocationHelper> 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);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.2.6</version>
<version>2.2.7</version>
<packaging>pom</packaging>

<modules>
Expand Down