41
41
42
42
import java .io .IOException ;
43
43
import java .io .InputStream ;
44
+ import java .nio .file .FileAlreadyExistsException ;
44
45
import java .nio .file .Path ;
45
46
import java .util .List ;
46
47
import java .util .Map ;
49
50
import io .cryostat .MainModule ;
50
51
import io .cryostat .configuration .CredentialsManager ;
51
52
import io .cryostat .core .agent .LocalProbeTemplateService ;
53
+ import io .cryostat .core .agent .ProbeTemplate ;
52
54
import io .cryostat .core .agent .ProbeValidationException ;
53
55
import io .cryostat .core .log .Logger ;
54
56
import io .cryostat .core .sys .FileSystem ;
@@ -202,7 +204,7 @@ void shouldRespond400IfXmlInvalid() throws Exception {
202
204
}
203
205
204
206
@ Test
205
- void shouldProcessGoodRequest () throws Exception {
207
+ void shouldRespond400IfFileExists () throws Exception {
206
208
FileUpload upload = Mockito .mock (FileUpload .class );
207
209
Mockito .when (upload .name ()).thenReturn ("probeTemplate" );
208
210
Mockito .when (upload .uploadedFileName ()).thenReturn ("/file-uploads/abcd-1234" );
@@ -214,23 +216,52 @@ void shouldProcessGoodRequest() throws Exception {
214
216
Path uploadPath = Mockito .mock (Path .class );
215
217
Mockito .when (fs .pathOf ("/file-uploads/abcd-1234" )).thenReturn (uploadPath );
216
218
219
+ InputStream stream = Mockito .mock (InputStream .class );
220
+ Mockito .when (fs .newInputStream (Mockito .any ())).thenReturn (stream );
221
+
222
+ Mockito .doThrow (FileAlreadyExistsException .class )
223
+ .when (templateService )
224
+ .addTemplate (stream , "foo.xml" );
225
+
226
+ ApiException ex =
227
+ Assertions .assertThrows (
228
+ ApiException .class , () -> handler .handle (requestParams ));
229
+ MatcherAssert .assertThat (ex .getStatusCode (), Matchers .equalTo (400 ));
230
+ Mockito .verify (fs ).deleteIfExists (uploadPath );
231
+ }
232
+
233
+ @ Test
234
+ void shouldProcessGoodRequest () throws Exception {
235
+ String templateName = "foo.xml" ;
236
+ FileUpload upload = Mockito .mock (FileUpload .class );
237
+ Mockito .when (upload .name ()).thenReturn ("probeTemplate" );
238
+ Mockito .when (upload .uploadedFileName ()).thenReturn ("/file-uploads/abcd-1234" );
239
+
240
+ Mockito .when (requestParams .getFileUploads ()).thenReturn (Set .of (upload ));
241
+ Mockito .when (requestParams .getPathParams ())
242
+ .thenReturn (Map .of ("probetemplateName" , templateName ));
243
+
244
+ Path uploadPath = Mockito .mock (Path .class );
245
+ Mockito .when (fs .pathOf ("/file-uploads/abcd-1234" )).thenReturn (uploadPath );
246
+
217
247
InputStream stream = Mockito .mock (InputStream .class );
218
248
Mockito .when (fs .newInputStream (uploadPath )).thenReturn (stream );
219
- Mockito .when (templateService .getTemplate (Mockito .anyString ()))
220
- .thenReturn ("someContent" );
249
+
250
+ ProbeTemplate template = Mockito .mock (ProbeTemplate .class );
251
+ String templateContent = "someContent" ;
252
+ Mockito .when (templateService .addTemplate (stream , templateName )).thenReturn (template );
253
+ Mockito .when (template .serialize ()).thenReturn (templateContent );
221
254
222
255
IntermediateResponse <Void > response = handler .handle (requestParams );
223
256
224
- Mockito .verify (templateService ).addTemplate (stream , "foo.xml" );
257
+ Mockito .verify (templateService ).addTemplate (stream , templateName );
225
258
Mockito .verify (notificationBuilder )
226
259
.message (
227
260
Map .of (
228
261
"probeTemplate" ,
229
- "/file-uploads/abcd-1234" ,
230
- "templateName" ,
231
- "foo.xml" ,
262
+ templateName ,
232
263
"templateContent" ,
233
- "someContent" ));
264
+ templateContent ));
234
265
Mockito .verifyNoMoreInteractions (templateService );
235
266
Mockito .verify (fs ).deleteIfExists (uploadPath );
236
267
0 commit comments