Search in sources :

Example 51 with DataProvider

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

the class EurekaHandlerTest method createEurekaInstanceConfig_returns_MyDataCenterInstanceConfig_when_datacenterType_is_null_or_MyOwn_or_invalid.

@DataProvider(value = { "null", "MyOwn", "not-a-real-datacenter-type" })
@Test
public void createEurekaInstanceConfig_returns_MyDataCenterInstanceConfig_when_datacenterType_is_null_or_MyOwn_or_invalid(String datacenterType) {
    // given
    doReturn(datacenterType).when(datacenterTypePropertySupplierMock).get();
    // when
    EurekaInstanceConfig instanceConfig = handlerSpy.createEurekaInstanceConfig();
    // then
    assertThat(instanceConfig).isInstanceOf(MyDataCenterInstanceConfig.class);
    assertThat(Whitebox.getInternalState(instanceConfig, "namespace")).isEqualTo(handlerSpy.eurekaClientNamespace);
}
Also used : EurekaInstanceConfig(com.netflix.appinfo.EurekaInstanceConfig) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 52 with DataProvider

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

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_SENT_without_endpoint.

@DataProvider(value = { "200", "404", "405", "500" })
@Test
public void onEvent_works_as_expected_for_RESPONSE_SENT_without_endpoint(int responseStatusCode) {
    // Should work the same as the onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint test above
    //      as far as CodahaleMetricsListener is concerned - it's the EndpointMetricsHandler's responsibility
    //      to deal with any intricacies around this use case.
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT;
    state.setEndpointForExecution(null, null);
    doReturn(responseStatusCode).when(responseInfoMock).getHttpStatusCodeWithDefault(ResponseSender.DEFAULT_HTTP_STATUS_CODE);
    int requestRawContentLengthBytes = (int) (Math.random() * 10000);
    doReturn(requestRawContentLengthBytes).when(requestInfoMock).getRawContentLengthInBytes();
    long finalResponseContentLength = (long) (Math.random() * 10000);
    doReturn(finalResponseContentLength).when(responseInfoMock).getFinalContentLength();
    // when
    long beforeCallTime = System.currentTimeMillis();
    listener.onEvent(event, state);
    long afterCallTime = System.currentTimeMillis();
    // then
    // Inflight requests counter decremented
    verify(listener.inflightRequests).dec();
    // Processed requests counter incremented
    verify(listener.processedRequests).inc();
    // If response code is greater than or equal to 400, then the failed requests counter should be incremented.
    if (responseStatusCode >= 400)
        verify(listener.failedRequests).inc();
    // Request and response size histograms should be updated with the relevant values from the request and response.
    verify(listener.requestSizes).update(requestRawContentLengthBytes);
    verify(listener.responseSizes).update(finalResponseContentLength);
    // The EndpointMetricsHandler should have been notified
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long expectedElapsedTimeMillisLowerBound = beforeCallTime - requestStartTime.toEpochMilli();
    long expectedElapsedTimeMillisUpperBound = afterCallTime - requestStartTime.toEpochMilli();
    ArgumentCaptor<Long> elapsedTimeMillisArgCaptor = ArgumentCaptor.forClass(Long.class);
    verify(endpointMetricsHandlerMock).handleRequest(eq(requestInfoMock), eq(responseInfoMock), eq(state), eq(responseStatusCode), eq(responseHttpStatusCodeXXValue), elapsedTimeMillisArgCaptor.capture());
    assertThat(elapsedTimeMillisArgCaptor.getValue()).isBetween(expectedElapsedTimeMillisLowerBound, expectedElapsedTimeMillisUpperBound);
}
Also used : Matchers.anyLong(org.mockito.Matchers.anyLong) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 53 with DataProvider

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

the class CodahaleMetricsListenerTest method defaultMetricNamingStrategy_works_as_expected.

@DataProvider(value = { // Underscore word delimiters on the next two
"null           |   _               |   foo_bar                             |   single", "somePrefix     |   _               |   somePrefix.foo_bar                  |   somePrefix.single", // Word delimiter on the next line is a dash '-', not an underscore '_'
"null           |   -               |   foo-bar                             |   single", "other.Prefix   |   weirddelimiter  |   other.Prefix.fooweirddelimiterbar   |   other.Prefix.single", "               |                   |   foobar                              |   single", "null           |   null            |   foobar                              |   single", "yetMorePrefix  |   null            |   yetMorePrefix.foobar                |   yetMorePrefix.single" }, splitBy = "\\|")
@Test
public void defaultMetricNamingStrategy_works_as_expected(String prefix, String wordDelimiter, String expectedFooBarName, String expectedSingleName) {
    // given
    DefaultMetricNamingStrategy<FooMetricName> strat = new DefaultMetricNamingStrategy<>(prefix, wordDelimiter);
    // when
    String actualFooBarName = strat.nameFor(FooMetricName.FOO_BAR);
    String actualSingleName = strat.nameFor(FooMetricName.SINGLE);
    // then
    assertThat(actualFooBarName).isEqualTo(expectedFooBarName);
    assertThat(actualSingleName).isEqualTo(expectedSingleName);
}
Also used : DefaultMetricNamingStrategy(com.nike.riposte.metrics.codahale.CodahaleMetricsListener.DefaultMetricNamingStrategy) Matchers.anyString(org.mockito.Matchers.anyString) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 54 with DataProvider

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

the class CodahaleMetricsListenerTest method onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint.

@DataProvider(value = { "GET    |   99", "GET    |   142", "GET    |   242", "GET    |   342", "GET    |   404", "GET    |   405", "GET    |   442", "GET    |   500", "GET    |   542", "GET    |   600", "POST   |   99", "POST   |   142", "POST   |   242", "POST   |   342", "POST   |   404", "POST   |   405", "POST   |   442", "POST   |   500", "POST   |   542", "POST   |   600", "PUT    |   99", "PUT    |   142", "PUT    |   242", "PUT    |   342", "PUT    |   404", "PUT    |   405", "PUT    |   442", "PUT    |   500", "PUT    |   542", "PUT    |   600", "DELETE |   99", "DELETE |   142", "DELETE |   242", "DELETE |   342", "DELETE |   404", "DELETE |   405", "DELETE |   442", "DELETE |   500", "DELETE |   542", "DELETE |   600", "PATCH  |   99", "PATCH  |   142", "PATCH  |   242", "PATCH  |   342", "PATCH  |   404", "PATCH  |   405", "PATCH  |   442", "PATCH  |   500", "PATCH  |   542", "PATCH  |   600", "null   |   99", "null   |   142", "null   |   242", "null   |   342", "null   |   404", "null   |   405", "null   |   442", "null   |   500", "null   |   542", "null   |   600" }, splitBy = "\\|")
@Test
public void onEvent_works_as_expected_for_RESPONSE_SENT_with_endpoint(String requestMethodStr, int responseStatusCode) throws InterruptedException {
    // given
    ServerMetricsEvent event = ServerMetricsEvent.RESPONSE_SENT;
    HttpMethod requestMethod = (requestMethodStr == null) ? null : HttpMethod.valueOf(requestMethodStr);
    doReturn(requestMethod).when(requestInfoMock).getMethod();
    Endpoint<?> endpoint = serverConfig.appEndpoints().iterator().next();
    String matchingPathTemplate = "/" + UUID.randomUUID().toString();
    state.setEndpointForExecution(endpoint, matchingPathTemplate);
    doReturn(responseStatusCode).when(responseInfoMock).getHttpStatusCodeWithDefault(ResponseSender.DEFAULT_HTTP_STATUS_CODE);
    int requestRawContentLengthBytes = (int) (Math.random() * 10000);
    doReturn(requestRawContentLengthBytes).when(requestInfoMock).getRawContentLengthInBytes();
    long finalResponseContentLength = (long) (Math.random() * 10000);
    doReturn(finalResponseContentLength).when(responseInfoMock).getFinalContentLength();
    Thread.sleep((long) (Math.random() * 25));
    // when
    long beforeCallTime = System.currentTimeMillis();
    listener.onEvent(event, state);
    long afterCallTime = System.currentTimeMillis();
    // then
    // Inflight requests counter decremented
    verify(listener.inflightRequests).dec();
    // Processed requests counter incremented
    verify(listener.processedRequests).inc();
    // If response code is greater than or equal to 400, then the failed requests counter should be incremented.
    if (responseStatusCode >= 400)
        verify(listener.failedRequests).inc();
    // Request and response size histograms should be updated with the relevant values from the request and response.
    verify(listener.requestSizes).update(requestRawContentLengthBytes);
    verify(listener.responseSizes).update(finalResponseContentLength);
    // The EndpointMetricsHandler should have been notified
    int responseHttpStatusCodeXXValue = responseStatusCode / 100;
    long expectedElapsedTimeMillisLowerBound = beforeCallTime - requestStartTime.toEpochMilli();
    long expectedElapsedTimeMillisUpperBound = afterCallTime - requestStartTime.toEpochMilli();
    ArgumentCaptor<Long> elapsedTimeMillisArgCaptor = ArgumentCaptor.forClass(Long.class);
    verify(endpointMetricsHandlerMock).handleRequest(eq(requestInfoMock), eq(responseInfoMock), eq(state), eq(responseStatusCode), eq(responseHttpStatusCodeXXValue), elapsedTimeMillisArgCaptor.capture());
    assertThat(elapsedTimeMillisArgCaptor.getValue()).isBetween(expectedElapsedTimeMillisLowerBound, expectedElapsedTimeMillisUpperBound);
}
Also used : Matchers.anyLong(org.mockito.Matchers.anyLong) ServerMetricsEvent(com.nike.riposte.server.metrics.ServerMetricsEvent) Matchers.anyString(org.mockito.Matchers.anyString) HttpMethod(io.netty.handler.codec.http.HttpMethod) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 55 with DataProvider

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

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getActiveInstanceInfoForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfoForVipAddressBlocking(boolean useExecutor) {
    // given
    InstanceInfo iiMock = mock(InstanceInfo.class);
    doReturn(Optional.of(iiMock)).when(serviceSpy).getActiveInstanceInfoForVipAddressBlocking(vip);
    Optional<Executor> executorOpt = useExecutor ? Optional.of(Executors.newSingleThreadExecutor()) : Optional.empty();
    // when
    CompletableFuture<Optional<InstanceInfo>> result = serviceSpy.getActiveInstanceInfoForVipAddress(vip, executorOpt);
    // then
    assertThat(result.join()).isEqualTo(Optional.of(iiMock));
}
Also used : Executor(java.util.concurrent.Executor) Optional(java.util.Optional) InstanceInfo(com.netflix.appinfo.InstanceInfo) 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