|
20 | 20 | import static org.junit.jupiter.api.Assertions.fail;
|
21 | 21 | import static org.mockito.ArgumentMatchers.any;
|
22 | 22 | import static org.mockito.ArgumentMatchers.eq;
|
| 23 | +import static org.mockito.Mockito.doReturn; |
| 24 | +import static org.mockito.Mockito.doThrow; |
23 | 25 | import static org.mockito.Mockito.mock;
|
24 | 26 | import static org.mockito.Mockito.times;
|
25 | 27 | import static org.mockito.Mockito.verify;
|
|
42 | 44 | import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
|
43 | 45 | import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
|
44 | 46 | import software.amazon.awssdk.awscore.exception.AwsServiceException;
|
| 47 | +import software.amazon.awssdk.core.ResponseBytes; |
| 48 | +import software.amazon.awssdk.core.ResponseInputStream; |
45 | 49 | import software.amazon.awssdk.core.exception.NonRetryableException;
|
46 | 50 | import software.amazon.awssdk.http.SdkHttpResponse;
|
47 | 51 | import software.amazon.awssdk.services.cloudformation.CloudFormationAsyncClient;
|
48 | 52 | import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
49 | 53 | import software.amazon.awssdk.services.cloudformation.model.DescribeStackEventsResponse;
|
| 54 | +import software.amazon.awssdk.services.s3.S3Client; |
| 55 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
50 | 56 | import software.amazon.cloudformation.exceptions.ResourceAlreadyExistsException;
|
51 | 57 | import software.amazon.cloudformation.exceptions.TerminalException;
|
52 | 58 | import software.amazon.cloudformation.proxy.delay.Constant;
|
@@ -229,6 +235,150 @@ public void testInjectCredentialsAndInvokeV2Async_WithException() {
|
229 | 235 |
|
230 | 236 | }
|
231 | 237 |
|
| 238 | + @Test |
| 239 | + public void testInjectCredentialsAndInvokeV2InputStream() { |
| 240 | + |
| 241 | + final LoggerProxy loggerProxy = mock(LoggerProxy.class); |
| 242 | + final Credentials credentials = new Credentials("accessKeyId", "secretAccessKey", "sessionToken"); |
| 243 | + final ResponseInputStream<?> responseInputStream = mock(ResponseInputStream.class); |
| 244 | + |
| 245 | + final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(loggerProxy, credentials, () -> 1000L); |
| 246 | + |
| 247 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest wrappedRequest = mock( |
| 248 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 249 | + |
| 250 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder builder = mock( |
| 251 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder.class); |
| 252 | + when(builder.overrideConfiguration(any(AwsRequestOverrideConfiguration.class))).thenReturn(builder); |
| 253 | + when(builder.build()).thenReturn(wrappedRequest); |
| 254 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest request = mock( |
| 255 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 256 | + when(request.toBuilder()).thenReturn(builder); |
| 257 | + |
| 258 | + final S3Client client = mock(S3Client.class); |
| 259 | + |
| 260 | + doReturn(responseInputStream).when(client) |
| 261 | + .getObject(any(software.amazon.awssdk.services.s3.model.GetObjectRequest.class)); |
| 262 | + |
| 263 | + final ResponseInputStream< |
| 264 | + GetObjectResponse> result = proxy.injectCredentialsAndInvokeV2InputStream(request, client::getObject); |
| 265 | + |
| 266 | + // verify request is rebuilt for injection |
| 267 | + verify(request).toBuilder(); |
| 268 | + |
| 269 | + // verify the wrapped request is sent over the initiate |
| 270 | + verify(client).getObject(wrappedRequest); |
| 271 | + |
| 272 | + // ensure the return type matches |
| 273 | + assertThat(result).isEqualTo(responseInputStream); |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void testInjectCredentialsAndInvokeV2InputStream_Exception() { |
| 278 | + |
| 279 | + final LoggerProxy loggerProxy = mock(LoggerProxy.class); |
| 280 | + final Credentials credentials = new Credentials("accessKeyId", "secretAccessKey", "sessionToken"); |
| 281 | + |
| 282 | + final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(loggerProxy, credentials, () -> 1000L); |
| 283 | + |
| 284 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest wrappedRequest = mock( |
| 285 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 286 | + |
| 287 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder builder = mock( |
| 288 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder.class); |
| 289 | + when(builder.overrideConfiguration(any(AwsRequestOverrideConfiguration.class))).thenReturn(builder); |
| 290 | + when(builder.build()).thenReturn(wrappedRequest); |
| 291 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest request = mock( |
| 292 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 293 | + when(request.toBuilder()).thenReturn(builder); |
| 294 | + |
| 295 | + final S3Client client = mock(S3Client.class); |
| 296 | + |
| 297 | + doThrow(new TerminalException(new RuntimeException("Sorry"))).when(client) |
| 298 | + .getObject(any(software.amazon.awssdk.services.s3.model.GetObjectRequest.class)); |
| 299 | + |
| 300 | + assertThrows(RuntimeException.class, () -> proxy.injectCredentialsAndInvokeV2InputStream(request, client::getObject), |
| 301 | + "Expected Runtime Exception."); |
| 302 | + |
| 303 | + // verify request is rebuilt for injection |
| 304 | + verify(request).toBuilder(); |
| 305 | + |
| 306 | + // verify the wrapped request is sent over the initiate |
| 307 | + verify(client).getObject(wrappedRequest); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + public void testInjectCredentialsAndInvokeV2Bytes() { |
| 312 | + |
| 313 | + final LoggerProxy loggerProxy = mock(LoggerProxy.class); |
| 314 | + final Credentials credentials = new Credentials("accessKeyId", "secretAccessKey", "sessionToken"); |
| 315 | + final ResponseBytes<?> responseBytes = mock(ResponseBytes.class); |
| 316 | + |
| 317 | + final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(loggerProxy, credentials, () -> 1000L); |
| 318 | + |
| 319 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest wrappedRequest = mock( |
| 320 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 321 | + |
| 322 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder builder = mock( |
| 323 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder.class); |
| 324 | + when(builder.overrideConfiguration(any(AwsRequestOverrideConfiguration.class))).thenReturn(builder); |
| 325 | + when(builder.build()).thenReturn(wrappedRequest); |
| 326 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest request = mock( |
| 327 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 328 | + when(request.toBuilder()).thenReturn(builder); |
| 329 | + |
| 330 | + final S3Client client = mock(S3Client.class); |
| 331 | + |
| 332 | + doReturn(responseBytes).when(client) |
| 333 | + .getObjectAsBytes(any(software.amazon.awssdk.services.s3.model.GetObjectRequest.class)); |
| 334 | + |
| 335 | + final ResponseBytes< |
| 336 | + GetObjectResponse> result = proxy.injectCredentialsAndInvokeV2Bytes(request, client::getObjectAsBytes); |
| 337 | + |
| 338 | + // verify request is rebuilt for injection |
| 339 | + verify(request).toBuilder(); |
| 340 | + |
| 341 | + // verify the wrapped request is sent over the initiate |
| 342 | + verify(client).getObjectAsBytes(wrappedRequest); |
| 343 | + |
| 344 | + // ensure the return type matches |
| 345 | + assertThat(result).isEqualTo(responseBytes); |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + public void testInjectCredentialsAndInvokeV2Bytes_Exception() { |
| 350 | + |
| 351 | + final LoggerProxy loggerProxy = mock(LoggerProxy.class); |
| 352 | + final Credentials credentials = new Credentials("accessKeyId", "secretAccessKey", "sessionToken"); |
| 353 | + |
| 354 | + final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(loggerProxy, credentials, () -> 1000L); |
| 355 | + |
| 356 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest wrappedRequest = mock( |
| 357 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 358 | + |
| 359 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder builder = mock( |
| 360 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder.class); |
| 361 | + when(builder.overrideConfiguration(any(AwsRequestOverrideConfiguration.class))).thenReturn(builder); |
| 362 | + when(builder.build()).thenReturn(wrappedRequest); |
| 363 | + final software.amazon.awssdk.services.s3.model.GetObjectRequest request = mock( |
| 364 | + software.amazon.awssdk.services.s3.model.GetObjectRequest.class); |
| 365 | + when(request.toBuilder()).thenReturn(builder); |
| 366 | + |
| 367 | + final S3Client client = mock(S3Client.class); |
| 368 | + |
| 369 | + doThrow(new TerminalException(new RuntimeException("Sorry"))).when(client) |
| 370 | + .getObjectAsBytes(any(software.amazon.awssdk.services.s3.model.GetObjectRequest.class)); |
| 371 | + |
| 372 | + assertThrows(RuntimeException.class, () -> proxy.injectCredentialsAndInvokeV2Bytes(request, client::getObjectAsBytes), |
| 373 | + "Expected Runtime Exception."); |
| 374 | + |
| 375 | + // verify request is rebuilt for injection |
| 376 | + verify(request).toBuilder(); |
| 377 | + |
| 378 | + // verify the wrapped request is sent over the initiate |
| 379 | + verify(client).getObjectAsBytes(wrappedRequest); |
| 380 | + } |
| 381 | + |
232 | 382 | private final Credentials MOCK = new Credentials("accessKeyId", "secretKey", "token");
|
233 | 383 |
|
234 | 384 | @Test
|
|
0 commit comments