Search in sources :

Example 1 with SdkAsyncClientHandler

use of software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler in project aws-sdk-java-v2 by aws.

the class AsyncClientHandlerExceptionTest method methodSetup.

@BeforeEach
public void methodSetup() throws Exception {
    executionParams = new ClientExecutionParams<SdkRequest, SdkResponse>().withInput(request).withMarshaller(marshaller).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkClientOption.ASYNC_HTTP_CLIENT, asyncHttpClient).option(SdkClientOption.RETRY_POLICY, RetryPolicy.none()).option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, Runnable::run).build();
    clientHandler = new SdkAsyncClientHandler(config);
    when(request.overrideConfiguration()).thenReturn(Optional.empty());
    when(marshaller.marshall(eq(request))).thenReturn(ValidSdkObjects.sdkHttpFullRequest().build());
    when(responseHandler.handle(any(SdkHttpFullResponse.class), any(ExecutionAttributes.class))).thenReturn(VoidSdkResponse.builder().build());
    when(asyncHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer((Answer<CompletableFuture<Void>>) invocationOnMock -> {
        SdkAsyncHttpResponseHandler handler = invocationOnMock.getArgument(0, AsyncExecuteRequest.class).responseHandler();
        handler.onHeaders(SdkHttpFullResponse.builder().statusCode(200).build());
        handler.onStream(new EmptyPublisher<>());
        return CompletableFuture.completedFuture(null);
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) CompletableFuture(java.util.concurrent.CompletableFuture) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Supplier(java.util.function.Supplier) Marshaller(software.amazon.awssdk.core.runtime.transform.Marshaller) Answer(org.mockito.stubbing.Answer) SdkAsyncClientHandler(software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) SdkAsyncHttpClient(software.amazon.awssdk.http.async.SdkAsyncHttpClient) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) EmptyPublisher(software.amazon.awssdk.core.async.EmptyPublisher) ValidSdkObjects(utils.ValidSdkObjects) Predicate(java.util.function.Predicate) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) Mockito.verify(org.mockito.Mockito.verify) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) Test(org.junit.jupiter.api.Test) ClientExecutionParams(software.amazon.awssdk.core.client.handler.ClientExecutionParams) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SdkResponse(software.amazon.awssdk.core.SdkResponse) SdkAdvancedAsyncClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption) HttpTestUtils(utils.HttpTestUtils) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) HttpResponseHandler(software.amazon.awssdk.core.http.HttpResponseHandler) SdkRequest(software.amazon.awssdk.core.SdkRequest) Mockito.mock(org.mockito.Mockito.mock) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) CompletableFuture(java.util.concurrent.CompletableFuture) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) SdkRequest(software.amazon.awssdk.core.SdkRequest) EmptyPublisher(software.amazon.awssdk.core.async.EmptyPublisher) SdkAsyncClientHandler(software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with SdkAsyncClientHandler

use of software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler in project aws-sdk-java-v2 by aws.

the class AsyncClientHandlerInterceptorExceptionTest method testSetup.

@Before
public void testSetup() throws Exception {
    executionParams = new ClientExecutionParams<SdkRequest, SdkResponse>().withInput(request).withMarshaller(marshaller).withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler);
    SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, Collections.singletonList(hook.interceptor())).option(SdkClientOption.ASYNC_HTTP_CLIENT, asyncHttpClient).option(SdkClientOption.RETRY_POLICY, RetryPolicy.none()).option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, Runnable::run).build();
    clientHandler = new SdkAsyncClientHandler(config);
    when(request.overrideConfiguration()).thenReturn(Optional.empty());
    when(marshaller.marshall(eq(request))).thenReturn(ValidSdkObjects.sdkHttpFullRequest().build());
    when(responseHandler.handle(any(SdkHttpFullResponse.class), any(ExecutionAttributes.class))).thenReturn(VoidSdkResponse.builder().build());
    Answer<CompletableFuture<Void>> prepareRequestAnswer;
    if (hook != Hook.ON_EXECUTION_FAILURE) {
        prepareRequestAnswer = invocationOnMock -> {
            SdkAsyncHttpResponseHandler handler = invocationOnMock.getArgument(0, AsyncExecuteRequest.class).responseHandler();
            handler.onHeaders(SdkHttpFullResponse.builder().statusCode(200).build());
            handler.onStream(new EmptyPublisher<>());
            return CompletableFuture.completedFuture(null);
        };
    } else {
        prepareRequestAnswer = invocationOnMock -> {
            SdkAsyncHttpResponseHandler handler = invocationOnMock.getArgument(0, AsyncExecuteRequest.class).responseHandler();
            RuntimeException error = new RuntimeException("Something went horribly wrong!");
            handler.onError(error);
            return CompletableFutureUtils.failedFuture(error);
        };
    }
    when(asyncHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer(prepareRequestAnswer);
}
Also used : SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkRequest(software.amazon.awssdk.core.SdkRequest) SdkAsyncClientHandler(software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler) SdkHttpFullResponse(software.amazon.awssdk.http.SdkHttpFullResponse) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) CompletableFuture(java.util.concurrent.CompletableFuture) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) VoidSdkResponse(software.amazon.awssdk.core.protocol.VoidSdkResponse) SdkResponse(software.amazon.awssdk.core.SdkResponse) Before(org.junit.Before)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2 SdkRequest (software.amazon.awssdk.core.SdkRequest)2 SdkResponse (software.amazon.awssdk.core.SdkResponse)2 SdkClientConfiguration (software.amazon.awssdk.core.client.config.SdkClientConfiguration)2 SdkAsyncClientHandler (software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler)2 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)2 VoidSdkResponse (software.amazon.awssdk.core.protocol.VoidSdkResponse)2 SdkHttpFullResponse (software.amazon.awssdk.http.SdkHttpFullResponse)2 AsyncExecuteRequest (software.amazon.awssdk.http.async.AsyncExecuteRequest)2 SdkAsyncHttpResponseHandler (software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler)2 Optional (java.util.Optional)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Before (org.junit.Before)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1 Mockito.mock (org.mockito.Mockito.mock)1