Search in sources :

Example 11 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ProfileDisableMultiRegionProviderTest method specifiedInOverrideConfig_shouldUse.

@Test
public void specifiedInOverrideConfig_shouldUse() {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "s3_disable_multiregion_access_points = true\n";
    ProfileFile profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).content(new StringInputStream(profileFileContent)).build();
    S3Client s3 = S3Client.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create()).overrideConfiguration(c -> c.defaultProfileFile(profileFile).defaultProfileName("default").addExecutionInterceptor(interceptor).retryPolicy(r -> r.numRetries(0))).serviceConfiguration(s -> s.useArnRegionEnabled(true)).build();
    String arn = "arn:aws:s3:us-banana-46:12345567890:accesspoint:foo";
    assertThatThrownBy(() -> s3.getObject(r -> r.bucket(arn).key("bar"))).isInstanceOf(SdkException.class);
    ArgumentCaptor<Context.BeforeTransmission> context = ArgumentCaptor.forClass(Context.BeforeTransmission.class);
    Mockito.verify(interceptor).beforeTransmission(context.capture(), any());
    String host = context.getValue().httpRequest().host();
    assertThat(host).contains("us-banana-46");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) AWS_CONFIG_FILE(software.amazon.awssdk.profiles.ProfileFileSystemSetting.AWS_CONFIG_FILE) S3Client(software.amazon.awssdk.services.s3.S3Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SdkException(software.amazon.awssdk.core.exception.SdkException) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) Test(org.junit.jupiter.api.Test) Context(software.amazon.awssdk.core.interceptor.Context) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Optional(java.util.Optional) AnonymousCredentialsProvider(software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider) Region(software.amazon.awssdk.regions.Region) TRUE(java.lang.Boolean.TRUE) Context(software.amazon.awssdk.core.interceptor.Context) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) S3Client(software.amazon.awssdk.services.s3.S3Client) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.jupiter.api.Test)

Example 12 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class DefaultJsonBaseClientBuilder method finalizeServiceConfiguration.

@Override
protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientConfiguration config) {
    ClasspathInterceptorChainFactory interceptorFactory = new ClasspathInterceptorChainFactory();
    List<ExecutionInterceptor> interceptors = interceptorFactory.getInterceptors("software/amazon/awssdk/services/json/execution.interceptors");
    interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS));
    return config.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors).build();
}
Also used : ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor)

Example 13 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ExecutionInterceptorTest method async_success_allInterceptorMethodsCalled.

@Test
public void async_success_allInterceptorMethodsCalled() throws Exception {
    // Given
    ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS);
    ProtocolRestJsonAsyncClient client = asyncClient(interceptor);
    MembersInHeadersRequest request = MembersInHeadersRequest.builder().build();
    stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody("")));
    // When
    MembersInHeadersResponse result = client.membersInHeaders(request).get(10, TimeUnit.SECONDS);
    // Expect
    expectAllMethodsCalled(interceptor, request, null, true);
    validateRequestResponse(result);
}
Also used : ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) MembersInHeadersResponse(software.amazon.awssdk.services.protocolrestjson.model.MembersInHeadersResponse) MembersInHeadersRequest(software.amazon.awssdk.services.protocolrestjson.model.MembersInHeadersRequest) Test(org.junit.Test)

Example 14 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ExecutionInterceptorTest method async_streamingInput_success_allInterceptorMethodsCalled.

@Test
public void async_streamingInput_success_allInterceptorMethodsCalled() throws Exception {
    // Given
    ExecutionInterceptor interceptor = mock(NoOpInterceptor.class, CALLS_REAL_METHODS);
    ProtocolRestJsonAsyncClient client = asyncClient(interceptor);
    StreamingInputOperationRequest request = StreamingInputOperationRequest.builder().build();
    stubFor(post(urlPathEqualTo(STREAMING_INPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("")));
    // When
    client.streamingInputOperation(request, new NoOpAsyncRequestBody()).get(10, TimeUnit.SECONDS);
    // Expect
    Context.BeforeTransmission beforeTransmissionArg = captureBeforeTransmissionArg(interceptor, true);
    // TODO: The content should actually be empty to match responses. We can fix this by updating the StructuredJsonGenerator
    // to use null for NO-OP marshalling of payloads. This will break streaming POST operations for JSON because of a hack in
    // the MoveParametersToBodyStage, but we can move the logic from there into the query marshallers (why the hack exists)
    // and then everything should be good for JSON.
    assertThat(beforeTransmissionArg.requestBody().get().contentStreamProvider().newStream().read()).isEqualTo(-1);
    assertThat(beforeTransmissionArg.httpRequest().firstMatchingHeader(Header.CONTENT_LENGTH).get()).contains(Long.toString(0L));
}
Also used : Context(software.amazon.awssdk.core.interceptor.Context) StreamingInputOperationRequest(software.amazon.awssdk.services.protocolrestjson.model.StreamingInputOperationRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Test(org.junit.Test)

Example 15 with ExecutionInterceptor

use of software.amazon.awssdk.core.interceptor.ExecutionInterceptor in project aws-sdk-java-v2 by aws.

the class ExecutionInterceptorTest method sync_interceptorException_failureInterceptorMethodsCalled.

@Test
public void sync_interceptorException_failureInterceptorMethodsCalled() {
    // Given
    ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS);
    RuntimeException exception = new RuntimeException("Uh oh!");
    doThrow(exception).when(interceptor).afterExecution(any(), any());
    ProtocolRestJsonClient client = client(interceptor);
    MembersInHeadersRequest request = MembersInHeadersRequest.builder().build();
    stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody("")));
    // When
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> client.membersInHeaders(request));
    // Expect
    expectAllMethodsCalled(interceptor, request, exception, false);
}
Also used : ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) MembersInHeadersRequest(software.amazon.awssdk.services.protocolrestjson.model.MembersInHeadersRequest) Test(org.junit.Test)

Aggregations

ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)33 Test (org.junit.Test)25 Context (software.amazon.awssdk.core.interceptor.Context)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)12 ProtocolRestJsonAsyncClient (software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient)12 ProtocolRestJsonClient (software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient)12 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)11 ArgumentCaptor (org.mockito.ArgumentCaptor)10 Region (software.amazon.awssdk.regions.Region)10 Mockito.mock (org.mockito.Mockito.mock)9 SdkAdvancedClientOption (software.amazon.awssdk.core.client.config.SdkAdvancedClientOption)9 ExecutionAttribute (software.amazon.awssdk.core.interceptor.ExecutionAttribute)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CompletionException (java.util.concurrent.CompletionException)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)7 Rule (org.junit.Rule)7 ExpectedException (org.junit.rules.ExpectedException)7 Mockito.doThrow (org.mockito.Mockito.doThrow)7 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)7