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
This commit is contained in:
Qingyixia 2022-09-08 11:44:41 -04:00 committed by GitHub
parent 6d9a19997f
commit 306bb8b64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 2 deletions

View File

@ -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() {}

View File

@ -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() + ".");
}
}
}

View File

@ -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))) {

View File

@ -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."