Ensure temp file ends with "." and then suffix. (#5894)

This commit is contained in:
Luke deGruchy 2024-04-30 17:53:44 -04:00 committed by GitHub
parent b7cdc751e7
commit 929583e2e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 102 additions and 2 deletions

View File

@ -19,6 +19,7 @@
*/
package ca.uhn.fhir.cli;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.jpa.model.util.JpaConstants;
import ca.uhn.fhir.jpa.provider.TerminologyUploaderProvider;
@ -31,6 +32,7 @@ import ca.uhn.fhir.system.HapiSystemProperties;
import ca.uhn.fhir.util.AttachmentUtil;
import ca.uhn.fhir.util.FileUtil;
import ca.uhn.fhir.util.ParametersUtil;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
@ -265,7 +267,7 @@ public class UploadTerminologyCommand extends BaseRequestGeneratingCommand {
"Response:\n{}", myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(response));
}
private void addFileToRequestBundle(IBaseParameters theInputParameters, String theFileName, byte[] theBytes) {
protected void addFileToRequestBundle(IBaseParameters theInputParameters, String theFileName, byte[] theBytes) {
byte[] bytes = theBytes;
String fileName = theFileName;
@ -277,7 +279,7 @@ public class UploadTerminologyCommand extends BaseRequestGeneratingCommand {
FileUtil.formatFileSize(ourTransferSizeLimit));
try {
File tempFile = File.createTempFile("hapi-fhir-cli", suffix);
File tempFile = File.createTempFile("hapi-fhir-cli", "." + suffix);
tempFile.deleteOnExit();
try (OutputStream fileOutputStream = new FileOutputStream(tempFile, false)) {
fileOutputStream.write(bytes);
@ -363,4 +365,9 @@ public class UploadTerminologyCommand extends BaseRequestGeneratingCommand {
}
return retVal;
}
@VisibleForTesting
void setFhirContext(FhirContext theFhirContext) {
myFhirCtx = theFhirContext;
}
}

View File

@ -22,6 +22,10 @@ import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyS
import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
import org.hl7.fhir.instance.model.api.IBaseParameters;
import org.hl7.fhir.r4.model.Attachment;
import org.hl7.fhir.r4.model.Parameters;
import org.hl7.fhir.r4.model.Type;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -43,6 +47,7 @@ import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -54,6 +59,8 @@ import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
@ -479,6 +486,86 @@ public class UploadTerminologyCommandTest {
uploadICD10UsingCompressedFile(theFhirVersion, theIncludeTls);
}
@ParameterizedTest
@MethodSource("paramsProvider")
@SuppressWarnings("unused") // Both params for @BeforeEach
void testZipFileInParameters(String theFhirVersion, boolean theIncludeTls) {
final IBaseParameters inputParameters = switch (myCtx.getVersion().getVersion()) {
case DSTU2, DSTU2_HL7ORG, DSTU2_1 -> new org.hl7.fhir.dstu2.model.Parameters();
case DSTU3 -> new org.hl7.fhir.dstu3.model.Parameters();
case R4 -> new Parameters();
case R4B -> new org.hl7.fhir.r4b.model.Parameters();
case R5 -> new org.hl7.fhir.r5.model.Parameters();
};
final UploadTerminologyCommand uploadTerminologyCommand = new UploadTerminologyCommand();
uploadTerminologyCommand.setFhirContext(myCtx);
uploadTerminologyCommand.setTransferSizeBytes(1);
uploadTerminologyCommand.addFileToRequestBundle(inputParameters, "something.zip", new byte[] {1,2});
final String actualAttachmentUrl = getAttachmentUrl(inputParameters, myCtx);
assertTrue(actualAttachmentUrl.endsWith(".zip"));
}
private static String getAttachmentUrl(IBaseParameters theInputParameters, FhirContext theCtx) {
switch (theCtx.getVersion().getVersion()) {
case DSTU2:
case DSTU2_HL7ORG:
case DSTU2_1: {
assertInstanceOf(org.hl7.fhir.dstu2.model.Parameters.class, theInputParameters);
final org.hl7.fhir.dstu2.model.Parameters dstu2Parameters = (org.hl7.fhir.dstu2.model.Parameters) theInputParameters;
final List<org.hl7.fhir.dstu2.model.Parameters.ParametersParameterComponent> dstu2ParametersList = dstu2Parameters.getParameter();
final Optional<org.hl7.fhir.dstu2.model.Parameters.ParametersParameterComponent> optDstu2FileParam = dstu2ParametersList.stream().filter(param -> TerminologyUploaderProvider.PARAM_FILE.equals(param.getName())).findFirst();
assertTrue(optDstu2FileParam.isPresent());
final org.hl7.fhir.dstu2.model.Type dstu2Value = optDstu2FileParam.get().getValue();
assertInstanceOf(org.hl7.fhir.dstu2.model.Attachment.class, dstu2Value);
final org.hl7.fhir.dstu2.model.Attachment dstu2Attachment = (org.hl7.fhir.dstu2.model.Attachment) dstu2Value;
return dstu2Attachment.getUrl();
}
case DSTU3: {
assertInstanceOf(org.hl7.fhir.dstu3.model.Parameters.class, theInputParameters);
final org.hl7.fhir.dstu3.model.Parameters dstu3Parameters = (org.hl7.fhir.dstu3.model.Parameters) theInputParameters;
final List<org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent> dstu3ParametersList = dstu3Parameters.getParameter();
final Optional<org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent> optDstu3FileParam = dstu3ParametersList.stream().filter(param -> TerminologyUploaderProvider.PARAM_FILE.equals(param.getName())).findFirst();
assertTrue(optDstu3FileParam.isPresent());
final org.hl7.fhir.dstu3.model.Type dstu3Value = optDstu3FileParam.get().getValue();
assertInstanceOf(org.hl7.fhir.dstu3.model.Attachment.class, dstu3Value);
final org.hl7.fhir.dstu3.model.Attachment dstu3Attachment = (org.hl7.fhir.dstu3.model.Attachment) dstu3Value;
return dstu3Attachment.getUrl();
}
case R4: {
assertInstanceOf(Parameters.class, theInputParameters);
final Parameters r4Parameters = (Parameters) theInputParameters;
final Parameters.ParametersParameterComponent r4Parameter = r4Parameters.getParameter(TerminologyUploaderProvider.PARAM_FILE);
final Type r4Value = r4Parameter.getValue();
assertInstanceOf(Attachment.class, r4Value);
final Attachment r4Attachment = (Attachment) r4Value;
return r4Attachment.getUrl();
}
case R4B: {
assertInstanceOf(org.hl7.fhir.r4b.model.Parameters.class, theInputParameters);
final org.hl7.fhir.r4b.model.Parameters r4bParameters = (org.hl7.fhir.r4b.model.Parameters) theInputParameters;
final org.hl7.fhir.r4b.model.Parameters.ParametersParameterComponent r4bParameter = r4bParameters.getParameter(TerminologyUploaderProvider.PARAM_FILE);
final org.hl7.fhir.r4b.model.DataType value = r4bParameter.getValue();
assertInstanceOf(org.hl7.fhir.r4b.model.Attachment.class, value);
final org.hl7.fhir.r4b.model.Attachment r4bAttachment = (org.hl7.fhir.r4b.model.Attachment) value;
return r4bAttachment.getUrl();
}
case R5: {
assertInstanceOf(org.hl7.fhir.r5.model.Parameters.class, theInputParameters);
final org.hl7.fhir.r5.model.Parameters r4Parameters = (org.hl7.fhir.r5.model.Parameters) theInputParameters;
final org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent parameter = r4Parameters.getParameter(TerminologyUploaderProvider.PARAM_FILE);
final org.hl7.fhir.r5.model.DataType value = parameter.getValue();
assertInstanceOf(org.hl7.fhir.r5.model.Attachment.class, value);
final org.hl7.fhir.r5.model.Attachment attachment = (org.hl7.fhir.r5.model.Attachment) value;
return attachment.getUrl();
}
default:
throw new IllegalStateException("Unknown FHIR version: " + theCtx.getVersion().getVersion());
}
}
private void uploadICD10UsingCompressedFile(String theFhirVersion, boolean theIncludeTls) throws IOException {
if (FHIR_VERSION_DSTU3.equals(theFhirVersion)) {
when(myTermLoaderSvc.loadIcd10cm(anyList(), any())).thenReturn(new UploadStatistics(100, new org.hl7.fhir.dstu3.model.IdType("CodeSystem/101")));

View File

@ -0,0 +1,6 @@
---
type: fix
issue: 5893
title: "Previously, hapi-fhir-cli: upload-terminology failed with a HAPI-0862 error when uploading LOINC.
This has been fixed."