From b247e8cde7fba5fcebb58f88a6714d53c1aec933 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 12 Feb 2021 11:04:25 -0500 Subject: [PATCH] Avoid NPE in RequestPartitionId on Default Partition (#2382) --- .../interceptor/model/RequestPartitionId.java | 5 ++++- .../model/RequestPartitionIdTest.java | 21 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/model/RequestPartitionId.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/model/RequestPartitionId.java index 9f497459194..410e995d9f5 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/model/RequestPartitionId.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/model/RequestPartitionId.java @@ -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) { diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/interceptor/model/RequestPartitionIdTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/interceptor/model/RequestPartitionIdTest.java index 28de3972f80..896be941a7c 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/interceptor/model/RequestPartitionIdTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/interceptor/model/RequestPartitionIdTest.java @@ -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()); }