18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
import static org .mockito .Mockito .verify ;
21
+ import static org .mockito .Mockito .when ;
21
22
22
23
import java .nio .ByteBuffer ;
23
24
import java .nio .charset .StandardCharsets ;
27
28
import org .junit .runner .RunWith ;
28
29
import org .mockito .ArgumentCaptor ;
29
30
import org .mockito .Mock ;
31
+ import org .mockito .Mockito ;
30
32
import org .mockito .junit .MockitoJUnitRunner ;
31
33
import software .amazon .awssdk .core .exception .SdkClientException ;
32
34
import software .amazon .awssdk .crt .http .HttpHeader ;
35
+ import software .amazon .awssdk .crt .s3 .S3FinishedResponseContext ;
33
36
import software .amazon .awssdk .http .SdkHttpResponse ;
34
37
import software .amazon .awssdk .http .async .SdkAsyncHttpResponseHandler ;
35
38
@@ -42,6 +45,9 @@ public class S3CrtResponseHandlerAdapterTest {
42
45
43
46
@ Mock
44
47
private S3CrtDataPublisher crtDataPublisher ;
48
+
49
+ @ Mock
50
+ private S3FinishedResponseContext context ;
45
51
private CompletableFuture <Void > future ;
46
52
47
53
@ Before
@@ -71,7 +77,7 @@ public void successfulResponse_shouldCompleteFutureSuccessfully() {
71
77
72
78
verify (sdkResponseHandler ).onStream (crtDataPublisher );
73
79
74
- responseHandlerAdapter .onFinished (0 , 0 , null );
80
+ responseHandlerAdapter .onFinished (stubResponseContext ( 0 , 0 , null ) );
75
81
assertThat (future ).isCompleted ();
76
82
}
77
83
@@ -90,7 +96,8 @@ public void errorResponse_shouldCompleteFutureSuccessfully() {
90
96
verify (sdkResponseHandler ).onStream (crtDataPublisher );
91
97
92
98
byte [] errorPayload = "errorResponse" .getBytes (StandardCharsets .UTF_8 );
93
- responseHandlerAdapter .onFinished (1 , statusCode , errorPayload );
99
+
100
+ responseHandlerAdapter .onFinished (stubResponseContext (1 , statusCode , errorPayload ));
94
101
95
102
ArgumentCaptor <ByteBuffer > byteBufferArgumentCaptor = ArgumentCaptor .forClass (ByteBuffer .class );
96
103
verify (crtDataPublisher ).deliverData (byteBufferArgumentCaptor .capture ());
@@ -105,7 +112,7 @@ public void errorResponse_shouldCompleteFutureSuccessfully() {
105
112
@ Test
106
113
public void requestFailed_shouldCompleteFutureExceptionally () {
107
114
108
- responseHandlerAdapter .onFinished (1 , 0 , null );
115
+ responseHandlerAdapter .onFinished (stubResponseContext ( 1 , 0 , null ) );
109
116
110
117
ArgumentCaptor <Exception > exceptionArgumentCaptor = ArgumentCaptor .forClass (Exception .class );
111
118
verify (crtDataPublisher ).notifyError (exceptionArgumentCaptor .capture ());
@@ -115,4 +122,12 @@ public void requestFailed_shouldCompleteFutureExceptionally() {
115
122
assertThat (actualException ).isInstanceOf (SdkClientException .class );
116
123
assertThat (future ).isCompletedExceptionally ();
117
124
}
125
+
126
+ private S3FinishedResponseContext stubResponseContext (int errorCode , int responseStatus , byte [] errorPayload ) {
127
+ Mockito .reset (context );
128
+ when (context .getErrorCode ()).thenReturn (errorCode );
129
+ when (context .getResponseStatus ()).thenReturn (responseStatus );
130
+ when (context .getErrorPayload ()).thenReturn (errorPayload );
131
+ return context ;
132
+ }
118
133
}
0 commit comments