diff --git a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties index 5c8dbaab662..5cbae62faa5 100644 --- a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties +++ b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties @@ -188,6 +188,7 @@ ca.uhn.fhir.jpa.dao.index.IdHelperService.nonUniqueForcedId=Non-unique ID specif ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.noIdSupplied=No Partition ID supplied ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.missingPartitionIdOrName=Partition must have an ID and a Name +ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.duplicatePartitionId=Partition ID already exists ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.unknownPartitionId=No partition exists with ID {0} ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.invalidName=Partition name "{0}" is not valid ca.uhn.fhir.jpa.partition.PartitionLookupSvcImpl.cantCreateDuplicatePartitionName=Partition name "{0}" is already defined diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5011-partitioning-can-create-more-than-one-partition-with-same-id.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5011-partitioning-can-create-more-than-one-partition-with-same-id.yaml new file mode 100644 index 00000000000..b685ee04221 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5011-partitioning-can-create-more-than-one-partition-with-same-id.yaml @@ -0,0 +1,5 @@ +--- +type: fix +issue: 5011 +title: "Previously, if a partition was created with an ID that was already in use, it would overwrite the partition that + was using that ID. Now attempting to overwrite will return a 400" 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 679e3ac201a..c924fa1f12e 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 @@ -51,6 +51,7 @@ import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import java.util.List; +import java.util.ListIterator; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; @@ -141,7 +142,7 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc { validateNotInUnnamedPartitionMode(); validateHaveValidPartitionIdAndName(thePartition); validatePartitionNameDoesntAlreadyExist(thePartition.getName()); - + validIdUponCreation(thePartition); ourLog.info("Creating new partition with ID {} and Name {}", thePartition.getId(), thePartition.getName()); PartitionEntity retVal = myPartitionDao.save(thePartition); @@ -212,6 +213,13 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc { } } + private void validIdUponCreation(PartitionEntity thePartition){ + if (myPartitionDao.findById(thePartition.getId()).isPresent()) { + String msg = myFhirCtx.getLocalizer().getMessageSanitized(PartitionLookupSvcImpl.class, "duplicatePartitionId"); + throw new InvalidRequestException(Msg.code(2366) + msg); + } + } + private void validateHaveValidPartitionIdAndName(PartitionEntity thePartition) { if (thePartition.getId() == null || isBlank(thePartition.getName())) { String msg = myFhirCtx.getLocalizer().getMessage(PartitionLookupSvcImpl.class, "missingPartitionIdOrName"); @@ -227,7 +235,6 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc { String msg = myFhirCtx.getLocalizer().getMessageSanitized(PartitionLookupSvcImpl.class, "invalidName", thePartition.getName()); throw new InvalidRequestException(Msg.code(1312) + msg); } - } private void validateNotInUnnamedPartitionMode() { diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java index 62cca49702c..71f81e8982e 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java @@ -103,6 +103,27 @@ public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { } } + @Test + public void testCreatePartition_whenPartitionIdAlreadyExists_operationNotAllowed(){ + try { + PartitionEntity partition = new PartitionEntity(); + partition.setId(123); + partition.setName("NAME123"); + partition.setDescription("A description"); + myPartitionConfigSvc.createPartition(partition, null); + + partition = new PartitionEntity(); + partition.setId(123); + partition.setName("NAME111"); + partition.setDescription("A description"); + myPartitionConfigSvc.createPartition(partition, null); + } + + catch (InvalidRequestException e) { + assertEquals( Msg.code(2366) + "Partition ID already exists", e.getMessage()); + } + } + @Test public void testUpdatePartition_TryToRenameDefault() { PartitionEntity partition = new PartitionEntity();