99import ch .cyberduck .core .DisabledConnectionCallback ;
1010import ch .cyberduck .core .DisabledListProgressListener ;
1111import ch .cyberduck .core .DisabledLoginCallback ;
12+ import ch .cyberduck .core .DisabledPasswordCallback ;
1213import ch .cyberduck .core .ListService ;
1314import ch .cyberduck .core .OAuthTokens ;
1415import ch .cyberduck .core .Path ;
15- import ch .cyberduck .core .Session ;
1616import ch .cyberduck .core .SimplePathPredicate ;
1717import ch .cyberduck .core .UUIDRandomStringService ;
1818import ch .cyberduck .core .exception .AccessDeniedException ;
19- import ch .cyberduck .core .exception .BackgroundException ;
2019import ch .cyberduck .core .exception .NotfoundException ;
2120import ch .cyberduck .core .features .AttributesFinder ;
22- import ch .cyberduck .core .features .Bulk ;
2321import ch .cyberduck .core .features .Delete ;
2422import ch .cyberduck .core .features .Directory ;
2523import ch .cyberduck .core .features .Find ;
2624import ch .cyberduck .core .features .Home ;
2725import ch .cyberduck .core .features .Move ;
28- import ch .cyberduck .core .features .Read ;
2926import ch .cyberduck .core .features .Vault ;
3027import ch .cyberduck .core .features .Write ;
31- import ch .cyberduck .core .io .StatusOutputStream ;
32- import ch .cyberduck .core .transfer .Transfer ;
33- import ch .cyberduck .core .transfer .TransferItem ;
3428import ch .cyberduck .core .transfer .TransferStatus ;
3529import ch .cyberduck .core .vault .VaultCredentials ;
3630import ch .cyberduck .core .vault .VaultRegistry ;
3731
38- import org .apache .commons .io .IOUtils ;
3932import org .apache .commons .lang3 .RandomUtils ;
4033import org .apache .commons .lang3 .StringUtils ;
4134import org .apache .logging .log4j .LogManager ;
4639import org .junit .jupiter .params .provider .MethodSource ;
4740import org .openapitools .jackson .nullable .JsonNullableModule ;
4841
49- import java .io .ByteArrayInputStream ;
50- import java .io .IOException ;
51- import java .io .InputStream ;
52- import java .util .Arrays ;
5342import java .util .Collections ;
5443import java .util .EnumSet ;
5544import java .util .HashMap ;
6150import cloud .katta .client .ApiClient ;
6251import cloud .katta .client .ApiException ;
6352import cloud .katta .client .api .StorageProfileResourceApi ;
64- import cloud .katta .client .api .UsersResourceApi ;
6553import cloud .katta .client .model .S3SERVERSIDEENCRYPTION ;
6654import cloud .katta .client .model .S3STORAGECLASSES ;
6755import cloud .katta .client .model .StorageProfileDto ;
7765import cloud .katta .protocols .hub .HubVaultRegistry ;
7866import cloud .katta .testsetup .AbstractHubTest ;
7967import cloud .katta .testsetup .HubTestConfig ;
68+ import cloud .katta .testsetup .HubTestUtilities ;
8069import cloud .katta .testsetup .MethodIgnorableSource ;
8170import com .fasterxml .jackson .annotation .JsonInclude ;
8271import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -306,50 +295,34 @@ void test03AddVault(final HubTestConfig config) throws Exception {
306295 }
307296 {
308297 // encrypted file upload
309- final Path file = new Path (vault , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (Path .Type .file ));
310- byte [] content = writeRandomFile (hubSession , file , 234 );
298+ final Path file = new Path (vault , new AlphanumericRandomStringService ().random (), EnumSet .of (Path .Type .file ));
299+ final byte [] content = HubTestUtilities . write (hubSession , file , RandomUtils . nextBytes ( 234 ) );
311300 final AttributedList <Path > list = hubSession .getFeature (ListService .class ).list (vault , new DisabledListProgressListener ());
312301 assertEquals (1 , list .size ());
313302 assertEquals (file .getName (), list .get (0 ).getName ());
314-
315- byte [] actual = new byte [300 ];
316- try (final InputStream inputStream = hubSession .getFeature (Read .class ).read (file , new TransferStatus (), new DisabledConnectionCallback ())) {
317- int l = inputStream .read (actual );
318- assert l == 234 ;
319- assertArrayEquals (content , Arrays .copyOfRange (actual , 0 , l ));
320- }
303+ assertArrayEquals (content , HubTestUtilities .read (hubSession , file , content .length ));
321304 }
322305 {
323306 // directory creation and listing
324- final Path folder = new Path (vault , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (Path .Type .directory ));
325-
307+ final Path folder = new Path (vault , new AlphanumericRandomStringService ().random (), EnumSet .of (Path .Type .directory ));
326308 hubSession .getFeature (Directory .class ).mkdir (hubSession .getFeature (Write .class ), folder , new TransferStatus ());
327309 final AttributedList <Path > list = hubSession .getFeature (ListService .class ).list (vault , new DisabledListProgressListener ());
328310 assertEquals (2 , list .size ()); // a file and a folder
329-
330311 {
331312 // file upload in subfolder
332- final Path file = new Path (folder , new AlphanumericRandomStringService (25 ).random (), EnumSet .of (Path .Type .file ));
333- final byte [] content = writeRandomFile (hubSession , file , 555 );
313+ final Path file = new Path (folder , new AlphanumericRandomStringService ().random (), EnumSet .of (Path .Type .file ));
314+ final byte [] content = HubTestUtilities . write (hubSession , file , RandomUtils . nextBytes ( 555 ) );
334315 final AttributedList <Path > sublist = hubSession .getFeature (ListService .class ).list (folder , new DisabledListProgressListener ());
335316 assertEquals (1 , sublist .size ());
336317 assertEquals (file .getName (), sublist .get (0 ).getName ());
337-
338- byte [] actual = new byte [600 ];
339- try (final InputStream inputStream = hubSession .getFeature (Read .class ).read (file , new TransferStatus (), new DisabledConnectionCallback ())) {
340- int l = inputStream .read (actual );
341- assert l == 555 ;
342- assertArrayEquals (content , Arrays .copyOfRange (actual , 0 , l ));
343- }
344-
318+ assertArrayEquals (content , HubTestUtilities .read (hubSession , file , content .length ));
345319 // move operation to root folder and read again
346- hubSession .getFeature (Move .class ).move (file , new Path (vault , file .getName (), EnumSet .of (Path .Type .file )), new TransferStatus (), new Delete . DisabledCallback (), new DisabledConnectionCallback ());
347-
320+ hubSession .getFeature (Move .class ).move (file , new Path (vault , file .getName (), EnumSet .of (Path .Type .file )), new TransferStatus (),
321+ new Delete . DisabledCallback (), new DisabledConnectionCallback ());
348322 final AttributedList <Path > list2 = hubSession .getFeature (ListService .class ).list (vault , new DisabledListProgressListener ());
349323 assertEquals (3 , list2 .size ()); // 1 subfolder and 2 files
350-
351- assertEquals (1 , list2 .toStream ().map (Path ::isDirectory ).filter (Boolean ::booleanValue ).count ());
352- assertEquals (2 , list2 .toStream ().map (Path ::isFile ).filter (Boolean ::booleanValue ).count ());
324+ assertEquals (1 , list2 .filter (Path ::isDirectory ).size ());
325+ assertEquals (2 , list2 .filter (Path ::isFile ).size ());
353326 }
354327 }
355328 vaultRegistry .close (vault );
@@ -374,18 +347,36 @@ void test04SetupCode(final HubTestConfig config) throws Exception {
374347 assertEquals (vaults , feature .list (Home .root (), new DisabledListProgressListener ()));
375348 for (final Path vault : vaults ) {
376349 assertTrue (hubSession .getFeature (Find .class ).find (vault ));
350+ final AttributedList <Path > list = hubSession .getFeature (ListService .class ).list (vault , new DisabledListProgressListener ());
351+ assertEquals (3 , list .size ()); // 1 subfolder and 2 files
352+ assertEquals (1 , list .toStream ().filter (Path ::isDirectory ).count ());
353+ assertEquals (2 , list .toStream ().filter (Path ::isFile ).count ());
354+ for (Path f : list .filter (Path ::isFile )) {
355+ final long length = f .attributes ().getSize ();
356+ HubTestUtilities .read (hubSession , f , (int ) length );
357+ }
358+ for (Path d : list .filter (Path ::isDirectory )) {
359+ {
360+ // New file
361+ final Path file = new Path (d , new AlphanumericRandomStringService ().random (), EnumSet .of (Path .Type .file ));
362+ final byte [] content = HubTestUtilities .write (hubSession , file , RandomUtils .nextBytes (247 ));
363+ assertArrayEquals (content , HubTestUtilities .read (hubSession , file , content .length ));
364+ hubSession .getFeature (Delete .class ).delete (Collections .singletonList (file ), new DisabledPasswordCallback (), new Delete .DisabledCallback ());
365+ assertFalse (hubSession .getFeature (Find .class ).find (file ));
366+ }
367+ {
368+ // New directory
369+ final Path folder = new Path (d , new AlphanumericRandomStringService ().random (), EnumSet .of (Path .Type .directory ));
370+ hubSession .getFeature (Directory .class ).mkdir (hubSession .getFeature (Write .class ), folder , new TransferStatus ());
371+ hubSession .getFeature (Delete .class ).delete (Collections .singletonList (folder ), new DisabledPasswordCallback (), new Delete .DisabledCallback ());
372+ assertFalse (hubSession .getFeature (Find .class ).find (folder ));
373+ }
374+ final AttributedList <Path > sublist = hubSession .getFeature (ListService .class ).list (d , new DisabledListProgressListener ());
375+ for (Path f : sublist .filter (Path ::isFile )) {
376+ final long length = f .attributes ().getSize ();
377+ HubTestUtilities .read (hubSession , f , (int ) length );
378+ }
379+ }
377380 }
378- new UsersResourceApi (hubSession .getClient ()).apiUsersMeGet (true , false );
379- }
380-
381- private static byte [] writeRandomFile (final Session <?> session , final Path file , int size ) throws BackgroundException , IOException {
382- final byte [] content = RandomUtils .nextBytes (size );
383- final TransferStatus transferStatus = new TransferStatus ().setLength (content .length );
384- transferStatus .setChecksum (session .getFeature (Write .class ).checksum (file , transferStatus ).compute (new ByteArrayInputStream (content ), transferStatus ));
385- session .getFeature (Bulk .class ).pre (Transfer .Type .upload , Collections .singletonMap (new TransferItem (file ), transferStatus ), new DisabledConnectionCallback ());
386- final StatusOutputStream <?> out = session .getFeature (Write .class ).write (file , transferStatus , new DisabledConnectionCallback ());
387- IOUtils .copyLarge (new ByteArrayInputStream (content ), out );
388- out .close ();
389- return content ;
390381 }
391382}
0 commit comments