Avoid NPE in RequestPartitionId on Default Partition (#2382)

This commit is contained in:
James Agnew 2021-02-12 11:04:25 -05:00 committed by GitHub
parent aaa91616aa
commit b247e8cde7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -161,7 +161,10 @@ public class RequestPartitionId {
* Returns true if this request partition contains only one partition ID and it is the DEFAULT partition ID (null)
*/
public boolean isDefaultPartition() {
return getPartitionIds().size() == 1 && getPartitionIds().get(0) == null;
if (isAllPartitions()) {
return false;
}
return hasPartitionIds() && getPartitionIds().size() == 1 && getPartitionIds().get(0) == null;
}
public boolean hasPartitionId(Integer thePartitionId) {

View File

@ -1,11 +1,14 @@
package ca.uhn.fhir.interceptor.model;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RequestPartitionIdTest {
@ -16,9 +19,21 @@ public class RequestPartitionIdTest {
@Test
public void testEquals() {
assertEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020,1,1)), RequestPartitionId.fromPartitionId(123, LocalDate.of(2020,1,1)));
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020,1,1)), null);
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020,1,1)), "123");
assertEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)));
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), null);
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), "123");
}
@Test
public void testPartition() {
assertFalse(RequestPartitionId.allPartitions().isDefaultPartition());
assertFalse(RequestPartitionId.defaultPartition().isAllPartitions());
assertTrue(RequestPartitionId.defaultPartition().isDefaultPartition());
assertTrue(RequestPartitionId.allPartitions().isAllPartitions());
assertFalse(RequestPartitionId.forPartitionIdsAndNames(Lists.newArrayList("Name1", "Name2"), null, null).isAllPartitions());
assertFalse(RequestPartitionId.forPartitionIdsAndNames(Lists.newArrayList("Name1", "Name2"), null, null).isDefaultPartition());
assertFalse(RequestPartitionId.forPartitionIdsAndNames(null, Lists.newArrayList(1, 2), null).isAllPartitions());
assertFalse(RequestPartitionId.forPartitionIdsAndNames(null, Lists.newArrayList(1, 2), null).isDefaultPartition());
}