From a5e734fec8a69d0e9bd9b6e35fc431ed18c9c5b3 Mon Sep 17 00:00:00 2001 From: Tadgh Date: Mon, 26 Apr 2021 22:24:47 -0400 Subject: [PATCH] Handle sorting entries which contain no resources --- .../java/ca/uhn/fhir/i18n/HapiLocalizer.java | 2 + .../java/ca/uhn/fhir/util/BundleUtil.java | 55 ++++++++++--------- .../uhn/fhir/util/bundle/BundleUtilTest.java | 10 ++++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java index 403ea58cc21..a7fd6d24edf 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java @@ -10,6 +10,8 @@ import java.util.concurrent.ConcurrentHashMap; import static org.apache.commons.lang3.StringUtils.*; + + /* * #%L * HAPI FHIR - Core Library diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java index 26119e9b0fd..f53af2cd4ca 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java @@ -248,39 +248,42 @@ public class BundleUtil { for (BundleEntryParts bundleEntryPart : bundleEntryParts) { IBaseResource resource = bundleEntryPart.getResource(); - String resourceId = resource.getIdElement().toVersionless().toString(); - resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); - if (resourceId == null) { - if (bundleEntryPart.getFullUrl() != null) { - resourceId = bundleEntryPart.getFullUrl(); + if (resource != null) { + String resourceId = resource.getIdElement().toVersionless().toString(); + resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); + if (resourceId == null) { + if (bundleEntryPart.getFullUrl() != null) { + resourceId = bundleEntryPart.getFullUrl(); + } } + + color.put(resourceId, WHITE); } - - color.put(resourceId, WHITE); - } for (BundleEntryParts bundleEntryPart : bundleEntryParts) { IBaseResource resource = bundleEntryPart.getResource(); - String resourceId = resource.getIdElement().toVersionless().toString(); - resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); - if (resourceId == null) { - if (bundleEntryPart.getFullUrl() != null) { - resourceId = bundleEntryPart.getFullUrl(); - } - } - List allResourceReferences = theContext.newTerser().getAllResourceReferences(resource); - String finalResourceId = resourceId; - allResourceReferences - .forEach(refInfo -> { - String referencedResourceId = refInfo.getResourceReference().getReferenceElement().toVersionless().getValue(); - if (color.containsKey(referencedResourceId)) { - if (!adjList.containsKey(finalResourceId)) { - adjList.put(finalResourceId, new ArrayList<>()); - } - adjList.get(finalResourceId).add(referencedResourceId); + if (resource != null) { + String resourceId = resource.getIdElement().toVersionless().toString(); + resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); + if (resourceId == null) { + if (bundleEntryPart.getFullUrl() != null) { + resourceId = bundleEntryPart.getFullUrl(); } - }); + } + List allResourceReferences = theContext.newTerser().getAllResourceReferences(resource); + String finalResourceId = resourceId; + allResourceReferences + .forEach(refInfo -> { + String referencedResourceId = refInfo.getResourceReference().getReferenceElement().toVersionless().getValue(); + if (color.containsKey(referencedResourceId)) { + if (!adjList.containsKey(finalResourceId)) { + adjList.put(finalResourceId, new ArrayList<>()); + } + adjList.get(finalResourceId).add(referencedResourceId); + } + }); + } } for (Map.Entry entry:color.entrySet()) { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java index c7ddf26b077..2fe10217652 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java @@ -1,6 +1,7 @@ package ca.uhn.fhir.util.bundle; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.util.BundleBuilder; import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.TestUtil; import org.hl7.fhir.r4.model.Bundle; @@ -256,6 +257,15 @@ public class BundleUtilTest { assertThat(entry.get(6).getRequest().getMethod(), is(equalTo(GET))); } + @Test + public void testBundleSortsCanHandlesDeletesThatContainNoResources() { + Patient p = new Patient(); + p.setId("Patient/123"); + BundleBuilder builder = new BundleBuilder(ourCtx); + builder.addTransactionDeleteEntry(p); + BundleUtil.sortEntriesIntoProcessingOrder(ourCtx, builder.getBundle()); + } + @Test public void testTransactionSorterReturnsDeletesInCorrectProcessingOrder() { Bundle b = new Bundle();