Search in sources :

Example 31 with DataProvider

use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method executeAsyncHttpRequest_completes_future_if_exception_happens_during_setup.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void executeAsyncHttpRequest_completes_future_if_exception_happens_during_setup(boolean throwCircuitBreakerOpenException) {
    // given
    RuntimeException exToThrow = (throwCircuitBreakerOpenException) ? new CircuitBreakerOpenException("foo", "kaboom") : new RuntimeException("kaboom");
    doThrow(exToThrow).when(helperSpy).getCircuitBreaker(any(RequestBuilderWrapper.class));
    Logger loggerMock = mock(Logger.class);
    Whitebox.setInternalState(helperSpy, "logger", loggerMock);
    // when
    CompletableFuture result = helperSpy.executeAsyncHttpRequest(mock(RequestBuilderWrapper.class), mock(AsyncResponseHandler.class), null, null);
    // then
    assertThat(result).isCompletedExceptionally();
    Throwable ex = catchThrowable(result::get);
    assertThat(ex).isInstanceOf(ExecutionException.class).hasCause(exToThrow);
    if (throwCircuitBreakerOpenException)
        verifyZeroInteractions(loggerMock);
    else
        verify(loggerMock).error(anyString(), anyString(), anyString(), eq(exToThrow));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Logger(org.slf4j.Logger) ExecutionException(java.util.concurrent.ExecutionException) CircuitBreakerOpenException(com.nike.fastbreak.exception.CircuitBreakerOpenException) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 32 with DataProvider

use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected.

@DataProvider(value = { "CONNECT", "DELETE", "GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "TRACE", "FOO_METHOD_DOES_NOT_EXIST" }, splitBy = "\\|")
@Test
public void getRequestBuilder_with_circuit_breaker_args_sets_values_as_expected(String methodName) {
    CircuitBreaker<Response> cbMock = mock(CircuitBreaker.class);
    List<Pair<Optional<CircuitBreaker<Response>>, Boolean>> variations = Arrays.asList(Pair.of(Optional.empty(), true), Pair.of(Optional.empty(), false), Pair.of(Optional.of(cbMock), true), Pair.of(Optional.of(cbMock), false));
    variations.forEach(variation -> {
        String url = "http://localhost/some/path";
        HttpMethod method = HttpMethod.valueOf(methodName);
        Optional<CircuitBreaker<Response>> cbOpt = variation.getLeft();
        boolean disableCb = variation.getRight();
        RequestBuilderWrapper rbw = helperSpy.getRequestBuilder(url, method, cbOpt, disableCb);
        verifyRequestBuilderWrapperGeneratedAsExpected(rbw, url, methodName, cbOpt, disableCb);
    });
}
Also used : Response(com.ning.http.client.Response) CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Pair(com.nike.internal.util.Pair) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 33 with DataProvider

use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method constructor_clears_out_tracing_and_mdc_info_before_building_underlying_client_and_resets_afterward.

@DataProvider(value = { "true   |   true", "true   |   false", "false  |   true", "false  |   false" }, splitBy = "\\|")
@Test
public void constructor_clears_out_tracing_and_mdc_info_before_building_underlying_client_and_resets_afterward(boolean emptyBeforeCall, boolean explode) {
    AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    List<Span> traceAtTimeOfBuildCall = new ArrayList<>();
    List<Map<String, String>> mdcAtTimeOfBuildCall = new ArrayList<>();
    RuntimeException explodeEx = new RuntimeException("kaboom");
    doAnswer(invocation -> {
        traceAtTimeOfBuildCall.add(Tracer.getInstance().getCurrentSpan());
        mdcAtTimeOfBuildCall.add(MDC.getCopyOfContextMap());
        if (explode)
            throw explodeEx;
        return config;
    }).when(builderMock).build();
    Span spanBeforeCall = (emptyBeforeCall) ? null : Tracer.getInstance().startRequestWithRootSpan("foo");
    Map<String, String> mdcBeforeCall = MDC.getCopyOfContextMap();
    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    if (emptyBeforeCall)
        assertThat(mdcBeforeCall).isNull();
    else
        assertThat(mdcBeforeCall).isNotEmpty();
    // when
    Throwable ex = catchThrowable(() -> new AsyncHttpClientHelper(builderMock, true));
    // then
    verify(builderMock).build();
    assertThat(traceAtTimeOfBuildCall).hasSize(1);
    assertThat(traceAtTimeOfBuildCall.get(0)).isNull();
    assertThat(mdcAtTimeOfBuildCall).hasSize(1);
    assertThat(mdcAtTimeOfBuildCall.get(0)).isNull();
    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    assertThat(MDC.getCopyOfContextMap()).isEqualTo(mdcBeforeCall);
    if (explode)
        assertThat(ex).isSameAs(explodeEx);
}
Also used : ArrayList(java.util.ArrayList) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Matchers.anyString(org.mockito.Matchers.anyString) Span(com.nike.wingtips.Span) Map(java.util.Map) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 34 with DataProvider

use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.

the class AsyncHttpClientHelperTest method kitchen_sink_constructor_sets_up_underlying_client_with_expected_config.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void kitchen_sink_constructor_sets_up_underlying_client_with_expected_config(boolean performSubspan) {
    // given
    int customRequestTimeoutVal = 4242;
    AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setRequestTimeout(customRequestTimeoutVal).build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    doReturn(config).when(builderMock).build();
    // when
    AsyncHttpClientHelper instance = new AsyncHttpClientHelper(builderMock, performSubspan);
    // then
    assertThat(instance.performSubSpanAroundDownstreamCalls).isEqualTo(performSubspan);
    assertThat(instance.asyncHttpClient.getConfig()).isSameAs(config);
    assertThat(instance.asyncHttpClient.getConfig().getRequestTimeout()).isEqualTo(customRequestTimeoutVal);
}
Also used : AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 35 with DataProvider

use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.

the class AsyncCompletionHandlerWithTracingAndMdcSupportTest method onCompleted_completes_completableFutureResponse_with_result_of_responseHandlerFunction.

@DataProvider(value = { "true", "false" })
@Test
public void onCompleted_completes_completableFutureResponse_with_result_of_responseHandlerFunction(boolean throwException) throws Throwable {
    // given
    Exception ex = new Exception("kaboom");
    if (throwException)
        doThrow(ex).when(responseHandlerFunctionMock).handleResponse(responseMock);
    // when
    Response ignoredResult = handlerSpy.onCompleted(responseMock);
    // then
    verify(responseHandlerFunctionMock).handleResponse(responseMock);
    if (throwException) {
        assertThat(completableFutureResponse).isCompletedExceptionally();
        assertThat(completableFutureResponse).hasFailedWithThrowableThat().isEqualTo(ex);
    } else {
        assertThat(completableFutureResponse).isCompleted();
        assertThat(completableFutureResponse.get()).isEqualTo(responseHandlerResult);
    }
    assertThat(ignoredResult).isEqualTo(responseMock);
    verifyZeroInteractions(responseMock);
}
Also used : Response(com.ning.http.client.Response) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)88 Test (org.junit.Test)85 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)20 Span (com.nike.wingtips.Span)19 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)19 Matchers.anyString (org.mockito.Matchers.anyString)11 HttpMethod (io.netty.handler.codec.http.HttpMethod)9 Endpoint (com.nike.riposte.server.http.Endpoint)8 Timer (com.codahale.metrics.Timer)7 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 RequestInfo (com.nike.riposte.server.http.RequestInfo)5 Charset (java.nio.charset.Charset)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Meter (com.codahale.metrics.Meter)4 Response (com.ning.http.client.Response)4 ExtractableResponse (io.restassured.response.ExtractableResponse)4 Deque (java.util.Deque)4 Optional (java.util.Optional)4