From d78fef67329bebdae8f81fecc1cbbc53630243ef Mon Sep 17 00:00:00 2001 From: James Agnew Date: Thu, 22 Jul 2021 08:34:28 -0400 Subject: [PATCH] Fix NPE in IdHelperService (#2819) --- .../fhir/jpa/dao/index/IdHelperService.java | 10 ++++-- .../jpa/dao/index/IdHelperServiceTest.java | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/index/IdHelperServiceTest.java diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/index/IdHelperService.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/index/IdHelperService.java index 031d75bc608..69302a227ca 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/index/IdHelperService.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/index/IdHelperService.java @@ -36,6 +36,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ListMultimap; import com.google.common.collect.MultimapBuilder; import org.apache.commons.lang3.StringUtils; @@ -389,9 +390,9 @@ public class IdHelperService { return retVal; } - private RequestPartitionId replaceDefault(RequestPartitionId theRequestPartitionId) { + RequestPartitionId replaceDefault(RequestPartitionId theRequestPartitionId) { if (myPartitionSettings.getDefaultPartitionId() != null) { - if (theRequestPartitionId.hasDefaultPartitionId()) { + if (!theRequestPartitionId.isAllPartitions() && theRequestPartitionId.hasDefaultPartitionId()) { List partitionIds = theRequestPartitionId .getPartitionIds() .stream() @@ -577,6 +578,11 @@ public class IdHelperService { } } + @VisibleForTesting + void setPartitionSettingsForUnitTest(PartitionSettings thePartitionSettings) { + myPartitionSettings = thePartitionSettings; + } + public static boolean isValidPid(IIdType theId) { if (theId == null) { return false; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/index/IdHelperServiceTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/index/IdHelperServiceTest.java new file mode 100644 index 00000000000..20e100065f1 --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/index/IdHelperServiceTest.java @@ -0,0 +1,35 @@ +package ca.uhn.fhir.jpa.dao.index; + +import ca.uhn.fhir.interceptor.model.RequestPartitionId; +import ca.uhn.fhir.jpa.model.config.PartitionSettings; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class IdHelperServiceTest { + + @Test + public void testReplaceDefault_AllPartitions() { + + IdHelperService svc = new IdHelperService(); + PartitionSettings partitionSettings = new PartitionSettings(); + partitionSettings.setDefaultPartitionId(1); + svc.setPartitionSettingsForUnitTest(partitionSettings); + + RequestPartitionId outcome = svc.replaceDefault(RequestPartitionId.allPartitions()); + assertSame(RequestPartitionId.allPartitions(), outcome); + } + + @Test + public void testReplaceDefault_DefaultPartition() { + + IdHelperService svc = new IdHelperService(); + PartitionSettings partitionSettings = new PartitionSettings(); + partitionSettings.setDefaultPartitionId(1); + svc.setPartitionSettingsForUnitTest(partitionSettings); + + RequestPartitionId outcome = svc.replaceDefault(RequestPartitionId.defaultPartition()); + assertEquals(1, outcome.getPartitionIds().get(0)); + } + +}