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