|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.s3control.functionaltests; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.any; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 24 | + |
| 25 | +import java.net.URI; |
| 26 | +import java.util.concurrent.CompletionException; |
| 27 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 31 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 32 | +import software.amazon.awssdk.core.interceptor.Context; |
| 33 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 34 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
| 35 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 36 | +import software.amazon.awssdk.regions.Region; |
| 37 | +import software.amazon.awssdk.services.s3control.S3ControlAsyncClient; |
| 38 | +import software.amazon.awssdk.services.s3control.S3ControlAsyncClientBuilder; |
| 39 | +import software.amazon.awssdk.services.s3control.S3ControlClient; |
| 40 | +import software.amazon.awssdk.services.s3control.S3ControlClientBuilder; |
| 41 | +import software.amazon.awssdk.services.s3control.model.S3ControlException; |
| 42 | + |
| 43 | +public class CreateJobFunctionalTest { |
| 44 | + |
| 45 | + private static final URI HTTP_LOCALHOST_URI = URI.create("http://localhost:8080/"); |
| 46 | + |
| 47 | + @Rule |
| 48 | + public WireMockRule wireMock = new WireMockRule(); |
| 49 | + |
| 50 | + private S3ControlClientBuilder getSyncClientBuilder() { |
| 51 | + return S3ControlClient.builder() |
| 52 | + .region(Region.US_EAST_1) |
| 53 | + .overrideConfiguration(c -> c.addExecutionInterceptor(new LocalhostEndpointAddressInterceptor())) |
| 54 | + .credentialsProvider( |
| 55 | + StaticCredentialsProvider.create( |
| 56 | + AwsBasicCredentials.create("key", "secret"))); |
| 57 | + } |
| 58 | + |
| 59 | + private S3ControlAsyncClientBuilder getAsyncClientBuilder() { |
| 60 | + return S3ControlAsyncClient.builder() |
| 61 | + .region(Region.US_EAST_1) |
| 62 | + .overrideConfiguration(c -> c.addExecutionInterceptor(new LocalhostEndpointAddressInterceptor())) |
| 63 | + .credentialsProvider( |
| 64 | + StaticCredentialsProvider.create( |
| 65 | + AwsBasicCredentials.create("key", "secret"))); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void createJob_syncClient_errorInResponseBody_correct() { |
| 70 | + String accountId = "Account-Id"; |
| 71 | + String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 72 | + + "<Error>\n" |
| 73 | + + "<Code>InvalidRequest</Code>\n" |
| 74 | + + "<Message>Missing role arn</Message>\n" |
| 75 | + + "<RequestId>656c76696e6727732072657175657374</RequestId>\n" |
| 76 | + + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n" |
| 77 | + + "</Error>"; |
| 78 | + |
| 79 | + stubFor(any(anyUrl()).willReturn(aResponse().withStatus(400).withBody(xmlResponseBody))); |
| 80 | + |
| 81 | + S3ControlClient s3Client = getSyncClientBuilder().build(); |
| 82 | + |
| 83 | + assertThatThrownBy(() -> s3Client.createJob(r -> r.accountId(accountId))) |
| 84 | + .isInstanceOf(S3ControlException.class) |
| 85 | + .satisfies(e -> assertThat(((S3ControlException) e).awsErrorDetails().errorCode()).isEqualTo("InvalidRequest")) |
| 86 | + .satisfies(e -> assertThat(((S3ControlException) e).awsErrorDetails().errorMessage()).isEqualTo("Missing role arn")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void createJob_asyncClient_errorInResponseBody_correct() { |
| 91 | + String accountId = "Account-Id"; |
| 92 | + String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 93 | + + "<Error>\n" |
| 94 | + + "<Code>InvalidRequest</Code>\n" |
| 95 | + + "<Message>Missing role arn</Message>\n" |
| 96 | + + "<RequestId>656c76696e6727732072657175657374</RequestId>\n" |
| 97 | + + "<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>\n" |
| 98 | + + "</Error>"; |
| 99 | + |
| 100 | + stubFor(any(anyUrl()).willReturn(aResponse().withStatus(400).withBody(xmlResponseBody))); |
| 101 | + |
| 102 | + S3ControlAsyncClient s3Client = getAsyncClientBuilder().build(); |
| 103 | + |
| 104 | + assertThatThrownBy(() -> s3Client.createJob(r -> r.accountId(accountId)).join()) |
| 105 | + .isInstanceOf(CompletionException.class) |
| 106 | + .hasCauseInstanceOf(S3ControlException.class) |
| 107 | + .satisfies(e -> { |
| 108 | + S3ControlException s3ControlException = (S3ControlException) e.getCause(); |
| 109 | + assertThat(s3ControlException.awsErrorDetails().errorCode()).isEqualTo("InvalidRequest"); |
| 110 | + assertThat(s3ControlException.awsErrorDetails().errorMessage()).isEqualTo("Missing role arn"); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private static final class LocalhostEndpointAddressInterceptor implements ExecutionInterceptor { |
| 115 | + |
| 116 | + @Override |
| 117 | + public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { |
| 118 | + return context.httpRequest() |
| 119 | + .toBuilder() |
| 120 | + .protocol(HTTP_LOCALHOST_URI.getScheme()) |
| 121 | + .host(HTTP_LOCALHOST_URI.getHost()) |
| 122 | + .port(HTTP_LOCALHOST_URI.getPort()) |
| 123 | + .build(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments