11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Net ;
56using System . Threading . Tasks ;
1213using SharpGrip . FileSystem . Adapters . AmazonS3 ;
1314using SharpGrip . FileSystem . Exceptions ;
1415using Xunit ;
16+ using DirectoryNotFoundException = SharpGrip . FileSystem . Exceptions . DirectoryNotFoundException ;
17+ using FileNotFoundException = SharpGrip . FileSystem . Exceptions . FileNotFoundException ;
1518
1619namespace Tests . FileSystem . Adapters . AmazonS3
1720{
@@ -356,28 +359,188 @@ public async Task Test_Delete_Directory_Async()
356359 await fileSystem . DeleteDirectoryAsync ( "prefix-1://test5" ) ;
357360 }
358361
362+ [ Fact ]
363+ public async Task Test_Read_File_Stream_Async ( )
364+ {
365+ var amazonS3Client = Substitute . For < IAmazonS3 > ( ) ;
366+ var amazonS3Adapter = new AmazonS3Adapter ( "prefix-1" , "root-path-1" , amazonS3Client , "bucket-1" ) ;
367+ var fileSystem = new SharpGrip . FileSystem . FileSystem ( new List < IAdapter > { amazonS3Adapter } ) ;
368+
369+ var getObjectResponse1 = Substitute . For < GetObjectResponse > ( ) ;
370+ var getObjectResponse2 = Substitute . For < GetObjectResponse > ( ) ;
371+
372+ getObjectResponse1 . Key = "test1.txt" ;
373+ getObjectResponse1 . ContentLength = 1 ;
374+ getObjectResponse1 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
375+
376+ getObjectResponse2 . Key = "test1.txt" ;
377+ getObjectResponse2 . ContentLength = 1 ;
378+ getObjectResponse2 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
379+ getObjectResponse2 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
380+
381+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test1.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 ) ;
382+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test2.txt" ) . ThrowsAsync ( amazonS3NoSuchKeyException ) ;
383+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test3.txt" ) . ThrowsAsync ( amazonS3InvalidAccessKeyIdException ) ;
384+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test4.txt" ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
385+
386+ var fileStream = await fileSystem . ReadFileStreamAsync ( "prefix-1://test1.txt" ) ;
387+ var streamReader = new StreamReader ( fileStream ) ;
388+
389+ Assert . Equal ( "test1" , await streamReader . ReadToEndAsync ( ) ) ;
390+ await Assert . ThrowsAsync < FileNotFoundException > ( ( ) => fileSystem . ReadFileStreamAsync ( "prefix-1://test2.txt" ) ) ;
391+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileStreamAsync ( "prefix-1://test3.txt" ) ) ;
392+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileStreamAsync ( "prefix-1://test4.txt" ) ) ;
393+ }
394+
359395 [ Fact ]
360396 public async Task Test_Read_File_Async ( )
361397 {
362- await Task . CompletedTask ;
398+ var amazonS3Client = Substitute . For < IAmazonS3 > ( ) ;
399+ var amazonS3Adapter = new AmazonS3Adapter ( "prefix-1" , "root-path-1" , amazonS3Client , "bucket-1" ) ;
400+ var fileSystem = new SharpGrip . FileSystem . FileSystem ( new List < IAdapter > { amazonS3Adapter } ) ;
401+
402+ var getObjectResponse1 = Substitute . For < GetObjectResponse > ( ) ;
403+ var getObjectResponse2 = Substitute . For < GetObjectResponse > ( ) ;
404+ var getObjectResponse3 = Substitute . For < GetObjectResponse > ( ) ;
405+
406+ getObjectResponse1 . Key = "test1.txt" ;
407+ getObjectResponse1 . ContentLength = 1 ;
408+ getObjectResponse1 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
409+
410+ getObjectResponse2 . Key = "test1.txt" ;
411+ getObjectResponse2 . ContentLength = 1 ;
412+ getObjectResponse2 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
413+
414+ getObjectResponse3 . Key = "test1.txt" ;
415+ getObjectResponse3 . ContentLength = 1 ;
416+ getObjectResponse3 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
417+ getObjectResponse3 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
418+
419+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test1.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 , getObjectResponse3 ) ;
420+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test2.txt" ) . ThrowsAsync ( amazonS3NoSuchKeyException ) ;
421+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test3.txt" ) . ThrowsAsync ( amazonS3InvalidAccessKeyIdException ) ;
422+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test4.txt" ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
423+
424+ var fileContents = await fileSystem . ReadFileAsync ( "prefix-1://test1.txt" ) ;
425+
426+ Assert . Equal ( "test1" , System . Text . Encoding . UTF8 . GetString ( fileContents ) ) ;
427+ await Assert . ThrowsAsync < FileNotFoundException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test2.txt" ) ) ;
428+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test3.txt" ) ) ;
429+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test4.txt" ) ) ;
363430 }
364431
365432 [ Fact ]
366433 public async Task Test_Read_Text_File_Async ( )
367434 {
368- await Task . CompletedTask ;
435+ var amazonS3Client = Substitute . For < IAmazonS3 > ( ) ;
436+ var amazonS3Adapter = new AmazonS3Adapter ( "prefix-1" , "root-path-1" , amazonS3Client , "bucket-1" ) ;
437+ var fileSystem = new SharpGrip . FileSystem . FileSystem ( new List < IAdapter > { amazonS3Adapter } ) ;
438+
439+ var getObjectResponse1 = Substitute . For < GetObjectResponse > ( ) ;
440+ var getObjectResponse2 = Substitute . For < GetObjectResponse > ( ) ;
441+ var getObjectResponse3 = Substitute . For < GetObjectResponse > ( ) ;
442+
443+ getObjectResponse1 . Key = "test1.txt" ;
444+ getObjectResponse1 . ContentLength = 1 ;
445+ getObjectResponse1 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
446+
447+ getObjectResponse2 . Key = "test1.txt" ;
448+ getObjectResponse2 . ContentLength = 1 ;
449+ getObjectResponse2 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
450+
451+ getObjectResponse3 . Key = "test1.txt" ;
452+ getObjectResponse3 . ContentLength = 1 ;
453+ getObjectResponse3 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
454+ getObjectResponse3 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
455+
456+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test1.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 , getObjectResponse3 ) ;
457+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test2.txt" ) . ThrowsAsync ( amazonS3NoSuchKeyException ) ;
458+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test3.txt" ) . ThrowsAsync ( amazonS3InvalidAccessKeyIdException ) ;
459+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test4.txt" ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
460+
461+ var fileContents = await fileSystem . ReadTextFileAsync ( "prefix-1://test1.txt" ) ;
462+
463+ Assert . Equal ( "test1" , fileContents ) ;
464+ await Assert . ThrowsAsync < FileNotFoundException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test2.txt" ) ) ;
465+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test3.txt" ) ) ;
466+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . ReadFileAsync ( "prefix-1://test4.txt" ) ) ;
369467 }
370468
371469 [ Fact ]
372470 public async Task Test_Write_File_Async ( )
373471 {
374- await Task . CompletedTask ;
472+ var amazonS3Client = Substitute . For < IAmazonS3 > ( ) ;
473+ var amazonS3Adapter = new AmazonS3Adapter ( "prefix-1" , "root-path-1" , amazonS3Client , "bucket-1" ) ;
474+ var fileSystem = new SharpGrip . FileSystem . FileSystem ( new List < IAdapter > { amazonS3Adapter } ) ;
475+
476+ var getObjectResponse = Substitute . For < GetObjectResponse > ( ) ;
477+
478+ getObjectResponse . Key = "test1.txt" ;
479+ getObjectResponse . ContentLength = 1 ;
480+ getObjectResponse . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
481+
482+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test1.txt" ) . Returns ( getObjectResponse ) ;
483+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test2.txt" ) . Returns ( getObjectResponse ) ;
484+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test3.txt" ) . Returns ( getObjectResponse ) ;
485+ amazonS3Client . PutObjectAsync ( Arg . Is < PutObjectRequest > ( x => x . BucketName == "bucket-1" && x . Key == "root-path-1/test2.txt" ) ) . ThrowsAsync ( amazonS3InvalidAccessKeyIdException ) ;
486+ amazonS3Client . PutObjectAsync ( Arg . Is < PutObjectRequest > ( x => x . BucketName == "bucket-1" && x . Key == "root-path-1/test3.txt" ) ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
487+
488+ await fileSystem . WriteFileAsync ( "prefix-1://test1.txt" , new MemoryStream ( ) , true ) ;
489+ await Assert . ThrowsAsync < FileExistsException > ( ( ) => fileSystem . WriteFileAsync ( "prefix-1://test1.txt" , new MemoryStream ( ) ) ) ;
490+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . WriteFileAsync ( "prefix-1://test2.txt" , new MemoryStream ( ) , true ) ) ;
491+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . WriteFileAsync ( "prefix-1://test3.txt" , new MemoryStream ( ) , true ) ) ;
375492 }
376493
377494 [ Fact ]
378495 public async Task Test_Append_File_Async ( )
379496 {
380- await Task . CompletedTask ;
497+ var amazonS3Client = Substitute . For < IAmazonS3 > ( ) ;
498+ var amazonS3Adapter = new AmazonS3Adapter ( "prefix-1" , "root-path-1" , amazonS3Client , "bucket-1" ) ;
499+ var fileSystem = new SharpGrip . FileSystem . FileSystem ( new List < IAdapter > { amazonS3Adapter } ) ;
500+
501+ var getObjectResponse1 = Substitute . For < GetObjectResponse > ( ) ;
502+ var getObjectResponse2 = Substitute . For < GetObjectResponse > ( ) ;
503+ var getObjectResponse3 = Substitute . For < GetObjectResponse > ( ) ;
504+ var getObjectResponse4 = Substitute . For < GetObjectResponse > ( ) ;
505+ var getObjectResponse5 = Substitute . For < GetObjectResponse > ( ) ;
506+
507+ getObjectResponse1 . Key = "test1.txt" ;
508+ getObjectResponse1 . ContentLength = 1 ;
509+ getObjectResponse1 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
510+ getObjectResponse3 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
511+
512+ getObjectResponse2 . Key = "test1.txt" ;
513+ getObjectResponse2 . ContentLength = 1 ;
514+ getObjectResponse2 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
515+ getObjectResponse3 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
516+
517+ getObjectResponse3 . Key = "test1.txt" ;
518+ getObjectResponse3 . ContentLength = 1 ;
519+ getObjectResponse3 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
520+ getObjectResponse3 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
521+
522+ getObjectResponse4 . Key = "test1.txt" ;
523+ getObjectResponse4 . ContentLength = 1 ;
524+ getObjectResponse4 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
525+ getObjectResponse4 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
526+
527+ getObjectResponse5 . Key = "test1.txt" ;
528+ getObjectResponse5 . ContentLength = 1 ;
529+ getObjectResponse5 . LastModified = new DateTime ( 1970 , 1 , 1 ) ;
530+ getObjectResponse5 . ResponseStream = new MemoryStream ( "test1"u8 . ToArray ( ) ) ;
531+
532+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test1.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 , getObjectResponse3 , getObjectResponse4 , getObjectResponse5 ) ;
533+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test2.txt" ) . ThrowsAsync ( amazonS3NoSuchKeyException ) ;
534+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test3.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 , getObjectResponse3 , getObjectResponse4 , getObjectResponse5 ) ;
535+ amazonS3Client . GetObjectAsync ( "bucket-1" , "root-path-1/test4.txt" ) . Returns ( getObjectResponse1 , getObjectResponse2 , getObjectResponse3 , getObjectResponse4 , getObjectResponse5 ) ;
536+ amazonS3Client . PutObjectAsync ( Arg . Is < PutObjectRequest > ( x => x . BucketName == "bucket-1" && x . Key == "root-path-1/test2.txt" ) ) . ThrowsAsync ( amazonS3InvalidAccessKeyIdException ) ;
537+ amazonS3Client . PutObjectAsync ( Arg . Is < PutObjectRequest > ( x => x . BucketName == "bucket-1" && x . Key == "root-path-1/test3.txt" ) ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
538+ amazonS3Client . PutObjectAsync ( Arg . Is < PutObjectRequest > ( x => x . BucketName == "bucket-1" && x . Key == "root-path-1/test4.txt" ) ) . ThrowsAsync ( amazonS3InvalidSecurityException ) ;
539+
540+ await fileSystem . AppendFileAsync ( "prefix-1://test1.txt" , new MemoryStream ( ) ) ;
541+ await Assert . ThrowsAsync < FileNotFoundException > ( ( ) => fileSystem . AppendFileAsync ( "prefix-1://test2.txt" , new MemoryStream ( ) ) ) ;
542+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . AppendFileAsync ( "prefix-1://test3.txt" , new MemoryStream ( ) ) ) ;
543+ await Assert . ThrowsAsync < ConnectionException > ( ( ) => fileSystem . AppendFileAsync ( "prefix-1://test4.txt" , new MemoryStream ( ) ) ) ;
381544 }
382545 }
383546}
0 commit comments