Skip to content

Commit

Permalink
Merge pull request #84 from onaio/issue83-jpa-test
Browse files Browse the repository at this point in the history
Workaround Fetch Binary breaks with latest JPA HAPI FHIR Server
  • Loading branch information
ndegwamartin authored Nov 28, 2024
2 parents 8b35a6f + d9da604 commit 3471eb3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 19 deletions.
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.2</version>
<version>2.2.3</version>
</parent>

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

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion 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.2</version>
<version>2.2.3</version>
</parent>

<artifactId>plugins</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,32 @@ private Map<String, List<String>> collateSyncStrategyIds(
&& practitionerDetails.getFhirPractitionerDetails()
!= null
? PractitionerDetailsEndpointHelper.getAttributedLocations(
practitionerDetails
.getFhirPractitionerDetails()
.getLocationHierarchyList())
PractitionerDetailsEndpointHelper.getLocationsHierarchy(
practitionerDetails
.getFhirPractitionerDetails()
.getLocations()
.stream()
.map(
location ->
location.getIdElement()
.getIdPart())
.collect(Collectors.toList())))
: new HashSet<>();
}

} else
throw new IllegalStateException(
"'" + syncStrategy + "' sync strategy NOT supported!!");

resultMap = Map.of(syncStrategy, new ArrayList<>(syncStrategyIds));
resultMap =
!syncStrategyIds.isEmpty()
? Map.of(syncStrategy, new ArrayList<>(syncStrategyIds))
: null;

if (resultMap == null) {
throw new IllegalStateException(
"No Sync strategy ids found for selected sync strategy " + syncStrategy);
}

} else
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,34 @@ public static Binary readApplicationConfigBinaryResource(
IGenericClient client = Utils.createFhirClientForR4(fhirContext);
Binary binary = null;
if (!binaryResourceId.isBlank()) {
binary = client.read().resource(Binary.class).withId(binaryResourceId).execute();
Bundle bundle =
(Bundle)
client.search()
.forResource(Binary.class)
.where(Binary.RES_ID.exactly().identifier(binaryResourceId))
.execute();
binary = (Binary) bundle.getEntryFirstRep().getResource();
}
return binary;
}

public static String findSyncStrategy(Binary binary) {

byte[] bytes =
binary != null && binary.getDataElement() != null
? Base64.getDecoder().decode(binary.getDataElement().getValueAsString())
: null;
return findSyncStrategy(bytes);
}

public static String findSyncStrategy(byte[] binaryDataBytes) {
if (binaryDataBytes == null || binaryDataBytes.length == 0)
return org.smartregister.utils.Constants.EMPTY_STRING;
String syncStrategy = org.smartregister.utils.Constants.EMPTY_STRING;
if (bytes != null) {
String json = new String(bytes);
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
JsonArray jsonArray =
jsonObject.getAsJsonArray(Constants.AppConfigJsonKey.SYNC_STRATEGY);
if (jsonArray != null && !jsonArray.isEmpty())
syncStrategy = jsonArray.get(0).getAsString();
}
String json = new String(binaryDataBytes);
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
JsonArray jsonArray = jsonObject.getAsJsonArray(Constants.AppConfigJsonKey.SYNC_STRATEGY);
if (jsonArray != null && !jsonArray.isEmpty())
syncStrategy = jsonArray.get(0).getAsString();

return syncStrategy;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.smartregister.fhir.gateway.plugins;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.r4.model.Base64BinaryType;
import org.hl7.fhir.r4.model.Binary;
import org.hl7.fhir.r4.model.Bundle;
Expand All @@ -12,13 +14,17 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.gclient.ICriterion;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.IUntypedQuery;

public class UtilsTest {

Expand Down Expand Up @@ -126,7 +132,7 @@ public void testGetBinaryResourceReferenceWithNoFocusReference() {

@Test
public void testFindSyncStrategyWithNullBinary() {
String result = Utils.findSyncStrategy(null);
String result = Utils.findSyncStrategy((Binary) null);
Assert.assertEquals(org.smartregister.utils.Constants.EMPTY_STRING, result);
}

Expand Down Expand Up @@ -179,4 +185,49 @@ public void testReadApplicationConfigBinaryResourceWithEmptyResourceId() {
Binary result = Utils.readApplicationConfigBinaryResource("", fhirContextMock);
Assert.assertNull(result);
}

@Test
public void testReadApplicationConfigBinaryResourceReturnsBinary() {
Binary binary = new Binary();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("appId", "test-app");
jsonObject.addProperty("appTitle", "Test App");

String json = jsonObject.toString();
String encodedJson = Base64.getEncoder().encodeToString(json.getBytes());
binary.setDataElement(new Base64BinaryType(encodedJson));
binary.setId("test-binary-id");

IGenericClient client = Mockito.mock(IGenericClient.class);
Mockito.doReturn(client)
.when(fhirContextMock)
.newRestfulGenericClient(ArgumentMatchers.any());

IUntypedQuery<IBaseBundle> binaryIUntypedQuery = Mockito.mock(IUntypedQuery.class);
Mockito.doReturn(binaryIUntypedQuery).when(client).search();

IQuery<IBaseBundle> binaryIQuery = Mockito.mock(IQuery.class);

Mockito.doReturn(binaryIQuery).when(binaryIUntypedQuery).forResource(Binary.class);

IQuery<IBaseBundle> iQuery = Mockito.mock(IQuery.class);
Mockito.doReturn(iQuery).when(binaryIQuery).where(ArgumentMatchers.any(ICriterion.class));

Bundle bundle = new Bundle();
bundle.getEntry().add(new Bundle.BundleEntryComponent().setResource(binary));

Mockito.doReturn(bundle).when(iQuery).execute();

Binary result =
Utils.readApplicationConfigBinaryResource("test-binary-id", fhirContextMock);

Assert.assertNotNull(result);
Assert.assertEquals("test-binary-id", result.getId());

byte[] binaryDataByteArray =
Base64.getDecoder().decode(result.getDataElement().getValueAsString());
String decodedJson = new String(binaryDataByteArray, StandardCharsets.UTF_8);

Assert.assertEquals("{\"appId\":\"test-app\",\"appTitle\":\"Test App\"}", decodedJson);
}
}
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.2</version>
<version>2.2.3</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit 3471eb3

Please sign in to comment.