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)); + } + +}