Skip to content

Commit f649ec0

Browse files
bastianschafferLucas0T
authored andcommitted
Add Test For ResultFileManager
1 parent 53a1339 commit f649ec0

File tree

5 files changed

+155
-35
lines changed

5 files changed

+155
-35
lines changed

src/main/java/de/medizininformatikinitiative/torch/management/OperationOutcomeCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static OperationOutcome createOperationOutcome(String jobId, Throwable th
1111
OperationOutcome.OperationOutcomeIssueComponent issueComponent = new OperationOutcome.OperationOutcomeIssueComponent();
1212
issueComponent.setSeverity(OperationOutcome.IssueSeverity.FATAL);
1313
issueComponent.setCode(createIssueType(throwable));
14-
issueComponent.setDiagnostics(throwable.getClass().getSimpleName() + ": " + throwable.getMessage());
14+
issueComponent.setDiagnostics(throwable.getMessage());
1515
operationOutcome.addIssue(issueComponent);
1616
return operationOutcome;
1717
}

src/main/java/de/medizininformatikinitiative/torch/util/ResultFileManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Map<String, HttpStatus> getJobStatusMap() {
7777
return jobStatusMap;
7878
}
7979

80-
public void loadExistingResults() {
80+
private void loadExistingResults() {
8181
try (Stream<Path> jobDirs = Files.list(resultsDirPath)) {
8282
jobDirs.filter(Files::isDirectory)
8383
.forEach(jobDir -> {

src/test/java/de/medizininformatikinitiative/torch/FhirControllerIT.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
import de.numcodex.sq2cql.Translator;
1818
import de.numcodex.sq2cql.model.structured_query.StructuredQuery;
1919
import org.hl7.fhir.r4.model.OperationOutcome;
20-
import org.junit.jupiter.api.Assertions;
21-
import org.junit.jupiter.api.BeforeAll;
22-
import org.junit.jupiter.api.Nested;
23-
import org.junit.jupiter.api.Test;
24-
import org.junit.jupiter.api.TestInstance;
20+
import org.junit.jupiter.api.*;
2521
import org.junit.jupiter.params.ParameterizedTest;
2622
import org.junit.jupiter.params.provider.ValueSource;
2723
import org.slf4j.Logger;
@@ -31,11 +27,7 @@
3127
import org.springframework.beans.factory.annotation.Value;
3228
import org.springframework.boot.test.context.SpringBootTest;
3329
import org.springframework.boot.test.web.client.TestRestTemplate;
34-
import org.springframework.http.HttpEntity;
35-
import org.springframework.http.HttpHeaders;
36-
import org.springframework.http.HttpMethod;
37-
import org.springframework.http.MediaType;
38-
import org.springframework.http.ResponseEntity;
30+
import org.springframework.http.*;
3931
import org.springframework.test.annotation.DirtiesContext;
4032
import org.springframework.test.context.ActiveProfiles;
4133
import org.springframework.web.client.HttpStatusCodeException;
@@ -300,27 +292,16 @@ void emptyRequestBodyReturnsBadRequest() {
300292
}
301293

302294
@Test
303-
void invalidCRTDLReturnsBadRequest() {
295+
void invalidCRTDLReturnsBadRequest() throws IOException {
304296
TestRestTemplate restTemplate = new TestRestTemplate();
305297
HttpHeaders headers = new HttpHeaders();
306298
headers.add("content-type", "application/fhir+json");
307-
308-
try {
309-
String fileContent = Files.readString(Paths.get("src/test/resources/CRTDL_Parameters/Parameters_invalid_CRTDL.json"), StandardCharsets.UTF_8);
310-
HttpEntity<String> entity = new HttpEntity<>(fileContent, headers);
311-
try {
312-
long start = System.nanoTime();
313-
ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/fhir/$extract-data", HttpMethod.POST, entity, String.class);
314-
assertThat(response.getStatusCode().value()).isEqualTo(400);
315-
assertThat(durationSecondsSince(start)).isLessThan(1);
316-
} catch (HttpStatusCodeException e) {
317-
logger.error("HTTP Status code error: {}", e.getStatusCode(), e);
318-
Assertions.fail("HTTP request failed with status code: " + e.getStatusCode());
319-
}
320-
321-
} catch (IOException e) {
322-
logger.error("CRTDL file not found", e);
323-
}
299+
String fileContent = Files.readString(Paths.get("src/test/resources/CRTDL_Parameters/Parameters_invalid_CRTDL.json"), StandardCharsets.UTF_8);
300+
HttpEntity<String> entity = new HttpEntity<>(fileContent, headers);
301+
long start = System.nanoTime();
302+
ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/fhir/$extract-data", HttpMethod.POST, entity, String.class);
303+
assertThat(response.getStatusCode().value()).isEqualTo(400);
304+
assertThat(durationSecondsSince(start)).isLessThan(1);
324305
}
325306

326307
}

src/test/java/de/medizininformatikinitiative/torch/rest/FhirControllerTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
import java.util.Map;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.mockito.ArgumentMatchers.any;
30-
import static org.mockito.ArgumentMatchers.anyString;
31-
import static org.mockito.ArgumentMatchers.eq;
29+
import static org.mockito.ArgumentMatchers.*;
3230
import static org.mockito.Mockito.when;
3331

3432
@ExtendWith(MockitoExtension.class)
3533
class FhirControllerTest {
3634

37-
private final static String BASE_URL = "http://base-url";
35+
private static final String BASE_URL = "http://base-url";
3836

3937
@Mock
4038
ResultFileManager resultFileManager;
@@ -199,7 +197,7 @@ void invalidCrtdlTriggersBadRequest() throws ValidationException {
199197
response.expectStatus().isBadRequest().expectHeader().contentType("application/fhir+json")
200198
.expectBody()
201199
.jsonPath("$.resourceType").isEqualTo("OperationOutcome")
202-
.jsonPath("$.issue[0].diagnostics").isEqualTo("IllegalArgumentException: Invalid CRTDL");
200+
.jsonPath("$.issue[0].diagnostics").isEqualTo("Invalid CRTDL");
203201
}
204202
}
205203
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)