21
21
import java .nio .file .Path ;
22
22
import java .nio .file .StandardOpenOption ;
23
23
import java .util .Set ;
24
+ import java .util .concurrent .CompletionException ;
25
+ import java .util .concurrent .ExecutionException ;
24
26
import java .util .concurrent .TimeoutException ;
25
27
import java .util .stream .Stream ;
26
28
32
34
import static org .assertj .core .api .Assertions .assertThat ;
33
35
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
34
36
import static org .mockito .ArgumentMatchers .any ;
37
+ import static org .mockito .Mockito .doThrow ;
35
38
import static org .mockito .Mockito .mock ;
36
39
import static org .mockito .Mockito .times ;
37
40
import static org .mockito .Mockito .verify ;
38
41
import static org .mockito .Mockito .when ;
39
42
import software .amazon .awssdk .services .s3 .S3AsyncClient ;
43
+ import software .amazon .awssdk .services .s3 .model .S3Exception ;
40
44
41
45
@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
42
46
class S3WritableByteChannelTest {
@@ -55,18 +59,69 @@ void whenFileExistsAndCreateNewShouldThrowFileAlreadyExistsException() throws In
55
59
.isInstanceOf (FileAlreadyExistsException .class );
56
60
}
57
61
62
+ @ Test
63
+ @ DisplayName ("when file does not exist and constructor is invoked the option `CREATE` option" )
64
+ void whenFileDoesNotExistsAndCreateOptionIsPresent () throws Exception {
65
+ var provider = mock (S3FileSystemProvider .class );
66
+ var fs = mock (S3FileSystem .class );
67
+ when (fs .provider ()).thenReturn (provider );
68
+ var file = S3Path .getPath (fs , "somefile" );
69
+ var s3Client = mock (S3AsyncClient .class );
70
+ var transferManager = mock (S3TransferUtil .class );
71
+ var exception = new CompletionException (S3Exception .builder ().statusCode (404 ).build ());
72
+ doThrow (exception ).when (transferManager ).downloadToLocalFile (any (S3Path .class ), any (Path .class ));
73
+
74
+ try (var channel = new S3WritableByteChannel (file , s3Client , transferManager , Set .of (CREATE ))) {
75
+ assertThat (channel .position ()).isZero ();
76
+ }
77
+ }
78
+
58
79
@ Test
59
80
@ DisplayName ("when file does not exist and constructor is invoked without option `CREATE_NEW` nor `CREATE` should throw NoSuchFileException" )
60
- void whenFileDoesNotExistsAndNoCreateNewShouldThrowNoSuchFileException () throws InterruptedException , TimeoutException {
61
- S3FileSystemProvider provider = mock ();
62
- when (provider .exists (any (S3AsyncClient .class ), any ())).thenReturn (false );
81
+ void whenFileDoesNotExistsAndNoCreateShouldThrowNoSuchFileException () throws Exception {
82
+ var provider = mock (S3FileSystemProvider .class );
83
+ var fs = mock (S3FileSystem .class );
84
+ when (fs .provider ()).thenReturn (provider );
85
+ var file = S3Path .getPath (fs , "somefile" );
86
+ var s3Client = mock (S3AsyncClient .class );
87
+ var transferManager = mock (S3TransferUtil .class );
88
+ var exception = new CompletionException (S3Exception .builder ().statusCode (404 ).build ());
89
+ doThrow (exception ).when (transferManager ).downloadToLocalFile (any (S3Path .class ), any (Path .class ));
63
90
64
- S3FileSystem fs = mock ();
91
+ assertThatThrownBy (() -> new S3WritableByteChannel (file , s3Client , transferManager , emptySet ()))
92
+ .isInstanceOf (NoSuchFileException .class );
93
+ }
94
+
95
+ @ Test
96
+ @ DisplayName ("when file the download fails due to an invalid request" )
97
+ void whenFileDownloadFailsDueToInvalidRequestTheExceptionShouldBePropagated () throws InterruptedException , TimeoutException , ExecutionException {
98
+ var provider = mock (S3FileSystemProvider .class );
99
+ var fs = mock (S3FileSystem .class );
65
100
when (fs .provider ()).thenReturn (provider );
101
+ var file = S3Path .getPath (fs , "somefile" );
102
+ var s3Client = mock (S3AsyncClient .class );
103
+ var transferManager = mock (S3TransferUtil .class );
104
+ var exception = new CompletionException (S3Exception .builder ().statusCode (400 ).message ("Invalid Request" ).build ());
105
+ doThrow (exception ).when (transferManager ).downloadToLocalFile (any (S3Path .class ), any (Path .class ));
66
106
107
+ assertThatThrownBy (() -> new S3WritableByteChannel (file , s3Client , transferManager , emptySet ()))
108
+ .isEqualTo (exception );
109
+ }
110
+
111
+ @ Test
112
+ @ DisplayName ("when file the download fails for an unknown reason" )
113
+ void whenFileDownloadFailsForUnknownReasonTheExceptionShouldBePropagated () throws InterruptedException , TimeoutException , ExecutionException {
114
+ var provider = mock (S3FileSystemProvider .class );
115
+ var fs = mock (S3FileSystem .class );
116
+ when (fs .provider ()).thenReturn (provider );
67
117
var file = S3Path .getPath (fs , "somefile" );
68
- assertThatThrownBy (() -> new S3WritableByteChannel (file , mock (), mock (), emptySet ()))
69
- .isInstanceOf (NoSuchFileException .class );
118
+ var s3Client = mock (S3AsyncClient .class );
119
+ var transferManager = mock (S3TransferUtil .class );
120
+ var exception = new CompletionException (new RuntimeException ("unknown error" ));
121
+ doThrow (exception ).when (transferManager ).downloadToLocalFile (any (S3Path .class ), any (Path .class ));
122
+
123
+ assertThatThrownBy (() -> new S3WritableByteChannel (file , s3Client , transferManager , emptySet ()))
124
+ .isEqualTo (exception );
70
125
}
71
126
72
127
@ Test
0 commit comments