Search in sources :

Example 11 with DataProvider

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

the class RequestFilterHandlerTest method handleFilterLogic_executes_all_filters_and_uses_original_request_when_filters_are_not_short_circuiting_and_return_null.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_executes_all_filters_and_uses_original_request_when_filters_are_not_short_circuiting_and_return_null(boolean isFirstChunk) {
    // given
    HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
    filtersList.forEach(filter -> doReturn(false).when(filter).isShortCircuitRequestFilter());
    // when
    PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.normalFilterCall, args.shortCircuitFilterCall);
    // then
    assertThat(result).isEqualTo(CONTINUE);
    filtersList.forEach(filter -> {
        if (isFirstChunk)
            verify(filter).filterRequestFirstChunkNoPayload(requestInfoMock, ctxMock);
        else
            verify(filter).filterRequestLastChunkWithFullPayload(requestInfoMock, ctxMock);
    });
    assertThat(state.getRequestInfo()).isSameAs(requestInfoMock);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 12 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 13 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 14 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 15 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)

Aggregations

DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)87 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 Map (java.util.Map)7 HashMap (java.util.HashMap)6 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