From 306bb8b64aac725d6172234d3a58fe74762ae5e5 Mon Sep 17 00:00:00 2001 From: Qingyixia <106992634+Qingyixia@users.noreply.github.com> Date: Thu, 8 Sep 2022 11:44:41 -0400 Subject: [PATCH] Handling unexpected response status code of import-poll-status operation (#3939) * Handling unexpected response status code when executing import-poll-status operation * Handling unexpected response status code --- .../src/main/java/ca/uhn/fhir/i18n/Msg.java | 2 +- .../ca/uhn/fhir/cli/BulkImportCommand.java | 5 +- .../ca/uhn/fhir/cli/BulkImportCommandIT.java | 47 +++++++++++++++++++ ...response-code-when-import-poll-status.yaml | 5 ++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3914-handle-unexpected-response-code-when-import-poll-status.yaml diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java index f96fed683c9..913483f5d19 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/Msg.java @@ -25,7 +25,7 @@ public final class Msg { /** * IMPORTANT: Please update the following comment after you add a new code - * Last used code value: 2136 + * Last used code value: 2138 */ private Msg() {} diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BulkImportCommand.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BulkImportCommand.java index 734ad83fbf2..a3216f1542c 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BulkImportCommand.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/main/java/ca/uhn/fhir/cli/BulkImportCommand.java @@ -177,10 +177,13 @@ public class BulkImportCommand extends BaseCommand { if (response.getResponseStatusCode() == 200) { break; - } else { + } else if (response.getResponseStatusCode() == 202){ // still in progress continue; } + else { + throw new InternalErrorException(Msg.code(2138) + "Unexpected response status code: " + response.getResponseStatusCode() + "."); + } } } diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BulkImportCommandIT.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BulkImportCommandIT.java index 1a89e40fff8..1f0b95bef93 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BulkImportCommandIT.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/BulkImportCommandIT.java @@ -8,6 +8,7 @@ import ca.uhn.fhir.batch2.model.JobInstanceStartRequest; import ca.uhn.fhir.batch2.model.StatusEnum; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.batch.models.Batch2JobStartResponse; +import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor; import ca.uhn.fhir.test.utilities.HttpClientExtension; import ca.uhn.fhir.test.utilities.server.RestfulServerExtension; @@ -174,6 +175,52 @@ public class BulkImportCommandIT { assertEquals(fileContents1, fetchFile(jobParameters.getNdJsonUrls().get(1))); } + @Test + public void testBulkImport_FAILED() throws IOException { + + JobInstance jobInfo = new JobInstance() + .setStatus(StatusEnum.FAILED) + .setCreateTime(parseDate("2022-01-01T12:00:00-04:00")) + .setStartTime(parseDate("2022-01-01T12:10:00-04:00")); + + when(myJobCoordinator.getInstance(eq("THE-JOB-ID"))).thenReturn(jobInfo); + + String fileContents1 = "{\"resourceType\":\"Observation\"}\n{\"resourceType\":\"Observation\"}"; + String fileContents2 = "{\"resourceType\":\"Patient\"}\n{\"resourceType\":\"Patient\"}"; + writeNdJsonFileToTempDirectory(fileContents1, "file1.json"); + writeNdJsonFileToTempDirectory(fileContents2, "file2.json"); + + when(myJobCoordinator.startInstance(any())).thenReturn(createJobStartResponse("THE-JOB-ID")); + + // Start the command in a separate thread + new Thread(() -> App.main(new String[]{ + BulkImportCommand.BULK_IMPORT, + "--" + BaseCommand.FHIR_VERSION_PARAM_LONGOPT, "r4", + "--" + BulkImportCommand.PORT, "0", + "--" + BulkImportCommand.SOURCE_DIRECTORY, myTempDir.toAbsolutePath().toString(), + "--" + BulkImportCommand.TARGET_BASE, myRestfulServerExtension.getBaseUrl() + })).start(); + + ourLog.info("Waiting for initiation requests"); + await().until(() -> myRestfulServerExtension.getRequestContentTypes().size(), equalTo(2)); + ourLog.info("Initiation requests complete"); + + verify(myJobCoordinator, timeout(10000).times(1)).startInstance(myStartCaptor.capture()); + + try{ + JobInstanceStartRequest startRequest = myStartCaptor.getValue(); + BulkImportJobParameters jobParameters = startRequest.getParameters(BulkImportJobParameters.class); + + // Reverse order because Patient should be first + assertEquals(2, jobParameters.getNdJsonUrls().size()); + assertEquals(fileContents2, fetchFile(jobParameters.getNdJsonUrls().get(0))); + assertEquals(fileContents1, fetchFile(jobParameters.getNdJsonUrls().get(1))); + } + catch(InternalErrorException e) { + ourLog.error(e.getMessage()); + } + } + private String fetchFile(String url) throws IOException { String outcome; try (CloseableHttpResponse response = myHttpClientExtension.getClient().execute(new HttpGet(url))) { diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3914-handle-unexpected-response-code-when-import-poll-status.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3914-handle-unexpected-response-code-when-import-poll-status.yaml new file mode 100644 index 00000000000..4ddddb383df --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3914-handle-unexpected-response-code-when-import-poll-status.yaml @@ -0,0 +1,5 @@ +--- +type: fix +issue: 3914 +title: "Previously, unexpected response status code is not handled by the import-poll-status operation. +Now, it is fixed and throwing an error message."