Search in sources :

Example 26 with DataProvider

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

the class CodahaleMetricsListenerTest method initServerConfigMetrics_adds_expected_metrics.

@DataProvider(value = { "true", "false" })
@Test
public void initServerConfigMetrics_adds_expected_metrics(boolean includeServerConfigMetrics) {
    // given
    setupMetricRegistryAndCodahaleMetricsCollector();
    CodahaleMetricsListener instance = CodahaleMetricsListener.newBuilder(cmcMock).withEndpointMetricsHandler(endpointMetricsHandlerMock).withIncludeServerConfigMetrics(includeServerConfigMetrics).build();
    verifyServerStatisticMetrics(instance);
    String expectedBossThreadsGaugeName = name(ServerConfig.class.getSimpleName(), "boss_threads");
    String expectedWorkerThreadsGaugeName = name(ServerConfig.class.getSimpleName(), "worker_threads");
    String expectedMaxRequestSizeInBytesGaugeName = name(ServerConfig.class.getSimpleName(), "max_request_size_in_bytes");
    String expectedEndpointsListGaugeName = name(ServerConfig.class.getSimpleName(), "endpoints");
    List<String> expectedEndpointsListValue = serverConfig.appEndpoints().stream().map(endpoint -> endpoint.getClass().getName() + "-" + instance.getMatchingHttpMethodsAsCombinedString(endpoint) + "-" + endpoint.requestMatcher().matchingPathTemplates()).collect(Collectors.toList());
    // when
    instance.initEndpointAndServerConfigMetrics(serverConfig);
    // then
    if (includeServerConfigMetrics) {
        // Metrics for server config values
        assertThat(registeredGauges).containsKey(expectedBossThreadsGaugeName);
        assertThat(registeredGauges.get(expectedBossThreadsGaugeName).getValue()).isEqualTo(serverConfig.numBossThreads());
        assertThat(registeredGauges).containsKey(expectedWorkerThreadsGaugeName);
        assertThat(registeredGauges.get(expectedWorkerThreadsGaugeName).getValue()).isEqualTo(serverConfig.numWorkerThreads());
        assertThat(registeredGauges).containsKey(expectedMaxRequestSizeInBytesGaugeName);
        assertThat(registeredGauges.get(expectedMaxRequestSizeInBytesGaugeName).getValue()).isEqualTo(serverConfig.maxRequestSizeInBytes());
        assertThat(registeredGauges).containsKey(expectedEndpointsListGaugeName);
        assertThat(registeredGauges.get(expectedEndpointsListGaugeName).getValue()).isEqualTo(expectedEndpointsListValue);
    } else {
        // No server config values should have been registered.
        verifyNoMoreInteractions(metricRegistryMock);
    }
    // In either case, the EndpointMetricsHandler should have been called to delegate setting up endpoint-specific metrics.
    verify(endpointMetricsHandlerMock).setupEndpointsMetrics(serverConfig, metricRegistryMock);
}
Also used : Arrays(java.util.Arrays) MetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.MetricNamingStrategy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseInfo(com.nike.riposte.server.http.ResponseInfo) EndpointMetricsHandlerDefaultImpl(com.nike.riposte.metrics.codahale.impl.EndpointMetricsHandlerDefaultImpl) ServerConfig(com.nike.riposte.server.config.ServerConfig) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.doThrow(org.mockito.Mockito.doThrow) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Counter(com.codahale.metrics.Counter) Matchers.anyInt(org.mockito.Matchers.anyInt) DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DEFAULT_REQUEST_AND_RESPONSE_SIZE_HISTOGRAM_SUPPLIER) Mockito.doReturn(org.mockito.Mockito.doReturn) Builder(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.Builder) DefaultMetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy) Collection(java.util.Collection) Metric(com.codahale.metrics.Metric) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) Endpoint(com.nike.riposte.server.http.Endpoint) List(java.util.List) Whitebox(org.mockito.internal.util.reflection.Whitebox) Timer(com.codahale.metrics.Timer) ResponseSender(com.nike.riposte.server.http.ResponseSender) Gauge(com.codahale.metrics.Gauge) Mockito.mock(org.mockito.Mockito.mock) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) Histogram(com.codahale.metrics.Histogram) RequestInfo(com.nike.riposte.server.http.RequestInfo) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ServerStatisticsMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerStatisticsMetricNames) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Supplier(java.util.function.Supplier) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfigMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerConfigMetricNames) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Meter(com.codahale.metrics.Meter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.anyLong(org.mockito.Matchers.anyLong) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DEFAULT_PREFIX(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy.DEFAULT_PREFIX) DEFAULT_WORD_DELIMITER(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy.DEFAULT_WORD_DELIMITER) Before(org.junit.Before) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Mockito.verify(org.mockito.Mockito.verify) Matcher(com.nike.riposte.util.Matcher) Mockito.never(org.mockito.Mockito.never) ChronoUnit(java.time.temporal.ChronoUnit) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) ServerConfig(com.nike.riposte.server.config.ServerConfig) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 27 with DataProvider

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

the class CodahaleMetricsListenerTest method builder_works_as_expected_for_specified_fields.

@DataProvider(value = { "true   |   true", "true   |   false", "false  |   true", "false  |   false" }, splitBy = "\\|")
@Test
public void builder_works_as_expected_for_specified_fields(boolean overrideCodahaleMetricsCollector, boolean includeServerConfigMetrics) {
    // given
    setupMetricRegistryAndCodahaleMetricsCollector();
    CodahaleMetricsCollector alternateCmcMock = mock(CodahaleMetricsCollector.class);
    doReturn(metricRegistryMock).when(alternateCmcMock).getMetricRegistry();
    MetricNamingStrategy<ServerStatisticsMetricNames> statsNamingStrat = new DefaultMetricNamingStrategy<>();
    MetricNamingStrategy<ServerConfigMetricNames> configNamingStrat = new DefaultMetricNamingStrategy<>();
    Supplier<Histogram> histogramSupplier = () -> mock(Histogram.class);
    Builder builder = CodahaleMetricsListener.newBuilder(cmcMock);
    if (overrideCodahaleMetricsCollector)
        builder = builder.withMetricsCollector(alternateCmcMock);
    builder = builder.withEndpointMetricsHandler(endpointMetricsHandlerMock).withIncludeServerConfigMetrics(includeServerConfigMetrics).withServerConfigMetricNamingStrategy(configNamingStrat).withServerStatsMetricNamingStrategy(statsNamingStrat).withRequestAndResponseSizeHistogramSupplier(histogramSupplier);
    // when
    CodahaleMetricsListener result = builder.build();
    // then
    if (overrideCodahaleMetricsCollector)
        assertThat(result.metricsCollector).isSameAs(alternateCmcMock);
    else
        assertThat(result.metricsCollector).isSameAs(cmcMock);
    assertThat(result.endpointMetricsHandler).isSameAs(endpointMetricsHandlerMock);
    assertThat(result.includeServerConfigMetrics).isEqualTo(includeServerConfigMetrics);
    assertThat(result.serverConfigMetricNamingStrategy).isSameAs(configNamingStrat);
    assertThat(result.serverStatsMetricNamingStrategy).isSameAs(statsNamingStrat);
    assertThat(result.requestAndResponseSizeHistogramSupplier).isSameAs(histogramSupplier);
}
Also used : Histogram(com.codahale.metrics.Histogram) DefaultMetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy) ServerConfigMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerConfigMetricNames) ServerStatisticsMetricNames(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.ServerStatisticsMetricNames) Builder(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.Builder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 28 with DataProvider

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

the class AllowAllTheThingsCORSFilterTest method filterRequestLastChunkWithOptionalShortCircuitResponse_always_returns_null.

@DataProvider(value = { "OPTIONS", "GET", "POST", "PUT" }, splitBy = "\\|")
@Test
public void filterRequestLastChunkWithOptionalShortCircuitResponse_always_returns_null(String httpMethodString) {
    // given
    HttpMethod method = HttpMethod.valueOf(httpMethodString);
    doReturn(method).when(requestMock).getMethod();
    // when
    Pair<RequestInfo<?>, Optional<ResponseInfo<?>>> result = filter.filterRequestLastChunkWithOptionalShortCircuitResponse(requestMock, ctxMock);
    // then
    assertThat(result).isNull();
}
Also used : Optional(java.util.Optional) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 29 with DataProvider

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

the class AllowAllTheThingsCORSFilterTest method filterRequestFirstChunkWithOptionalShortCircuitResponse_short_circuits_on_CORS_preflight_OPTIONS_request.

@DataProvider(value = { "OPTIONS    |   true", "GET        |   false", "POST       |   false", "PUT        |   false" }, splitBy = "\\|")
@Test
public void filterRequestFirstChunkWithOptionalShortCircuitResponse_short_circuits_on_CORS_preflight_OPTIONS_request(String httpMethodString, boolean expectShortCircuit) {
    // given
    HttpMethod method = HttpMethod.valueOf(httpMethodString);
    doReturn(method).when(requestMock).getMethod();
    // when
    Pair<RequestInfo<?>, Optional<ResponseInfo<?>>> result = filter.filterRequestFirstChunkWithOptionalShortCircuitResponse(requestMock, ctxMock);
    // then
    if (expectShortCircuit) {
        assertThat(result).isNotNull();
        assertThat(result.getLeft()).isSameAs(requestMock);
        assertThat(result.getRight()).isPresent();
        assertThat(result.getRight().get().getHttpStatusCode()).isEqualTo(200);
    } else
        assertThat(result).isNull();
}
Also used : Optional(java.util.Optional) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 30 with DataProvider

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

the class MainClassUtilsTest method getAppIdAndEnvironmentFromSystemProperties_works_as_expected.

@DataProvider(value = { "foo    |   null    |   bar     |   null    |   false   |   foo     |   bar", "foo    |   notused |   bar     |   notused |   false   |   foo     |   bar", "null   |   foo     |   null    |   bar     |   false   |   foo     |   bar", "null   |   null    |   bar     |   notused |   true    |   null    |   null", "foo    |   notused |   null    |   null    |   true    |   null    |   null" }, splitBy = "\\|")
@Test
public void getAppIdAndEnvironmentFromSystemProperties_works_as_expected(String appId, String archaiusAppId, String environment, String archaiusEnvironment, boolean expectIllegalStateException, String expectedAppId, String expectedEnvironment) {
    // given
    setAppIdAndEnvironemntSystemProperties(appId, archaiusAppId, environment, archaiusEnvironment);
    // when
    Throwable ex = null;
    Pair<String, String> results = null;
    try {
        results = MainClassUtils.getAppIdAndEnvironmentFromSystemProperties();
    } catch (Throwable t) {
        ex = t;
    }
    // then
    if (expectIllegalStateException)
        assertThat(ex).isInstanceOf(IllegalStateException.class);
    else {
        assertThat(ex).isNull();
        assertThat(results.getLeft()).isEqualTo(expectedAppId);
        assertThat(results.getRight()).isEqualTo(expectedEnvironment);
    }
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) 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