|
| 1 | +package de.medizininformatikinitiative.torch.util; |
| 2 | + |
| 3 | +import ca.uhn.fhir.context.FhirContext; |
| 4 | +import ca.uhn.fhir.parser.IParser; |
| 5 | +import org.hl7.fhir.r4.model.Bundle; |
| 6 | +import org.hl7.fhir.r4.model.OperationOutcome; |
| 7 | +import org.junit.jupiter.api.AfterEach; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.util.FileSystemUtils; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +class ResultFileManagerTest { |
| 23 | + static final String RESULTS_DIR = "ResultFileManagerDir"; |
| 24 | + static final String ERROR_FILE = "error.json"; |
| 25 | + static final HttpStatus HTTP_OK = HttpStatus.OK; |
| 26 | + |
| 27 | + ResultFileManager resultFileManager = new ResultFileManager(RESULTS_DIR, "PT20S", FhirContext.forR4(), "hostname", "fileServerName"); |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + void setUp() throws IOException { |
| 31 | + var dirFile = new File(RESULTS_DIR); |
| 32 | + if (dirFile.exists()) { |
| 33 | + FileSystemUtils.deleteRecursively(dirFile); |
| 34 | + } |
| 35 | + Files.createDirectory(new File(RESULTS_DIR).toPath()); |
| 36 | + } |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + void tearDown() { |
| 40 | + FileSystemUtils.deleteRecursively(new File(RESULTS_DIR)); |
| 41 | + } |
| 42 | + |
| 43 | + private String readJobErrorFile(String jobDir) throws IOException { |
| 44 | + try (Stream<String> lines = Files.lines(Path.of(RESULTS_DIR, jobDir, ERROR_FILE))) { |
| 45 | + var linesList = lines.toList(); |
| 46 | + if (linesList.isEmpty()) { |
| 47 | + return ""; |
| 48 | + } else { |
| 49 | + return linesList.getFirst(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testSaveErrorToJson() throws IOException { |
| 57 | + var jobId = "job-102903"; |
| 58 | + var operationOutcome = new OperationOutcome(); |
| 59 | + |
| 60 | + resultFileManager.saveErrorToJson(jobId, operationOutcome, HTTP_OK).block(); |
| 61 | + |
| 62 | + assertThat(readJobErrorFile(jobId)).isEqualTo(fhirParser().encodeResourceToString(operationOutcome)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testLoadErrorDirect() throws IOException { |
| 67 | + var jobId = "job-110619"; |
| 68 | + var error = "error-110656"; |
| 69 | + Files.createDirectories(Path.of(RESULTS_DIR, jobId)); |
| 70 | + Files.writeString(Path.of(RESULTS_DIR, jobId, ERROR_FILE), error); |
| 71 | + |
| 72 | + var loadedError = resultFileManager.loadErrorFromFileSystem(jobId); |
| 73 | + |
| 74 | + assertThat(loadedError).isEqualTo(error); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testLoadErrorFileNotExists() { |
| 79 | + var jobId = "job-110619"; |
| 80 | + |
| 81 | + var loadedError = resultFileManager.loadErrorFromFileSystem(jobId); |
| 82 | + |
| 83 | + assertThat(fhirParser().parseResource(loadedError)).isInstanceOf(OperationOutcome.class); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testSaveAndLoad() { |
| 88 | + var jobId = "job-102903"; |
| 89 | + var operationOutcome = new OperationOutcome(); |
| 90 | + |
| 91 | + resultFileManager.saveErrorToJson(jobId, operationOutcome, HTTP_OK).block(); |
| 92 | + var loadedError = resultFileManager.loadErrorFromFileSystem(jobId).replace(System.lineSeparator(), ""); |
| 93 | + |
| 94 | + assertThat(loadedError).isEqualTo(fhirParser().encodeResourceToString(operationOutcome)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testLoadExistingResult_FatalAndInvalid() throws IOException { |
| 99 | + var jobId = "job-115645"; |
| 100 | + var operationOutcome = new OperationOutcome() |
| 101 | + .setIssue(List.of(new OperationOutcome.OperationOutcomeIssueComponent() |
| 102 | + .setSeverity(OperationOutcome.IssueSeverity.FATAL) |
| 103 | + .setCode(OperationOutcome.IssueType.INVALID))); |
| 104 | + Files.createDirectories(Path.of(RESULTS_DIR, jobId)); |
| 105 | + Files.writeString(Path.of(RESULTS_DIR, jobId, ERROR_FILE), fhirParser().encodeResourceToString(operationOutcome)); |
| 106 | + |
| 107 | + resultFileManager = new ResultFileManager(RESULTS_DIR, "PT20S", FhirContext.forR4(), "hostname", "fileServerName"); |
| 108 | + |
| 109 | + assertThat(resultFileManager.getStatus(jobId)).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testLoadExistingResult_NoNdjsonExists() throws IOException { |
| 114 | + var jobId = "job-115645"; |
| 115 | + var operationOutcome = new OperationOutcome() |
| 116 | + .setIssue(List.of(new OperationOutcome.OperationOutcomeIssueComponent() |
| 117 | + .setSeverity(OperationOutcome.IssueSeverity.WARNING) |
| 118 | + .setCode(OperationOutcome.IssueType.INVALID))); |
| 119 | + Files.createDirectories(Path.of(RESULTS_DIR, jobId)); |
| 120 | + Files.writeString(Path.of(RESULTS_DIR, jobId, ERROR_FILE), fhirParser().encodeResourceToString(operationOutcome)); |
| 121 | + |
| 122 | + resultFileManager = new ResultFileManager(RESULTS_DIR, "PT20S", FhirContext.forR4(), "hostname", "fileServerName"); |
| 123 | + |
| 124 | + assertThat(resultFileManager.getStatus(jobId)).isEqualTo(HttpStatus.NOT_FOUND); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testLoadExistingResult_NdjsonExists() throws IOException { |
| 129 | + var jobId = "job-115645"; |
| 130 | + Files.createDirectories(Path.of(RESULTS_DIR, jobId)); |
| 131 | + Files.writeString(Path.of(RESULTS_DIR, jobId, "bundle.ndjson"), fhirParser().encodeResourceToString(new Bundle())); |
| 132 | + |
| 133 | + resultFileManager = new ResultFileManager(RESULTS_DIR, "PT20S", FhirContext.forR4(), "hostname", "fileServerName"); |
| 134 | + |
| 135 | + assertThat(resultFileManager.getStatus(jobId)).isEqualTo(HttpStatus.OK); |
| 136 | + } |
| 137 | + |
| 138 | + IParser fhirParser() { |
| 139 | + return FhirContext.forR4().newJsonParser().setPrettyPrint(false); |
| 140 | + } |
| 141 | +} |
0 commit comments