From 227b66e34b4b6a329bdfd78d25e1fb02698e2a1a Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 29 Jan 2025 19:19:24 +0300 Subject: [PATCH 01/14] Add suport for location lineage post processing --- .../gateway/plugins/SyncAccessDecision.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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..e996e15b 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,37 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle); } + String requestPath = request.getRequestPath(); + String method = request.getRequestType().name(); + + if (requestPath.contains("/Location")) { + IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); + + if ("POST".equals(method) || "PUT".equals(method)) { + String locationId = getLocationId(method, requestPath, parsedResource); + + if (StringUtils.isNotBlank(locationId)) { + LocationHelper.updateLocationLineage(locationId); + } + } + } + return resultContent; } + private static String getLocationId( + String method, String requestPath, IBaseResource parsedResource) { + String locationId = null; + + if ("PUT".equals(method)) { + String[] pathParts = requestPath.split("/"); + locationId = pathParts[pathParts.length - 1]; + } else if (parsedResource instanceof Location) { + locationId = ((Location) parsedResource).getIdElement().getIdPart(); + } + return locationId; + } + private IBaseResource processRelatedEntityLocationSyncStrategy( RequestDetailsReader request, HttpResponse response) throws IOException { String resultContent; From 0a7333fca73b48ceb79702ecce4c3eb9a6e4b09d Mon Sep 17 00:00:00 2001 From: lincmba Date: Thu, 30 Jan 2025 16:54:04 +0300 Subject: [PATCH 02/14] Handle response from Helper class Better target to requests to location endpoint --- .../fhir/gateway/plugins/SyncAccessDecision.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 e996e15b..6eff01eb 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 @@ -252,14 +252,14 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) String requestPath = request.getRequestPath(); String method = request.getRequestType().name(); - if (requestPath.contains("/Location")) { + // todo need a better way to target requests to Location endpoint + if (requestPath.contains("Location")) { IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); - if ("POST".equals(method) || "PUT".equals(method)) { String locationId = getLocationId(method, requestPath, parsedResource); - if (StringUtils.isNotBlank(locationId)) { - LocationHelper.updateLocationLineage(locationId); + Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); + resultContent = this.fhirR4JsonParser.encodeResourceToString(location); } } } From cedc6842b194ca94a088addc7025914774fb7f30 Mon Sep 17 00:00:00 2001 From: lincmba Date: Fri, 31 Jan 2025 11:39:23 +0300 Subject: [PATCH 03/14] Better way to target requests to the Location endpoint --- .../fhir/gateway/plugins/SyncAccessDecision.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 6eff01eb..c1d58769 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 @@ -252,13 +252,15 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) String requestPath = request.getRequestPath(); String method = request.getRequestType().name(); - // todo need a better way to target requests to Location endpoint - if (requestPath.contains("Location")) { + String resourceType = request.getResourceName(); + + if ("Location".equals(resourceType)) { IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); if ("POST".equals(method) || "PUT".equals(method)) { String locationId = getLocationId(method, requestPath, parsedResource); if (StringUtils.isNotBlank(locationId)) { - Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); + Location location = + LocationHelper.updateLocationLineage(fhirR4Client, locationId); resultContent = this.fhirR4JsonParser.encodeResourceToString(location); } } From e336a667215f306d9cde907eb28a32275ceddc22 Mon Sep 17 00:00:00 2001 From: lincmba Date: Fri, 31 Jan 2025 11:59:27 +0300 Subject: [PATCH 04/14] Try getting location id from parsed resource first before trying from requestPath --- .../fhir/gateway/plugins/SyncAccessDecision.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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 c1d58769..570a9506 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 @@ -257,7 +257,7 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) if ("Location".equals(resourceType)) { IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); if ("POST".equals(method) || "PUT".equals(method)) { - String locationId = getLocationId(method, requestPath, parsedResource); + String locationId = getLocationId(requestPath, parsedResource); if (StringUtils.isNotBlank(locationId)) { Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); @@ -269,15 +269,14 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) return resultContent; } - private static String getLocationId( - String method, String requestPath, IBaseResource parsedResource) { - String locationId = null; + private static String getLocationId(String requestPath, IBaseResource parsedResource) { - if ("PUT".equals(method)) { + String locationId; + if (parsedResource instanceof Location) { + locationId = ((Location) parsedResource).getIdElement().getIdPart(); + } else { String[] pathParts = requestPath.split("/"); locationId = pathParts[pathParts.length - 1]; - } else if (parsedResource instanceof Location) { - locationId = ((Location) parsedResource).getIdElement().getIdPart(); } return locationId; } From a6a913c60e5c4a8b689ce59e40059444981a6840 Mon Sep 17 00:00:00 2001 From: lincmba Date: Fri, 31 Jan 2025 12:03:06 +0300 Subject: [PATCH 05/14] Remove magic string --- .../smartregister/fhir/gateway/plugins/SyncAccessDecision.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 570a9506..3d569c70 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 @@ -254,7 +254,7 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) String resourceType = request.getResourceName(); - if ("Location".equals(resourceType)) { + if (Constants.SyncStrategy.LOCATION.equals(resourceType)) { IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); if ("POST".equals(method) || "PUT".equals(method)) { String locationId = getLocationId(requestPath, parsedResource); From 6f53ea5828a52e8750524bb1a3bc112f981fd58b Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:01:14 +0300 Subject: [PATCH 06/14] Update commons dependency --- plugins/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/pom.xml b/plugins/pom.xml index 1d95e9f6..06d515c8 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -45,7 +45,7 @@ org.smartregister fhir-common-utils - 1.0.1-SNAPSHOT + 1.0.2-SNAPSHOT compile From 5252c089588ba49597654139a89b0eef0e5e2b16 Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:02:05 +0300 Subject: [PATCH 07/14] Update method to gracefully handle null resultcontent --- .../gateway/plugins/SyncAccessDecision.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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 3d569c70..0a6f09fa 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 @@ -255,9 +255,8 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) String resourceType = request.getResourceName(); if (Constants.SyncStrategy.LOCATION.equals(resourceType)) { - IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); if ("POST".equals(method) || "PUT".equals(method)) { - String locationId = getLocationId(requestPath, parsedResource); + String locationId = getLocationId(requestPath, resultContent); if (StringUtils.isNotBlank(locationId)) { Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); @@ -269,15 +268,20 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) return resultContent; } - private static String getLocationId(String requestPath, IBaseResource parsedResource) { + @VisibleForTesting + protected String getLocationId(String requestPath, String resultContent) { String locationId; - if (parsedResource instanceof Location) { - locationId = ((Location) parsedResource).getIdElement().getIdPart(); - } else { - String[] pathParts = requestPath.split("/"); - locationId = pathParts[pathParts.length - 1]; + 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]; + return locationId; } From 0924b121da4b9ccb8c1a978ca15b2242896e42de Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:02:38 +0300 Subject: [PATCH 08/14] Add updateLocationLineage Tests --- .../plugins/SyncAccessDecisionTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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..5097b805 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,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; @@ -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; @@ -997,6 +1000,51 @@ 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"))); + + Assert.assertNotNull(location); + Assert.assertEquals("Location/123", location.getId()); + } + } + @Test public void preProcessWhenRequestIsAnOperationRequestShouldAddFilters() { userRoles.add(Constants.ROLE_ANDROID_CLIENT); From 0de2cf62b3eb980ab41d049223b2cf8c2c718dfc Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:08:25 +0300 Subject: [PATCH 09/14] Fix failing tests --- .../fhir/gateway/plugins/SyncAccessDecision.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 0a6f09fa..21cdda5f 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 @@ -249,13 +249,12 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle); } - String requestPath = request.getRequestPath(); - String method = request.getRequestType().name(); - String resourceType = request.getResourceName(); if (Constants.SyncStrategy.LOCATION.equals(resourceType)) { + String method = request.getRequestType().name(); if ("POST".equals(method) || "PUT".equals(method)) { + String requestPath = request.getRequestPath(); String locationId = getLocationId(requestPath, resultContent); if (StringUtils.isNotBlank(locationId)) { Location location = From 30075ec097a952487d038e4c92b0d2c9a3b83834 Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:23:32 +0300 Subject: [PATCH 10/14] add tests for getLocationId method --- .../plugins/SyncAccessDecisionTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 5097b805..b5f6ac5b 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 @@ -1045,6 +1045,31 @@ public void testPostProcessCallsUpdateLocationLineage() throws IOException { } } + @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); From 9d12e6dbe5cc947d85b5d5314103dc6147668a0d Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 12:39:08 +0300 Subject: [PATCH 11/14] Upgrade version --- exec/pom.xml | 4 ++-- plugins/pom.xml | 2 +- pom.xml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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 06d515c8..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 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 From 452f29da3117993edeeb8d01714e3291ef3c35de Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 13:02:09 +0300 Subject: [PATCH 12/14] Merge conditions to one, Update test to handle this merge --- .../gateway/plugins/SyncAccessDecision.java | 20 ++++++++----------- .../plugins/SyncAccessDecisionTest.java | 3 --- 2 files changed, 8 insertions(+), 15 deletions(-) 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 21cdda5f..2eebe13a 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 @@ -249,18 +249,14 @@ 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)) { - String requestPath = request.getRequestPath(); - String locationId = getLocationId(requestPath, resultContent); - if (StringUtils.isNotBlank(locationId)) { - Location location = - LocationHelper.updateLocationLineage(fhirR4Client, locationId); - resultContent = this.fhirR4JsonParser.encodeResourceToString(location); - } + if (Constants.SyncStrategy.LOCATION.equals(request.getResourceName()) + && ("POST".equals(request.getRequestType().name()) + || "PUT".equals(request.getRequestType().name()))) { + String requestPath = request.getRequestPath(); + String locationId = getLocationId(requestPath, resultContent); + if (StringUtils.isNotBlank(locationId)) { + Location location = LocationHelper.updateLocationLineage(fhirR4Client, locationId); + resultContent = this.fhirR4JsonParser.encodeResourceToString(location); } } 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 b5f6ac5b..5b7fc2f1 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 @@ -1039,9 +1039,6 @@ public void testPostProcessCallsUpdateLocationLineage() throws IOException { testInstance.postProcess(requestDetailsSpy, fhirResponseMock); locationHelperMock.verify( () -> LocationHelper.updateLocationLineage(eq(iGenericClient), eq("123"))); - - Assert.assertNotNull(location); - Assert.assertEquals("Location/123", location.getId()); } } From 51b8e8f6a3fc3204f3dd4cad2d20a63470ec7927 Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 13:13:19 +0300 Subject: [PATCH 13/14] Improve getLocationId to be more clear --- .../fhir/gateway/plugins/SyncAccessDecision.java | 10 ++++------ .../fhir/gateway/plugins/SyncAccessDecisionTest.java | 6 ------ 2 files changed, 4 insertions(+), 12 deletions(-) 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 2eebe13a..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 @@ -252,6 +252,7 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) 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)) { @@ -265,13 +266,10 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) @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(); - } + IBaseResource parsedResource = this.fhirR4JsonParser.parseResource(resultContent); + if (parsedResource instanceof Location) { + return ((Location) parsedResource).getIdElement().getIdPart(); } String[] pathParts = requestPath.split("/"); 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 5b7fc2f1..4e0c4786 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 @@ -1059,12 +1059,6 @@ public void testGetLocationId() { 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 From 72a63a2985a685dcc6b0f7d3ca8cd13820bc0cb4 Mon Sep 17 00:00:00 2001 From: lincmba Date: Wed, 5 Feb 2025 14:23:00 +0300 Subject: [PATCH 14/14] Improve test coverage --- .../fhir/gateway/plugins/SyncAccessDecisionTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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 4e0c4786..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 @@ -22,6 +22,7 @@ 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; @@ -1059,6 +1060,13 @@ public void testGetLocationId() { 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