Search in sources :

Example 1 with DataProvider

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

the class SignalFxEndpointMetricsHandlerTest method two_arg_constructor_fails_with_IllegalArgumentException_if_reporting_frequency_args_are_null.

@DataProvider(value = { "true   |   false", "false  |   true" }, splitBy = "\\|")
@Test
public void two_arg_constructor_fails_with_IllegalArgumentException_if_reporting_frequency_args_are_null(boolean amountIsNull, boolean timeUnitIsNull) {
    // given
    SignalFxReporterFactory reporterFactoryMock = mock(SignalFxReporterFactory.class);
    wireUpReporterFactoryMockForConstructor(reporterFactoryMock, metricRegistryMock);
    if (amountIsNull)
        doReturn(null).when(reporterFactoryMock).getInterval();
    if (timeUnitIsNull)
        doReturn(null).when(reporterFactoryMock).getTimeUnit();
    // when
    Throwable ex = catchThrowable(() -> new SignalFxEndpointMetricsHandler(reporterFactoryMock, metricRegistryMock));
    // then
    if (amountIsNull) {
        assertThat(ex).isInstanceOf(IllegalArgumentException.class).hasMessage("reportingFrequency amount cannot be null");
    }
    if (timeUnitIsNull) {
        assertThat(ex).isInstanceOf(IllegalArgumentException.class).hasMessage("reportingFrequency TimeUnit cannot be null");
    }
}
Also used : SignalFxReporterFactory(com.nike.riposte.metrics.codahale.contrib.SignalFxReporterFactory) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 2 with DataProvider

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

the class SignalFxEndpointMetricsHandlerTest method three_arg_constructor_fails_with_IllegalArgumentException_if_reporting_frequency_args_are_null.

@DataProvider(value = { "true   |   false", "false  |   true" }, splitBy = "\\|")
@Test
public void three_arg_constructor_fails_with_IllegalArgumentException_if_reporting_frequency_args_are_null(boolean amountIsNull, boolean timeUnitIsNull) {
    // given
    SignalFxReporter reporterMock = mock(SignalFxReporter.class);
    wireUpReporterForConstructor(reporterMock);
    Long amount = (amountIsNull) ? null : 42L;
    TimeUnit timeUnit = (timeUnitIsNull) ? null : TimeUnit.DAYS;
    // when
    Throwable ex = catchThrowable(() -> new SignalFxEndpointMetricsHandler(reporterMock, Pair.of(amount, timeUnit), metricRegistryMock));
    // then
    if (amountIsNull) {
        assertThat(ex).isInstanceOf(IllegalArgumentException.class).hasMessage("reportingFrequency amount cannot be null");
    }
    if (timeUnitIsNull) {
        assertThat(ex).isInstanceOf(IllegalArgumentException.class).hasMessage("reportingFrequency TimeUnit cannot be null");
    }
}
Also used : SignalFxReporter(com.signalfx.codahale.reporter.SignalFxReporter) Matchers.anyLong(org.mockito.Matchers.anyLong) TimeUnit(java.util.concurrent.TimeUnit) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 3 with DataProvider

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

the class ChannelFutureListenerWithTracingAndMdcTest 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();
    ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc(consumerMock, spanStack, mdcInfo);
    resetTracingAndMdc();
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
    // when
    Throwable ex = catchThrowable(() -> instance.operationComplete(inObj));
    // then
    verify(consumerMock).accept(inObj);
    if (throwException) {
        assertThat(ex).isNotNull();
    } else {
        assertThat(ex).isNull();
    }
    assertThat(currentSpanStackWhenChannelFutureWasCalled.get(0)).isEqualTo(spanStack);
    assertThat(currentMdcInfoWhenChannelFutureWasCalled.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 4 with DataProvider

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

the class FunctionWithTracingAndMdcSupportTest 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();
    FunctionWithTracingAndMdcSupport instance = new FunctionWithTracingAndMdcSupport(functionMock, spanStack, mdcInfo);
    resetTracingAndMdc();
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
    assertThat(MDC.getCopyOfContextMap()).isEmpty();
    // when
    Throwable ex = null;
    Object result = null;
    try {
        result = instance.apply(inObj);
    } catch (Throwable t) {
        ex = t;
    }
    // then
    verify(functionMock).apply(inObj);
    if (throwException) {
        assertThat(ex).isNotNull();
        assertThat(result).isNull();
    } else {
        assertThat(ex).isNull();
        assertThat(result).isSameAs(outObj);
    }
    assertThat(currentSpanStackWhenFunctionWasCalled.get(0)).isEqualTo(spanStack);
    assertThat(currentMdcInfoWhenFunctionWasCalled.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 5 with DataProvider

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

the class SignalFxEndpointMetricsHandlerTest method handleRequest_gracefully_handles_some_null_args.

@DataProvider(value = { "true   |   false   |   false", "false  |   true    |   false", "false  |   false   |   true", "true   |   true    |   true" }, splitBy = "\\|")
@Test
public void handleRequest_gracefully_handles_some_null_args(boolean endpointIsNull, boolean methodIsNull, boolean matchingPathTemplateIsNull) {
    // given
    int statusCode = 242;
    int statusCodeXXValue = 2;
    long elapsedTimeMillis = 42;
    Endpoint endpoint = (endpointIsNull) ? null : endpointMock;
    doReturn(endpoint).when(httpStateMock).getEndpointForExecution();
    String expectedEndpointClass = (endpointIsNull) ? "NONE" : endpoint.getClass().getName();
    HttpMethod method = (methodIsNull) ? null : httpMethod;
    doReturn(method).when(requestInfoMock).getMethod();
    String expectedMethodName = (methodIsNull) ? "NONE" : method.name();
    String pathTemplate = (matchingPathTemplateIsNull) ? null : matchingPathTemplate;
    doReturn(pathTemplate).when(httpStateMock).getMatchingPathTemplate();
    String expectedPathTemplate = (matchingPathTemplateIsNull) ? "NONE" : pathTemplate;
    // when
    handler.handleRequest(requestInfoMock, responseInfoMock, httpStateMock, statusCode, statusCodeXXValue, elapsedTimeMillis);
    // then
    verify(metricMetadataMock).forBuilder(requestTimerBuilderMock);
    verify(dimensionConfiguratorMock).setupMetricWithDimensions(timerBuilderTaggerMock, requestInfoMock, responseInfoMock, httpStateMock, statusCode, statusCodeXXValue, elapsedTimeMillis, endpoint, expectedEndpointClass, expectedMethodName, expectedPathTemplate);
    verify(timerBuilderTaggerMock).createOrGet(metricRegistryMock);
    verify(timerMock).update(elapsedTimeMillis, TimeUnit.MILLISECONDS);
}
Also used : Endpoint(com.nike.riposte.server.http.Endpoint) Matchers.anyString(org.mockito.Matchers.anyString) Endpoint(com.nike.riposte.server.http.Endpoint) HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)176 Test (org.junit.Test)169 AssertionInfo (org.assertj.core.api.AssertionInfo)34 Matchers.anyString (org.mockito.Matchers.anyString)23 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)22 Span (com.nike.wingtips.Span)21 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)21 Pair (com.nike.internal.util.Pair)15 ExtractableResponse (io.restassured.response.ExtractableResponse)11 Timer (com.codahale.metrics.Timer)9 Endpoint (com.nike.riposte.server.http.Endpoint)9 RequestInfo (com.nike.riposte.server.http.RequestInfo)9 HttpMethod (io.netty.handler.codec.http.HttpMethod)9 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)8 HashMap (java.util.HashMap)8 Histogram (com.codahale.metrics.Histogram)7 Map (java.util.Map)7 Meter (com.codahale.metrics.Meter)6 IntegersBaseTest (org.assertj.core.internal.IntegersBaseTest)6 LongsBaseTest (org.assertj.core.internal.LongsBaseTest)6