use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.
the class RequestFilterHandlerTest method handleFilterLogic_short_circuits_as_expected_if_filter_returns_valid_response.
@DataProvider(value = { "true | 0 | true", "true | 0 | false", "true | 1 | true", "true | 1 | false", "false | 0 | true", "false | 0 | false", "false | 1 | true", "false | 1 | false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_short_circuits_as_expected_if_filter_returns_valid_response(boolean isFirstChunk, int shortCircuitingFilterIndex, boolean filterReturnsModifiedRequestInfo) {
// given
HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
RequestAndResponseFilter shortCircuitingFilter = filtersList.get(shortCircuitingFilterIndex);
doReturn(true).when(shortCircuitingFilter).isShortCircuitRequestFilter();
RequestInfo<?> modifiedRequestInfoMock = mock(RequestInfo.class);
RequestInfo<?> returnedRequestInfo = (filterReturnsModifiedRequestInfo) ? modifiedRequestInfoMock : null;
ResponseInfo<?> returnedResponseInfoMock = mock(ResponseInfo.class);
doReturn(Pair.of(returnedRequestInfo, Optional.of(returnedResponseInfoMock))).when(shortCircuitingFilter).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
doReturn(Pair.of(returnedRequestInfo, Optional.of(returnedResponseInfoMock))).when(shortCircuitingFilter).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
// when
PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.normalFilterCall, args.shortCircuitFilterCall);
// then
// Pipeline stops for the given msg event.
assertThat(result).isEqualTo(DO_NOT_FIRE_CONTINUE_EVENT);
// The filter's short-circuit-capable method was called.
if (isFirstChunk)
verify(shortCircuitingFilter).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
else
verify(shortCircuitingFilter).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
// The state is updated with the correct RequestInfo depending on what the filter returned.
if (filterReturnsModifiedRequestInfo)
assertThat(state.getRequestInfo()).isSameAs(modifiedRequestInfoMock);
else
assertThat(state.getRequestInfo()).isSameAs(requestInfoMock);
// The state is updated with the ResponseInfo returned by the filter.
assertThat(state.getResponseInfo()).isSameAs(returnedResponseInfoMock);
// The short circuiting "we're all done, return the response to the caller" event is fired down the pipeline.
verify(ctxMock).fireChannelRead(LastOutboundMessageSendFullResponseInfo.INSTANCE);
}
use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.
the class RequestFilterHandlerTest method handleFilterLogic_executes_all_filters_and_uses_original_request_when_filters_are_short_circuiting_and_return_null.
@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_executes_all_filters_and_uses_original_request_when_filters_are_short_circuiting_and_return_null(boolean isFirstChunk) {
// given
HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
filtersList.forEach(filter -> doReturn(true).when(filter).isShortCircuitRequestFilter());
// when
PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.normalFilterCall, args.shortCircuitFilterCall);
// then
assertThat(result).isEqualTo(CONTINUE);
filtersList.forEach(filter -> {
if (isFirstChunk)
verify(filter).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
else
verify(filter).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
});
assertThat(state.getRequestInfo()).isSameAs(requestInfoMock);
}
use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.
the class RequestFilterHandlerTest method handleFilterLogic_executes_all_filters_and_uses_requestInfo_returned_by_filters_when_filters_are_mixed_type_and_do_not_short_circuit.
@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_executes_all_filters_and_uses_requestInfo_returned_by_filters_when_filters_are_mixed_type_and_do_not_short_circuit(boolean isFirstChunk) {
// given
HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
doReturn(true).when(filtersList.get(0)).isShortCircuitRequestFilter();
doReturn(false).when(filtersList.get(1)).isShortCircuitRequestFilter();
RequestInfo<?> firstFilterResult = mock(RequestInfo.class);
RequestInfo<?> secondFilterResult = mock(RequestInfo.class);
// Do a mix of empty Optional vs null for the response to hit branch coverage (both indicate no short circuiting response)
doReturn(Pair.of(firstFilterResult, Optional.empty())).when(filter1Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
doReturn(Pair.of(firstFilterResult, null)).when(filter1Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
doReturn(secondFilterResult).when(filter2Mock).filterRequestFirstChunkNoPayload(any(), any());
doReturn(secondFilterResult).when(filter2Mock).filterRequestLastChunkWithFullPayload(any(), any());
// when
PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.normalFilterCall, args.shortCircuitFilterCall);
// then
assertThat(result).isEqualTo(CONTINUE);
// First filter should have been passed the original request.
if (isFirstChunk)
verify(filter1Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
else
verify(filter1Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
// Second filter should have been passed the result of the first filter.
if (isFirstChunk)
verify(filter2Mock).filterRequestFirstChunkNoPayload(firstFilterResult, ctxMock);
else
verify(filter2Mock).filterRequestLastChunkWithFullPayload(firstFilterResult, ctxMock);
// The state should have been updated with the result of the second filter.
assertThat(state.getRequestInfo()).isSameAs(secondFilterResult);
}
use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.
the class RequestInfoSetterHandlerTest method doChannelRead_override_uses_global_configured_max_request_size_when_necessary_no_error.
@DataProvider(value = { "true | false", "false | true" }, splitBy = "\\|")
@Test
public void doChannelRead_override_uses_global_configured_max_request_size_when_necessary_no_error(boolean endpointIsNull, boolean endpointReturnsNullOverride) {
// given
if (endpointIsNull)
doReturn(null).when(stateMock).getEndpointForExecution();
if (endpointReturnsNullOverride)
doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride();
maxRequestSizeInBytes = 10;
handler = new RequestInfoSetterHandler(maxRequestSizeInBytes);
doReturn(5).when(requestInfo).addContentChunk(anyObject());
// when
PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, httpContentMock);
// then
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
verify(httpContentMock).release();
}
use of com.tngtech.java.junit.dataprovider.DataProvider in project riposte by Nike-Inc.
the class RequestFilterHandlerTest method doChannelRead_delegates_to_handleFilterLogic_with_first_chunk_method_references_when_msg_is_HttpRequest.
@DataProvider(value = { "CONTINUE", "DO_NOT_FIRE_CONTINUE_EVENT" }, splitBy = "\\|")
@Test
public void doChannelRead_delegates_to_handleFilterLogic_with_first_chunk_method_references_when_msg_is_HttpRequest(PipelineContinuationBehavior expectedPipelineContinuationBehavior) throws Exception {
// given
doReturn(expectedPipelineContinuationBehavior).when(handlerSpy).handleFilterLogic(any(), any(), any(), any());
// when
PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, firstChunkMsgMock);
// then
assertThat(result).isEqualTo(expectedPipelineContinuationBehavior);
ArgumentCaptor<BiFunction> normalFilterCallCaptor = ArgumentCaptor.forClass(BiFunction.class);
ArgumentCaptor<BiFunction> shortCircuitFilterCallCaptor = ArgumentCaptor.forClass(BiFunction.class);
verify(handlerSpy).handleFilterLogic(eq(ctxMock), eq(firstChunkMsgMock), normalFilterCallCaptor.capture(), shortCircuitFilterCallCaptor.capture());
BiFunction<RequestAndResponseFilter, RequestInfo, RequestInfo> normalFilterCall = normalFilterCallCaptor.getValue();
BiFunction<RequestAndResponseFilter, RequestInfo, Pair<RequestInfo, Optional<ResponseInfo<?>>>> shortCircuitFilterCall = shortCircuitFilterCallCaptor.getValue();
RequestAndResponseFilter filterForNormalCallMock = mock(RequestAndResponseFilter.class);
normalFilterCall.apply(filterForNormalCallMock, requestInfoMock);
verify(filterForNormalCallMock).filterRequestFirstChunkNoPayload(requestInfoMock, ctxMock);
RequestAndResponseFilter filterForShortCircuitCallMock = mock(RequestAndResponseFilter.class);
shortCircuitFilterCall.apply(filterForShortCircuitCallMock, requestInfoMock);
verify(filterForShortCircuitCallMock).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
}
Aggregations