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
2 changes: 1 addition & 1 deletion plugins/pom.xml
Original file line number Diff line number Diff line change
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,41 @@ public String postProcess(RequestDetailsReader request, HttpResponse response)
resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle);
}

String resourceType = request.getResourceName();

if (Constants.SyncStrategy.LOCATION.equals(resourceType)) {
String method = request.getRequestType().name();
if ("POST".equals(method) || "PUT".equals(method)) {
lincmba marked this conversation as resolved.
Show resolved Hide resolved
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;
if (StringUtils.isNotBlank(resultContent)) {
IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent);
if (parsedResource instanceof Location) {
return ((Location) parsedResource).getIdElement().getIdPart();
}
}

String[] pathParts = requestPath.split("/");
locationId = pathParts[pathParts.length - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these two conditions are primarily for differentiating between a PUT and POST I'd recommend passing the request Method (PUT|POST) as a parameter and using an if else statement for more clarity

Copy link
Contributor Author

@lincmba lincmba Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're not for differentiating the request method. They are there incase a request is made but the resource updated/created is not returned in the json or isn't a valid location resource. Done away with one condition though


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,7 @@
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.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -30,6 +32,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 +1000,76 @@ 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")));

Assert.assertNotNull(location);
Assert.assertEquals("Location/123", location.getId());
lincmba marked this conversation as resolved.
Show resolved Hide resolved
}
}

@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);

locationId = testInstance.getLocationId(requestPath, null);
Assert.assertEquals("123", locationId);

locationId = testInstance.getLocationId(requestPath, "");
Assert.assertEquals("123", locationId);
}

@Test
public void preProcessWhenRequestIsAnOperationRequestShouldAddFilters() {
userRoles.add(Constants.ROLE_ANDROID_CLIENT);
Expand Down