Search in sources :

Example 26 with ExecutionInterceptor

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

the class AsyncHttpClientApiCallTimeoutTests method successfulResponse_SlowBeforeRequestRequestHandler_ThrowsApiCallTimeoutException.

@Test
public void successfulResponse_SlowBeforeRequestRequestHandler_ThrowsApiCallTimeoutException() {
    stubFor(get(anyUrl()).willReturn(aResponse().withStatus(200).withBody("{}")));
    ExecutionInterceptor interceptor = new SlowExecutionInterceptor().beforeTransmissionWaitInSeconds(SLOW_REQUEST_HANDLER_TIMEOUT);
    CompletableFuture future = requestBuilder().executionContext(withInterceptors(interceptor)).execute(noOpResponseHandler());
    assertThatThrownBy(future::join).hasCauseInstanceOf(ApiCallTimeoutException.class);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) Test(org.junit.Test)

Example 27 with ExecutionInterceptor

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

the class HttpClientApiCallAttemptTimeoutTest method successfulResponse_SlowBeforeTransmissionExecutionInterceptor_ThrowsApiCallTimeoutException.

@Test
public void successfulResponse_SlowBeforeTransmissionExecutionInterceptor_ThrowsApiCallTimeoutException() {
    stubFor(get(anyUrl()).willReturn(aResponse().withStatus(200).withBody("{}")));
    ExecutionInterceptor interceptor = new SlowExecutionInterceptor().beforeTransmissionWaitInSeconds(SLOW_REQUEST_HANDLER_TIMEOUT);
    assertThatThrownBy(() -> requestBuilder().executionContext(withInterceptors(interceptor)).execute(noOpSyncResponseHandler())).isInstanceOf(ApiCallAttemptTimeoutException.class);
}
Also used : SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SlowExecutionInterceptor(software.amazon.awssdk.core.internal.http.request.SlowExecutionInterceptor) Test(org.junit.Test)

Example 28 with ExecutionInterceptor

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

the class ProfileUseArnRegionProviderTest method specifiedInOverrideConfig_shouldUse.

@Test
public void specifiedInOverrideConfig_shouldUse() {
    ExecutionInterceptor interceptor = Mockito.spy(AbstractExecutionInterceptor.class);
    String profileFileContent = "[default]\n" + "s3_use_arn_region = 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))).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) FALSE(java.lang.Boolean.FALSE) 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 29 with ExecutionInterceptor

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

the class ExecutionAttributesTest method syncClient_requestOverrideExecutionAttributesHavePrecedence.

@Test
public void syncClient_requestOverrideExecutionAttributesHavePrecedence() {
    ExecutionInterceptor interceptor = mock(ExecutionInterceptor.class);
    ArgumentCaptor<ExecutionAttributes> attributesCaptor = ArgumentCaptor.forClass(ExecutionAttributes.class);
    doThrow(new RuntimeException("BOOM")).when(interceptor).beforeExecution(any(Context.BeforeExecution.class), attributesCaptor.capture());
    ExecutionAttribute testAttribute = attr("TestAttribute");
    String testValue = "TestValue";
    ProtocolRestJsonClient sync = ProtocolRestJsonClient.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))).region(Region.US_WEST_2).overrideConfiguration(c -> c.addExecutionInterceptor(interceptor).putExecutionAttribute(testAttribute, testValue)).build();
    String overwrittenValue = "TestValue2";
    AllTypesRequest request = AllTypesRequest.builder().overrideConfiguration(c -> c.putExecutionAttribute(testAttribute, overwrittenValue)).build();
    thrown.expect(RuntimeException.class);
    try {
        sync.allTypes(request);
    } finally {
        ExecutionAttributes attributes = attributesCaptor.getValue();
        assertThat(attributes.getAttribute(testAttribute)).isEqualTo(overwrittenValue);
        sync.close();
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SdkAdvancedClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedClientOption) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) Context(software.amazon.awssdk.core.interceptor.Context) Mockito.doThrow(org.mockito.Mockito.doThrow) Rule(org.junit.Rule) ArgumentCaptor(org.mockito.ArgumentCaptor) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Region(software.amazon.awssdk.regions.Region) ExpectedException(org.junit.rules.ExpectedException) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Mockito.mock(org.mockito.Mockito.mock) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) Test(org.junit.Test)

Example 30 with ExecutionInterceptor

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

the class ExecutionAttributesTest method asyncClient_requestOverrideExecutionAttributesHavePrecedence.

@Test
public void asyncClient_requestOverrideExecutionAttributesHavePrecedence() {
    ExecutionInterceptor interceptor = mock(ExecutionInterceptor.class);
    ArgumentCaptor<ExecutionAttributes> attributesCaptor = ArgumentCaptor.forClass(ExecutionAttributes.class);
    doThrow(new RuntimeException("BOOM")).when(interceptor).beforeExecution(any(Context.BeforeExecution.class), attributesCaptor.capture());
    ExecutionAttribute testAttribute = attr("TestAttribute");
    String testValue = "TestValue";
    ProtocolRestJsonAsyncClient async = ProtocolRestJsonAsyncClient.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))).region(Region.US_WEST_2).overrideConfiguration(c -> c.addExecutionInterceptor(interceptor).putExecutionAttribute(testAttribute, testValue)).build();
    String overwrittenValue = "TestValue2";
    AllTypesRequest request = AllTypesRequest.builder().overrideConfiguration(c -> c.putExecutionAttribute(testAttribute, overwrittenValue)).build();
    thrown.expect(CompletionException.class);
    try {
        async.allTypes(request).join();
    } finally {
        ExecutionAttributes attributes = attributesCaptor.getValue();
        assertThat(attributes.getAttribute(testAttribute)).isEqualTo(overwrittenValue);
        async.close();
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) SdkAdvancedClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedClientOption) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) Context(software.amazon.awssdk.core.interceptor.Context) Mockito.doThrow(org.mockito.Mockito.doThrow) Rule(org.junit.Rule) ArgumentCaptor(org.mockito.ArgumentCaptor) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Region(software.amazon.awssdk.regions.Region) ExpectedException(org.junit.rules.ExpectedException) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) Mockito.mock(org.mockito.Mockito.mock) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) ExecutionAttribute(software.amazon.awssdk.core.interceptor.ExecutionAttribute) AllTypesRequest(software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) ProtocolRestJsonAsyncClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient) 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