Search in sources :

Example 76 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 77 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)

Example 78 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 79 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 80 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)

Aggregations

DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)88 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 HashMap (java.util.HashMap)7 Map (java.util.Map)7 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