From 6431ae4401980ea634ad6188a5872a2edf1f206b Mon Sep 17 00:00:00 2001 From: katie_smilecdr Date: Fri, 24 Sep 2021 13:00:05 -0400 Subject: [PATCH] [3020] fix implementation/add tests --- .../jpa/partition/PartitionLookupSvcImpl.java | 2 - .../PartitionManagementProvider.java | 24 ++++++--- .../PartitionManagementProviderTest.java | 54 ++++++++++++++----- .../PartitionSettingsSvcImplTest.java | 24 +++++++++ 4 files changed, 81 insertions(+), 23 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java index cd0d61d947c..5f548604114 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java @@ -171,10 +171,8 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc { } @Override - @Transactional public List listPartitions() { List allPartitions = myPartitionDao.findAll(); - clearCaches(); return allPartitions; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionManagementProvider.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionManagementProvider.java index 19f4071fffe..25509dc5974 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionManagementProvider.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionManagementProvider.java @@ -27,6 +27,7 @@ import ca.uhn.fhir.rest.annotation.OperationParam; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.server.provider.ProviderConstants; import ca.uhn.fhir.util.ParametersUtil; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.springframework.beans.factory.annotation.Autowired; @@ -150,11 +151,11 @@ public class PartitionManagementProvider { /** * Add Partition: * - * $partition-management-read-partition + * $partition-management-list-partitions * */ @Operation(name = ProviderConstants.PARTITION_MANAGEMENT_LIST_PARTITIONS, idempotent = true) - public List listPartitions( + public IBaseParameters addPartitions( @ResourceParam IBaseParameters theRequest ) { List output = myPartitionLookupSvc.listPartitions(); @@ -171,12 +172,21 @@ public class PartitionManagementProvider { return retVal; } - private List prepareOutputList(List theOutput) { - List retVal = new ArrayList(); - for (PartitionEntity partitionEntity : theOutput) { - retVal.add(prepareOutput(partitionEntity)); + private IBaseParameters prepareOutputList(List theOutput) { + if (theOutput != null) { + IBaseParameters retVal = ParametersUtil.newInstance(myCtx); + for (PartitionEntity partitionEntity : theOutput) { + IBase resultPart = ParametersUtil.addParameterToParameters(myCtx, retVal, "partition"); + ParametersUtil.addPartInteger(myCtx, resultPart, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID, partitionEntity.getId()); + ParametersUtil.addPartCode(myCtx, resultPart, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME, partitionEntity.getName()); + if (isNotBlank(partitionEntity.getDescription())) { + ParametersUtil.addPartString(myCtx, resultPart, ProviderConstants.PARTITION_MANAGEMENT_PARTITION_DESC, partitionEntity.getDescription()); + } + } + return retVal; + } else { + return null; } - return retVal; } @Nonnull diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionManagementProviderTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionManagementProviderTest.java index 161a67354e7..9f4b3d57039 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionManagementProviderTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionManagementProviderTest.java @@ -28,8 +28,13 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import javax.annotation.Nonnull; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; @@ -241,30 +246,48 @@ public class PartitionManagementProviderTest { partition1.setId(1); partition1.setName("PARTITION-1"); partition1.setDescription("a description1"); - when(myPartitionConfigSvc.getPartitionById(eq(1))).thenReturn(partition1); PartitionEntity partition2 = new PartitionEntity(); - partition1.setId(2); - partition1.setName("PARTITION-2"); - partition1.setDescription("a description2"); - when(myPartitionConfigSvc.getPartitionById(eq(2))).thenReturn(partition2); + partition2.setId(2); + partition2.setName("PARTITION-2"); + partition2.setDescription("a description2"); - List response = (List) myClient + List partitionList = new ArrayList(); + partitionList.add(partition1); + partitionList.add(partition2); + when(myPartitionConfigSvc.listPartitions()).thenReturn(partitionList); + + + Parameters response = myClient .operation() .onServer() .named(ProviderConstants.PARTITION_MANAGEMENT_LIST_PARTITIONS) .withNoParameters(Parameters.class) + .useHttpGet() .encodedXml() .execute(); - System.out.println(response); -// ourLog.info("Response:\n{}", ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(response.get(0))); -// verify(myPartitionConfigSvc, times(1)).getPartitionById(any()); -// verifyNoMoreInteractions(myPartitionConfigSvc); -// -// assertEquals(1, ((IntegerType) response.get(0).getParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID)).getValue().intValue()); -// assertEquals("PARTITION-1", ((StringType) response.get(0).getParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME)).getValue()); -// assertEquals("a description1", ((StringType) response.get(0).getParameter(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_DESC)).getValue()); + ourLog.info("Response:\n{}", ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(response)); + verify(myPartitionConfigSvc, times(1)).listPartitions(); + verifyNoMoreInteractions(myPartitionConfigSvc); + + List list = getParametersByName(response, "partition"); + assertThat(list, hasSize(2)); + List part = list.get(0).getPart(); + assertThat(part.get(0).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID)); + assertEquals(1, ((IntegerType) part.get(0).getValue()).getValue().intValue()); + assertThat(part.get(1).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME)); + assertEquals("PARTITION-1", part.get(1).getValue().toString()); + assertThat(part.get(2).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_DESC)); + assertEquals("a description1", part.get(2).getValue().toString()); + + part = list.get(1).getPart(); + assertThat(part.get(0).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_ID)); + assertEquals(2, ((IntegerType) part.get(0).getValue()).getValue().intValue()); + assertThat(part.get(1).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_NAME)); + assertEquals("PARTITION-2", part.get(1).getValue().toString()); + assertThat(part.get(2).getName(), is(ProviderConstants.PARTITION_MANAGEMENT_PARTITION_DESC)); + assertEquals("a description2", part.get(2).getValue().toString()); } @@ -288,4 +311,7 @@ public class PartitionManagementProviderTest { return t -> t.getArgument(0, PartitionEntity.class); } + private List getParametersByName(Parameters theParams, String theName) { + return theParams.getParameter().stream().filter(p -> p.getName().equals(theName)).collect(Collectors.toList()); + } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java index 42dce8fcce3..bd9604e1aab 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java @@ -8,7 +8,10 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { @@ -166,4 +169,25 @@ public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { } + @Test + public void testListPartitions() { + PartitionEntity partition1 = new PartitionEntity(); + partition1.setId(1); + partition1.setName("PARTITION-1"); + partition1.setDescription("a description1"); + + PartitionEntity partition2 = new PartitionEntity(); + partition2.setId(2); + partition2.setName("PARTITION-2"); + partition2.setDescription("a description2"); + + myPartitionConfigSvc.createPartition(partition1); + myPartitionConfigSvc.createPartition(partition2); + + List actual = myPartitionConfigSvc.listPartitions(); + + assertEquals(2, actual.size()); + assertTrue(actual.stream().anyMatch(item -> "PARTITION-1".equals(item.getName()))); + assertTrue(actual.stream().anyMatch(item -> "PARTITION-2".equals(item.getName()))); + } }