3535import org .entando .entando .aps .system .services .tenants .ITenantManager ;
3636import org .entando .entando .aps .system .services .tenants .TenantConfig ;
3737import org .entando .entando .ent .exception .EntException ;
38+ import org .entando .entando .ent .exception .EntResourceNotFoundException ;
3839import org .entando .entando .ent .exception .EntRuntimeException ;
3940import org .junit .jupiter .api .Test ;
4041import org .junit .jupiter .api .extension .ExtendWith ;
@@ -238,13 +239,19 @@ void shouldManageErrorWhenCallGetStream() throws Exception {
238239 ).isInstanceOf (EntRuntimeException .class ).hasMessageStartingWith ("Error validating path" );
239240
240241 String testFilePath = "/testfolder/test.txt" ;
242+
243+ Assertions .assertThatThrownBy (
244+ ()-> cdsStorageManager .getStream (testFilePath , false )
245+ ).isInstanceOf (EntResourceNotFoundException .class ).hasMessageStartingWith ("File \" " + testFilePath );
246+
241247 URI testFile = URI .create ( baseUrl + "/custom-path" + testFilePath );
248+ this .mockExecuteListFolder ("http://cds-kube-service:8081/mytenant/api/v1/list/protected/testfolder" , "test.txt" );
242249 Mockito .when (cdsRemoteCaller .getFile (eq (testFile ),
243250 any (),
244251 eq (false ))).thenReturn (null );
245252 Assertions .assertThatThrownBy (
246253 ()-> cdsStorageManager .getStream (testFilePath ,false )
247- ).isInstanceOf (EntException .class ).hasMessageStartingWith ("Error extracting file" );
254+ ).isInstanceOf (EntResourceNotFoundException .class ).hasMessageStartingWith ("Error extracting file" );
248255
249256 String testFilePathBadGateway = "/testfolder/test-badgw.txt" ;
250257 URI testFileBadGateway = URI .create ( baseUrl + "/custom-path" + testFilePathBadGateway );
@@ -256,15 +263,9 @@ void shouldManageErrorWhenCallGetStream() throws Exception {
256263 ()-> cdsStorageManager .getStream (testFilePathBadGateway ,false )
257264 ).isInstanceOf (EntException .class ).hasMessageStartingWith ("Error extracting file" );
258265
259-
260266 String testFilePathNotFound = "/testfolder/test-notfound.txt" ;
261- URI testFileNotFound = URI .create ( baseUrl + "/custom-path" + testFilePathNotFound );
262- Mockito .when (cdsRemoteCaller .getFile (eq (testFileNotFound ),
263- any (),
264- eq (false ))).thenThrow (new HttpClientErrorException (HttpStatus .NOT_FOUND ));
265-
266- Assertions .assertThat (cdsStorageManager .getStream (testFilePathNotFound ,false )).isNull ();
267-
267+ Assertions .assertThatThrownBy (() -> cdsStorageManager .getStream (testFilePathNotFound ,false ))
268+ .isInstanceOf (EntResourceNotFoundException .class ).hasMessageStartingWith ("Error extracting file" );
268269 }
269270
270271 @ Test
@@ -276,20 +277,22 @@ void shouldReturnDataWhenCallGetStream() throws Exception {
276277 "cdsPath" ,"/mytenant/api/v1/" );
277278 TenantConfig tc = new TenantConfig (configMap );
278279 Mockito .when (tenantManager .getConfig ("my-tenant" )).thenReturn (Optional .ofNullable (tc ));
279-
280+
280281 Mockito .when (cdsRemoteCaller .getFile (eq (URI .create ("http://my-server/tenant1/cms-resources/test-folder/test.txt" )),
281282 any (),
282283 eq (false ))).thenReturn (Optional .ofNullable (new ByteArrayInputStream ("text random" .getBytes (StandardCharsets .UTF_8 ))));
283-
284+
285+ this .mockExecuteListFolder ("http://cds-kube-service:8081/mytenant/api/v1/list/test-folder" , "test.txt" );
284286 ApsTenantApplicationUtils .setTenant ("my-tenant" );
285287 InputStream is = cdsStorageManager .getStream (testFilePath ,false );
286288 Assertions .assertThat (new BufferedReader (new InputStreamReader (is ))
287289 .lines ().collect (Collectors .joining ("" ))).isEqualTo ("text random" );
288-
290+
289291 Mockito .when (cdsRemoteCaller .getFile (eq (URI .create ("http://cds-kube-service:8081/mytenant/api/v1/protected/test-folder/test.txt" )),
290292 any (),
291293 eq (true ))).thenReturn (Optional .ofNullable (new ByteArrayInputStream ("text random" .getBytes (StandardCharsets .UTF_8 ))));
292-
294+
295+ this .mockExecuteListFolder ("http://cds-kube-service:8081/mytenant/api/v1/list/protected/test-folder" , "test.txt" );
293296 is = cdsStorageManager .getStream (testFilePath ,true );
294297 Assertions .assertThat (new BufferedReader (new InputStreamReader (is ))
295298 .lines ().collect (Collectors .joining ("" ))).isEqualTo ("text random" );
@@ -466,24 +469,25 @@ void shouldReadFile() throws Exception {
466469 String testFilePath = "/testfolder/test.txt" ;
467470
468471 Map <String ,String > configMap = Map .of ("cdsPublicUrl" ,"http://my-server/tenant1/cms-resources" ,
469- "cdsPrivateUrl" ,"http://cds -kube-service:8081/" ,
472+ "cdsPrivateUrl" ,"http://cdsmaster -kube-service:8081/" ,
470473 "cdsPath" ,"/mytenant/api/v1/" );
471474 TenantConfig tc = new TenantConfig (configMap );
472475 Mockito .when (tenantManager .getConfig ("my-tenant" )).thenReturn (Optional .ofNullable (tc ));
473476
474477 Mockito .when (cdsRemoteCaller .getFile (any (),
475478 any (),
476479 eq (false ))).thenReturn (Optional .ofNullable (new ByteArrayInputStream ("text random" .getBytes (StandardCharsets .UTF_8 ))));
477-
478-
480+
481+ this .mockExecuteListFolder ("http://cdsmaster-kube-service:8081/mytenant/api/v1/list/testfolder" , "test.txt" );
482+
479483 ApsTenantApplicationUtils .setTenant ("my-tenant" );
480484 Assertions .assertThat (cdsStorageManager .readFile (testFilePath ,false ))
481485 .isEqualTo ("text random" );
482486 }
483487
484488 @ Test
485489 void shouldManageExceptionWhenReadFile () throws Exception {
486- String testFilePath = "/testfolder/test.txt" ;
490+ String testFilePath = "/testfolder/subfolder/ test.txt" ;
487491
488492 Map <String ,String > configMap = Map .of ("cdsPublicUrl" ,"http://my-server/tenant1/cms-resources" ,
489493 "cdsPrivateUrl" ,"http://cds-kube-service:8081/" ,
@@ -500,7 +504,7 @@ void shouldManageExceptionWhenReadFile() throws Exception {
500504 try (MockedStatic <IOUtils > ioUtils = Mockito .mockStatic (IOUtils .class )) {
501505 ioUtils .when (() -> IOUtils .toString (any (InputStream .class ), eq (StandardCharsets .UTF_8 )))
502506 .thenThrow (new IOException ());
503-
507+ this . mockExecuteListFolder ( "http://cds-kube-service:8081/mytenant/api/v1/list/testfolder/subfolder" , "test.txt" );
504508 Assertions .assertThatThrownBy (() -> cdsStorageManager .readFile (testFilePath , false ))
505509 .isInstanceOf (EntException .class )
506510 .hasMessageStartingWith ("Error extracting text" );
@@ -585,5 +589,12 @@ void testListAttributes() throws Throwable {
585589 }
586590 assertTrue (containsCms );
587591 }
592+
593+ private void mockExecuteListFolder (String uri , String filename ) {
594+ CdsFileAttributeViewDto file = new CdsFileAttributeViewDto ();
595+ file .setName (filename );
596+ Mockito .when (cdsRemoteCaller .getFileAttributeView (eq (URI .create (uri )),
597+ any ())).thenReturn (Optional .ofNullable (new CdsFileAttributeViewDto []{file }));
598+ }
588599
589600}
0 commit comments