Search in sources :

Example 71 with DataProvider

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

the class ConsumerWithTracingAndMdcSupportTest method apply_handles_tracing_and_mdc_info_as_expected.

@DataProvider(value = { "true", "false" })
@Test
public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) {
    // given
    throwExceptionDuringCall = throwException;
    Tracer.getInstance().startRequestWithRootSpan("foo");
    Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> mdcInfo = MDC.getCopyOfContextMap();
    ConsumerWithTracingAndMdcSupport instance = new ConsumerWithTracingAndMdcSupport(consumerMock, spanStack, mdcInfo);
    resetTracingAndMdc();
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
    // when
    Throwable ex = catchThrowable(() -> instance.accept(inObj));
    // then
    verify(consumerMock).accept(inObj);
    if (throwException) {
        assertThat(ex).isNotNull();
    } else {
        assertThat(ex).isNull();
    }
    assertThat(currentSpanStackWhenConsumerWasCalled.get(0)).isEqualTo(spanStack);
    assertThat(currentMdcInfoWhenConsumerWasCalled.get(0)).isEqualTo(mdcInfo);
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Span(com.nike.wingtips.Span) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 72 with DataProvider

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

the class SignalFxEndpointMetricsHandlerTest method kitchen_sink_constructor_fails_with_IllegalArgumentException_if_certain_args_are_null.

@DataProvider(value = { "true   |   false   |   false   |   signalFxReporterMetricMetadata cannot be null", "false  |   true    |   false   |   metricRegistry cannot be null", "false  |   false   |   true    |   requestTimerBuilder cannot be null" }, splitBy = "\\|")
@Test
public void kitchen_sink_constructor_fails_with_IllegalArgumentException_if_certain_args_are_null(boolean metadataIsNull, boolean registryIsNull, boolean timerBuilderIsNull, String expectedMessage) {
    // given
    MetricMetadata metricMetadata = (metadataIsNull) ? null : metricMetadataMock;
    MetricRegistry registry = (registryIsNull) ? null : metricRegistryMock;
    MetricBuilder<Timer> timerBuilder = (timerBuilderIsNull) ? null : requestTimerBuilderMock;
    // when
    Throwable ex = catchThrowable(() -> new SignalFxEndpointMetricsHandler(metricMetadata, registry, timerBuilder, null));
    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class).hasMessage(expectedMessage);
}
Also used : MetricMetadata(com.signalfx.codahale.reporter.MetricMetadata) Timer(com.codahale.metrics.Timer) MetricRegistry(com.codahale.metrics.MetricRegistry) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 73 with DataProvider

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

the class VerifyResponseHttpStatusCodeHandlingRfcCorrectnessComponentTest method responseStatusCodeScenariosDataProvider.

@DataProvider
public static Object[][] responseStatusCodeScenariosDataProvider() {
    // We don't need to test *ALL* possibilities, just the extended list (http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml)
    //      plus a few unassigned ones.
    List<Integer> statusCodesToTest = IntStream.rangeClosed(200, // No good way to test codes less than 200 at the moment.
    999).filter(val -> (val <= 230 || val >= 300)).filter(val -> (val <= 320 || val >= 400)).filter(val -> (val <= 435 || val == 451 || val >= 500)).filter(val -> (val <= 520 || val >= 600)).filter(val -> (val <= 610 || val >= 700)).filter(val -> (val <= 710 || val >= 800)).filter(val -> (val <= 810 || val >= 900)).filter(val -> val <= 910).boxed().collect(Collectors.toList());
    Object[][] data = new Object[statusCodesToTest.size() * 2][2];
    for (int i = 0; i < statusCodesToTest.size(); i++) {
        int statusCode = statusCodesToTest.get(i);
        int dataIndexBase = i * 2;
        data[dataIndexBase] = new Object[] { statusCode, true };
        data[dataIndexBase + 1] = new Object[] { statusCode, false };
    }
    return data;
}
Also used : IntStream(java.util.stream.IntStream) RequestInfo(com.nike.riposte.server.http.RequestInfo) BeforeClass(org.junit.BeforeClass) Level(uk.org.lidalia.slf4jext.Level) TestLoggerFactory(uk.org.lidalia.slf4jtest.TestLoggerFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) CompletableFuture(java.util.concurrent.CompletableFuture) StreamingAsyncHttpClient(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient) ServerConfig(com.nike.riposte.server.config.ServerConfig) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CALL_ID_RESPONSE_HEADER_KEY(com.nike.riposte.server.componenttest.VerifyResponseHttpStatusCodeHandlingRfcCorrectnessComponentTest.BackendEndpoint.CALL_ID_RESPONSE_HEADER_KEY) RedirectConfig.redirectConfig(io.restassured.config.RedirectConfig.redirectConfig) Server(com.nike.riposte.server.Server) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) ComponentTestUtils(com.nike.riposte.server.testutils.ComponentTestUtils) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Collection(java.util.Collection) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) UUID(java.util.UUID) TRANSFER_ENCODING(io.netty.handler.codec.http.HttpHeaders.Names.TRANSFER_ENCODING) Collectors(java.util.stream.Collectors) CHUNKED(io.netty.handler.codec.http.HttpHeaders.Values.CHUNKED) RestAssured.config(io.restassured.RestAssured.config) Endpoint(com.nike.riposte.server.http.Endpoint) ExtractableResponse(io.restassured.response.ExtractableResponse) Matcher(com.nike.riposte.util.Matcher) List(java.util.List) SimpleProxyRouterEndpoint(com.nike.riposte.server.http.impl.SimpleProxyRouterEndpoint) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Optional(java.util.Optional) CONTENT_LENGTH(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH) RestAssured.given(io.restassured.RestAssured.given) Collections(java.util.Collections) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) SimpleProxyRouterEndpoint(com.nike.riposte.server.http.impl.SimpleProxyRouterEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 74 with DataProvider

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

the class VerifyRequestAndResponseFilteringComponentTest method verify_filters_work_for_non_short_circuit_calls_for_endpoints_and_errors.

@DataProvider(value = { "false", "true" }, splitBy = "\\|")
@Test
public void verify_filters_work_for_non_short_circuit_calls_for_endpoints_and_errors(boolean forceError) throws IOException, InterruptedException {
    ExtractableResponse response = given().baseUri("http://127.0.0.1").port(serverConfig.endpointsPort()).basePath(BasicEndpoint.MATCHING_PATH).header(BasicEndpoint.FORCE_ERROR_HEADER_KEY, String.valueOf(forceError)).log().all().when().get().then().log().headers().extract();
    // Should have hit the endpoint
    if (forceError)
        verifyErrorReceived(response, BasicEndpoint.FORCED_ERROR);
    else
        assertThat(response.asString()).isEqualTo(BasicEndpoint.RESPONSE_PAYLOAD);
    // All the filter-specific request and response headers should be present.
    assertThat(response.headers().getValues(FIRST_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(FIRST_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(FIRST_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    assertThat(response.headers().getValues(SECOND_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(SECOND_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(SECOND_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    assertThat(response.headers().getValues(THIRD_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(THIRD_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.headers().getValues(THIRD_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    // The override request and response headers should be correct - last filter wins - so third filter for the request and first filter for the response
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_FIRST_CHUNK_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_REQUEST_FIRST_CHUNK_OVERRIDE_HEADER_VALUE));
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_REQUEST_LAST_CHUNK_OVERRIDE_HEADER_VALUE));
    assertThat(response.headers().getValues(COMMON_FILTER_RESPONSE_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_RESPONSE_OVERRIDE_HEADER_KEY));
    // The cumulative request and response headers should be correct (contain all values from all filters)
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(FIRST_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE, SECOND_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE, THIRD_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE));
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(FIRST_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_VALUE, SECOND_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_VALUE, THIRD_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_VALUE));
    assertThat(response.headers().getValues(COMMON_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(THIRD_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY, SECOND_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY, FIRST_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY));
}
Also used : ExtractableResponse(io.restassured.response.ExtractableResponse) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 75 with DataProvider

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

the class VerifyRequestAndResponseFilteringComponentTest method verify_filters_work_for_last_chunk_short_circuit_calls.

@DataProvider(value = { "false", "true" }, splitBy = "\\|")
@Test
public void verify_filters_work_for_last_chunk_short_circuit_calls(boolean hit404Path) throws IOException, InterruptedException {
    String basePath = (hit404Path) ? "/foobardoesnotexist" : BasicEndpoint.MATCHING_PATH;
    ExtractableResponse response = given().baseUri("http://127.0.0.1").port(serverConfig.endpointsPort()).basePath(basePath).header(SecondFilterShortCircuiting.SHOULD_SHORT_CIRCUIT_LAST_CHUNK, "true").log().all().when().get().then().log().headers().extract();
    //      * Should have thrown a 404 after the first chunk was fully filtered for an invalid 404 path.
    if (hit404Path)
        verifyErrorReceived(response, SampleCoreApiError.NOT_FOUND);
    else
        assertThat(response.asString()).isEqualTo(SecondFilterShortCircuiting.SHORT_CIRCUIT_LAST_CHUNK_RESPONSE_PAYLOAD);
    // Some of the filter-specific request and response headers should be present - the ones added after the short circuit should not be present.
    assertThat(response.headers().getValues(FIRST_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    if (hit404Path) {
        // 404 prevents last chunk filters from running because the 404 exception is thrown after the first chunk is processed but before the last chunk.
        assertThat(response.headers().getValues(FIRST_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEmpty();
    } else {
        assertThat(response.headers().getValues(FIRST_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_VALUE));
    }
    assertThat(response.headers().getValues(FIRST_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    assertThat(response.headers().getValues(SECOND_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    if (hit404Path) {
        // 404 prevents last chunk filters from running because the 404 exception is thrown after the first chunk is processed but before the last chunk.
        assertThat(response.headers().getValues(SECOND_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEmpty();
    } else {
        assertThat(response.headers().getValues(SECOND_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_VALUE));
    }
    assertThat(response.headers().getValues(SECOND_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    assertThat(response.headers().getValues(THIRD_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_ONLY_FIRST_CHUNK_REQ_HEADER_VALUE));
    assertThat(response.header(THIRD_FILTER_ONLY_LAST_CHUNK_REQ_HEADER_KEY)).isNull();
    assertThat(response.headers().getValues(THIRD_FILTER_ONLY_RESPONSE_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_ONLY_RESPONSE_HEADER_VALUE));
    // The override request and response headers should be correct based on when the short circuit occurred
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_FIRST_CHUNK_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(THIRD_FILTER_REQUEST_FIRST_CHUNK_OVERRIDE_HEADER_VALUE));
    if (hit404Path) {
        // 404 prevents last chunk filters from running because the 404 exception is thrown after the first chunk is processed but before the last chunk.
        assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_OVERRIDE_HEADER_KEY)).isEmpty();
    } else {
        assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(SECOND_FILTER_REQUEST_LAST_CHUNK_OVERRIDE_HEADER_VALUE));
    }
    assertThat(response.headers().getValues(COMMON_FILTER_RESPONSE_OVERRIDE_HEADER_KEY)).isEqualTo(singletonList(FIRST_FILTER_RESPONSE_OVERRIDE_HEADER_KEY));
    // The cumulative request and response headers should be correct based on when the short circuit occurred
    assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(FIRST_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE, SECOND_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE, THIRD_FILTER_REQUEST_FIRST_CHUNK_CUMULATIVE_HEADER_VALUE));
    if (hit404Path) {
        // 404 prevents last chunk filters from running because the 404 exception is thrown after the first chunk is processed but before the last chunk.
        assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_KEY)).isEmpty();
    } else {
        assertThat(response.headers().getValues(COMMON_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(FIRST_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_VALUE, SECOND_FILTER_REQUEST_LAST_CHUNK_CUMULATIVE_HEADER_VALUE));
    }
    assertThat(response.headers().getValues(COMMON_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY)).isEqualTo(Arrays.asList(THIRD_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY, SECOND_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY, FIRST_FILTER_RESPONSE_CUMULATIVE_HEADER_KEY));
}
Also used : ExtractableResponse(io.restassured.response.ExtractableResponse) 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