From 3a8881f9133dbe13c9060a23db06de6c7a15b4b4 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sun, 14 Mar 2021 13:12:58 -0400 Subject: [PATCH] Resolve NPE in group bulk export (#2476) --- .../bulk/provider/BulkDataExportProvider.java | 12 ++++---- .../jpa/bulk/BulkDataExportProviderTest.java | 17 +++++++++++ .../jpa/bulk/BulkDataExportSvcImplR4Test.java | 29 +++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java index dbdb57e3f82..28c4bc39e05 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java @@ -132,12 +132,14 @@ public class BulkDataExportProvider { } private void validateResourceTypesAllContainPatientSearchParams(Set theResourceTypes) { - List badResourceTypes = theResourceTypes.stream() - .filter(resourceType -> !myBulkDataExportSvc.getPatientCompartmentResources().contains(resourceType)) - .collect(Collectors.toList()); + if (theResourceTypes != null) { + List badResourceTypes = theResourceTypes.stream() + .filter(resourceType -> !myBulkDataExportSvc.getPatientCompartmentResources().contains(resourceType)) + .collect(Collectors.toList()); - if (!badResourceTypes.isEmpty()) { - throw new InvalidRequestException(String.format("Resource types [%s] are invalid for this type of export, as they do not contain search parameters that refer to patients.", String.join(",", badResourceTypes))); + if (!badResourceTypes.isEmpty()) { + throw new InvalidRequestException(String.format("Resource types [%s] are invalid for this type of export, as they do not contain search parameters that refer to patients.", String.join(",", badResourceTypes))); + } } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java index f33e57d7b06..ede41a213e8 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java @@ -148,6 +148,7 @@ public class BulkDataExportProviderTest { assertThat(options.getFilters(), containsInAnyOrder("Patient?identifier=foo")); } + @Test public void testSuccessfulInitiateBulkRequest_Get() throws IOException { @@ -432,6 +433,22 @@ public class BulkDataExportProviderTest { assertThat(responseBody, is(containsString("Resource types [StructureDefinition] are invalid for this type of export, as they do not contain search parameters that refer to patients."))); } + @Test + public void testInitiateGroupExportWithNoResourceTypes() throws IOException { + IBulkDataExportSvc.JobInfo jobInfo = new IBulkDataExportSvc.JobInfo() + .setJobId(A_JOB_ID); + when(myBulkDataExportSvc.submitJob(any(), any())).thenReturn(jobInfo); + + String url = "http://localhost:" + myPort + "/" + "Group/123/" +JpaConstants.OPERATION_EXPORT + + "?" + JpaConstants.PARAM_EXPORT_OUTPUT_FORMAT + "=" + UrlUtil.escapeUrlParam(Constants.CT_FHIR_NDJSON);; + + HttpGet get = new HttpGet(url); + get.addHeader(Constants.HEADER_PREFER, Constants.HEADER_PREFER_RESPOND_ASYNC); + CloseableHttpResponse execute = myClient.execute(get); + + assertThat(execute.getStatusLine().getStatusCode(), is(equalTo(202))); + } + @Test public void testInitiateWithPostAndMultipleTypeFilters() throws IOException { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportSvcImplR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportSvcImplR4Test.java index b06c03fa049..404045a4ff8 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportSvcImplR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportSvcImplR4Test.java @@ -19,9 +19,11 @@ import ca.uhn.fhir.jpa.entity.BulkExportCollectionEntity; import ca.uhn.fhir.jpa.entity.BulkExportCollectionFileEntity; import ca.uhn.fhir.jpa.entity.BulkExportJobEntity; import ca.uhn.fhir.jpa.entity.MdmLink; +import ca.uhn.fhir.jpa.model.util.JpaConstants; import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum; import ca.uhn.fhir.mdm.api.MdmMatchResultEnum; import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.util.UrlUtil; import com.google.common.base.Charsets; @@ -37,6 +39,7 @@ import org.hl7.fhir.r4.model.Group; import org.hl7.fhir.r4.model.Immunization; import org.hl7.fhir.r4.model.InstantType; import org.hl7.fhir.r4.model.Observation; +import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Reference; import org.junit.jupiter.api.Test; @@ -52,6 +55,7 @@ import org.springframework.batch.core.explore.JobExplorer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Date; @@ -404,6 +408,31 @@ public class BulkDataExportSvcImplR4Test extends BaseJpaR4Test { } } + @Test + public void testGroupExport_NoResourceTypesSpecified() { + createResources(); + + // Create a bulk job + BulkDataExportOptions bulkDataExportOptions = new BulkDataExportOptions(); + bulkDataExportOptions.setOutputFormat(null); + bulkDataExportOptions.setSince(null); + bulkDataExportOptions.setFilters(null); + bulkDataExportOptions.setGroupId(myPatientGroupId); + bulkDataExportOptions.setExpandMdm(true); + bulkDataExportOptions.setExportStyle(BulkDataExportOptions.ExportStyle.GROUP); + IBulkDataExportSvc.JobInfo jobDetails = myBulkDataExportSvc.submitJob(bulkDataExportOptions); + + + myBulkDataExportSvc.buildExportFiles(); + awaitAllBulkJobCompletions(); + + IBulkDataExportSvc.JobInfo jobInfo = myBulkDataExportSvc.getJobInfoOrThrowResourceNotFound(jobDetails.getJobId()); + + assertThat(jobInfo.getStatus(), equalTo(BulkJobStatusEnum.COMPLETE)); + assertThat(jobInfo.getFiles().size(), equalTo(5)); + } + + @Test public void testGenerateBulkExport_WithHas() {