Search in sources :

Example 56 with DataProvider

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

the class HttpServletRequestWrapperForRequestInfoTest method getProtocol_delegates_to_requestInfo.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getProtocol_delegates_to_requestInfo(boolean useNull) {
    // given
    HttpVersion protocolVersion = (useNull) ? null : HttpVersion.HTTP_1_1;
    doReturn(protocolVersion).when(requestInfoMock).getProtocolVersion();
    // when
    String result = wrapper.getProtocol();
    // then
    if (useNull)
        assertThat(result).isNull();
    else
        assertThat(result).isEqualTo(protocolVersion.text());
}
Also used : HttpVersion(io.netty.handler.codec.http.HttpVersion) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 57 with DataProvider

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

the class HttpServletResponseWrapperForResponseInfoTest method getCharacterEncoding_delegates_to_responseInfo.

@DataProvider(value = { "null       |   ISO-8859-1", "ISO-8859-1 |   ISO-8859-1", "UTF-8      |   UTF-8" }, splitBy = "\\|")
@Test
public void getCharacterEncoding_delegates_to_responseInfo(String contentWriterEncodingName, String expectedResult) {
    // given
    Charset contentWriterEncoding = (contentWriterEncodingName == null) ? null : Charset.forName(contentWriterEncodingName);
    doReturn(contentWriterEncoding).when(responseInfoMock).getDesiredContentWriterEncoding();
    // when
    String result = wrapper.getCharacterEncoding();
    // expect
    assertThat(result).isEqualTo(expectedResult);
}
Also used : Charset(java.nio.charset.Charset) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 58 with DataProvider

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

the class HttpServletRequestWrapperForRequestInfoTest method getMethod_delegates_to_requestInfo.

@DataProvider(value = { "null", "GET", "POST", "OPTIONS" }, splitBy = "\\|")
@Test
public void getMethod_delegates_to_requestInfo(String methodName) {
    // given
    HttpMethod method = (methodName == null) ? null : HttpMethod.valueOf(methodName);
    doReturn(method).when(requestInfoMock).getMethod();
    // when
    String result = wrapper.getMethod();
    // then
    assertThat(result).isEqualTo(methodName);
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 59 with DataProvider

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

the class AsyncHttpClientHelperTest method getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getCircuitBreaker_returns_CircuitBreakerDelegate_wrapping_default_CircuitBreakerForHttpStatusCode_using_host_as_the_key(boolean useNettyEventLoop) {
    // given
    String host = UUID.randomUUID().toString();
    String url = "http://" + host + "/some/path";
    String method = "GET";
    AsyncHttpClient.BoundRequestBuilder reqMock = mock(AsyncHttpClient.BoundRequestBuilder.class);
    Optional<CircuitBreaker<Response>> customCb = Optional.empty();
    RequestBuilderWrapper rbw = new RequestBuilderWrapper(url, method, reqMock, customCb, false);
    if (useNettyEventLoop)
        rbw.setCtx(ctxMock);
    // when
    Optional<CircuitBreaker<Response>> result = helperSpy.getCircuitBreaker(rbw);
    // then
    assertThat(result).isPresent();
    assertThat(result.get()).isInstanceOf(CircuitBreakerDelegate.class);
    CircuitBreakerDelegate<Response, Integer> wrapper = (CircuitBreakerDelegate) result.get();
    CircuitBreaker<Integer> delegate = (CircuitBreaker<Integer>) Whitebox.getInternalState(wrapper, "delegate");
    Function<Response, Integer> eventConverter = (Function<Response, Integer>) Whitebox.getInternalState(wrapper, "eventConverter");
    assertThat(delegate).isSameAs(CircuitBreakerForHttpStatusCode.getDefaultHttpStatusCodeCircuitBreakerForKey(host));
    Response responseMock = mock(Response.class);
    doReturn(42).when(responseMock).getStatusCode();
    assertThat(eventConverter.apply(responseMock)).isEqualTo(42);
    assertThat(eventConverter.apply(null)).isNull();
    if (useNettyEventLoop) {
        assertThat(Whitebox.getInternalState(delegate, "scheduler")).isEqualTo(eventLoopMock);
        assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isEqualTo(eventLoopMock);
    } else {
        assertThat(Whitebox.getInternalState(delegate, "scheduler")).isNotEqualTo(eventLoopMock);
        assertThat(Whitebox.getInternalState(delegate, "stateChangeNotificationExecutor")).isNotEqualTo(eventLoopMock);
    }
}
Also used : CircuitBreaker(com.nike.fastbreak.CircuitBreaker) Matchers.anyString(org.mockito.Matchers.anyString) Response(com.ning.http.client.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(java.util.function.Function) CircuitBreakerDelegate(com.nike.fastbreak.CircuitBreakerDelegate) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 60 with DataProvider

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

the class AsyncHttpClientHelperTest method executeAsyncHttpRequest_sets_up_and_executes_call_as_expected.

@DataProvider(value = { "true   |   true", "true   |   false", "false  |   true", "false  |   false" }, splitBy = "\\|")
@Test
public void executeAsyncHttpRequest_sets_up_and_executes_call_as_expected(boolean performSubspan, boolean currentTracingInfoNull) {
    // given
    Whitebox.setInternalState(helperSpy, "performSubSpanAroundDownstreamCalls", performSubspan);
    CircuitBreaker<Response> circuitBreakerMock = mock(CircuitBreaker.class);
    doReturn(Optional.of(circuitBreakerMock)).when(helperSpy).getCircuitBreaker(any(RequestBuilderWrapper.class));
    ManualModeTask<Response> cbManualTaskMock = mock(ManualModeTask.class);
    doReturn(cbManualTaskMock).when(circuitBreakerMock).newManualModeTask();
    String url = "http://localhost/some/path";
    String method = "GET";
    AsyncHttpClient.BoundRequestBuilder reqMock = mock(AsyncHttpClient.BoundRequestBuilder.class);
    RequestBuilderWrapper rbw = new RequestBuilderWrapper(url, method, reqMock, Optional.empty(), false);
    AsyncResponseHandler responseHandlerMock = mock(AsyncResponseHandler.class);
    Span initialSpan = (currentTracingInfoNull) ? null : Tracer.getInstance().startRequestWithRootSpan("foo");
    Deque<Span> initialSpanStack = (currentTracingInfoNull) ? null : Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> initialMdc = (currentTracingInfoNull) ? null : MDC.getCopyOfContextMap();
    resetTracingAndMdc();
    // when
    CompletableFuture resultFuture = helperSpy.executeAsyncHttpRequest(rbw, responseHandlerMock, initialSpanStack, initialMdc);
    // then
    // Verify that the circuit breaker came from the getCircuitBreaker helper method and that its
    //      throwExceptionIfCircuitBreakerIsOpen() method was called.
    verify(helperSpy).getCircuitBreaker(rbw);
    verify(cbManualTaskMock).throwExceptionIfCircuitBreakerIsOpen();
    // Verify that the inner request's execute method was called with a
    //      AsyncCompletionHandlerWithTracingAndMdcSupport for the handler.
    ArgumentCaptor<AsyncHandler> executedHandlerCaptor = ArgumentCaptor.forClass(AsyncHandler.class);
    verify(reqMock).execute(executedHandlerCaptor.capture());
    AsyncHandler executedHandler = executedHandlerCaptor.getValue();
    assertThat(executedHandler).isInstanceOf(AsyncCompletionHandlerWithTracingAndMdcSupport.class);
    // Verify that the AsyncCompletionHandlerWithTracingAndMdcSupport was created with the expected args
    AsyncCompletionHandlerWithTracingAndMdcSupport achwtams = (AsyncCompletionHandlerWithTracingAndMdcSupport) executedHandler;
    assertThat(achwtams.completableFutureResponse).isSameAs(resultFuture);
    assertThat(achwtams.responseHandlerFunction).isSameAs(responseHandlerMock);
    assertThat(achwtams.performSubSpanAroundDownstreamCalls).isEqualTo(performSubspan);
    assertThat(achwtams.circuitBreakerManualTask).isEqualTo(Optional.of(cbManualTaskMock));
    if (performSubspan) {
        int initialSpanStackSize = (initialSpanStack == null) ? 0 : initialSpanStack.size();
        assertThat(achwtams.distributedTraceStackToUse).hasSize(initialSpanStackSize + 1);
        Span subspan = (Span) achwtams.distributedTraceStackToUse.peek();
        assertThat(subspan.getSpanName()).isEqualTo(handlerWithTracingAndMdcDummyExample.getSubspanSpanName(method, url));
        if (initialSpan != null) {
            assertThat(subspan.getTraceId()).isEqualTo(initialSpan.getTraceId());
            assertThat(subspan.getParentSpanId()).isEqualTo(initialSpan.getSpanId());
        }
        assertThat(achwtams.mdcContextToUse.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
    } else {
        assertThat(achwtams.distributedTraceStackToUse).isSameAs(initialSpanStack);
        assertThat(achwtams.mdcContextToUse).isSameAs(initialMdc);
    }
    // Verify that the trace headers were added (or not depending on state).
    Span spanForDownstreamCall = achwtams.getSpanForCall();
    if (initialSpan == null && !performSubspan) {
        assertThat(spanForDownstreamCall).isNull();
        verifyZeroInteractions(reqMock);
    } else {
        assertThat(spanForDownstreamCall).isNotNull();
        verify(reqMock).setHeader(TraceHeaders.TRACE_SAMPLED, String.valueOf(spanForDownstreamCall.isSampleable()));
        verify(reqMock).setHeader(TraceHeaders.TRACE_ID, spanForDownstreamCall.getTraceId());
        verify(reqMock).setHeader(TraceHeaders.SPAN_ID, spanForDownstreamCall.getSpanId());
        verify(reqMock).setHeader(TraceHeaders.PARENT_SPAN_ID, spanForDownstreamCall.getParentSpanId());
        verify(reqMock).setHeader(TraceHeaders.SPAN_NAME, spanForDownstreamCall.getSpanName());
    }
}
Also used : AsyncHandler(com.ning.http.client.AsyncHandler) Matchers.anyString(org.mockito.Matchers.anyString) Span(com.nike.wingtips.Span) Response(com.ning.http.client.Response) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) 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