Handle sorting entries which contain no resources

This commit is contained in:
Tadgh 2021-04-26 22:24:47 -04:00
parent 798e960bb8
commit a5e734fec8
3 changed files with 41 additions and 26 deletions

View File

@ -10,6 +10,8 @@ import java.util.concurrent.ConcurrentHashMap;
import static org.apache.commons.lang3.StringUtils.*; import static org.apache.commons.lang3.StringUtils.*;
/* /*
* #%L * #%L
* HAPI FHIR - Core Library * HAPI FHIR - Core Library

View File

@ -248,39 +248,42 @@ public class BundleUtil {
for (BundleEntryParts bundleEntryPart : bundleEntryParts) { for (BundleEntryParts bundleEntryPart : bundleEntryParts) {
IBaseResource resource = bundleEntryPart.getResource(); IBaseResource resource = bundleEntryPart.getResource();
String resourceId = resource.getIdElement().toVersionless().toString(); if (resource != null) {
resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); String resourceId = resource.getIdElement().toVersionless().toString();
if (resourceId == null) { resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart);
if (bundleEntryPart.getFullUrl() != null) { if (resourceId == null) {
resourceId = bundleEntryPart.getFullUrl(); if (bundleEntryPart.getFullUrl() != null) {
resourceId = bundleEntryPart.getFullUrl();
}
} }
color.put(resourceId, WHITE);
} }
color.put(resourceId, WHITE);
} }
for (BundleEntryParts bundleEntryPart : bundleEntryParts) { for (BundleEntryParts bundleEntryPart : bundleEntryParts) {
IBaseResource resource = bundleEntryPart.getResource(); IBaseResource resource = bundleEntryPart.getResource();
String resourceId = resource.getIdElement().toVersionless().toString(); if (resource != null) {
resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart); String resourceId = resource.getIdElement().toVersionless().toString();
if (resourceId == null) { resourceIdToBundleEntryMap.put(resourceId, bundleEntryPart);
if (bundleEntryPart.getFullUrl() != null) { if (resourceId == null) {
resourceId = bundleEntryPart.getFullUrl(); if (bundleEntryPart.getFullUrl() != null) {
} resourceId = bundleEntryPart.getFullUrl();
}
List<ResourceReferenceInfo> 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);
} }
}); }
List<ResourceReferenceInfo> 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<String, Integer> entry:color.entrySet()) { for (Map.Entry<String, Integer> entry:color.entrySet()) {

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.util.bundle; package ca.uhn.fhir.util.bundle;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.BundleBuilder;
import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.BundleUtil;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Bundle;
@ -256,6 +257,15 @@ public class BundleUtilTest {
assertThat(entry.get(6).getRequest().getMethod(), is(equalTo(GET))); 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 @Test
public void testTransactionSorterReturnsDeletesInCorrectProcessingOrder() { public void testTransactionSorterReturnsDeletesInCorrectProcessingOrder() {
Bundle b = new Bundle(); Bundle b = new Bundle();