Fix NPE in IdHelperService (#2819)

This commit is contained in:
James Agnew 2021-07-22 08:34:28 -04:00 committed by GitHub
parent 8b8db82666
commit d78fef6732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -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<Integer> 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;

View File

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