use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method processError_gets_requestInfo_then_calls_riposteErrorHandler_then_converts_to_response_using_setupResponseInfoBasedOnErrorResponseInfo.
@Test
public void processError_gets_requestInfo_then_calls_riposteErrorHandler_then_converts_to_response_using_setupResponseInfoBasedOnErrorResponseInfo() throws UnexpectedMajorErrorHandlingError, JsonProcessingException {
// given
HttpProcessingState stateMock = mock(HttpProcessingState.class);
Object msg = new Object();
Throwable cause = new Exception();
ExceptionHandlingHandler handlerSpy = spy(handler);
RequestInfo<?> requestInfoMock = mock(RequestInfo.class);
ErrorResponseInfo errorResponseInfoMock = mock(ErrorResponseInfo.class);
RiposteErrorHandler riposteErrorHandlerMock = mock(RiposteErrorHandler.class);
Whitebox.setInternalState(handlerSpy, "riposteErrorHandler", riposteErrorHandlerMock);
doReturn(requestInfoMock).when(handlerSpy).getRequestInfo(stateMock, msg);
doReturn(errorResponseInfoMock).when(riposteErrorHandlerMock).maybeHandleError(cause, requestInfoMock);
// when
ResponseInfo<ErrorResponseBody> response = handlerSpy.processError(stateMock, msg, cause);
// then
verify(handlerSpy).getRequestInfo(stateMock, msg);
verify(riposteErrorHandlerMock).maybeHandleError(cause, requestInfoMock);
ArgumentCaptor<ResponseInfo> responseInfoArgumentCaptor = ArgumentCaptor.forClass(ResponseInfo.class);
verify(handlerSpy).setupResponseInfoBasedOnErrorResponseInfo(responseInfoArgumentCaptor.capture(), eq(errorResponseInfoMock));
ResponseInfo<ErrorResponseBody> responseInfoPassedIntoSetupMethod = responseInfoArgumentCaptor.getValue();
assertThat(response, is(responseInfoPassedIntoSetupMethod));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ChannelPipelineFinalizerHandlerTest method finalizeChannelPipeline_should_send_error_response_if_state_indicates_no_response_already_sent.
@Test
public void finalizeChannelPipeline_should_send_error_response_if_state_indicates_no_response_already_sent() throws JsonProcessingException {
// given
state.setResponseWriterFinalChunkChannelFuture(null);
HttpProcessingState stateSpy = spy(state);
doReturn(stateSpy).when(stateAttributeMock).get();
doReturn(false).when(responseInfoMock).isResponseSendingStarted();
Object msg = new Object();
Throwable cause = new Exception("intentional test exception");
RequestInfo<?> requestInfoMock = mock(RequestInfo.class);
ResponseInfo<ErrorResponseBody> errorResponseMock = mock(ResponseInfo.class);
doReturn(requestInfoMock).when(exceptionHandlingHandlerMock).getRequestInfo(stateSpy, msg);
doReturn(errorResponseMock).when(exceptionHandlingHandlerMock).processUnhandledError(eq(stateSpy), eq(msg), any(Throwable.class));
// when
handler.finalizeChannelPipeline(ctxMock, msg, stateSpy, cause);
// then
verify(responseSenderMock).sendErrorResponse(ctxMock, requestInfoMock, errorResponseMock);
verify(metricsListenerMock).onEvent(ServerMetricsEvent.RESPONSE_SENT, stateSpy);
verify(ctxMock).flush();
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class RiposteUnhandledExceptionHandlerTest method prepareFrameworkResponseUsesErrorResponseBodyNettyAdapterWrapper.
@Test
public void prepareFrameworkResponseUsesErrorResponseBodyNettyAdapterWrapper() {
RiposteUnhandledExceptionHandler myAdapter = new RiposteUnhandledExceptionHandler(projectApiErrors, utils);
DefaultErrorContractDTO errorContract = new DefaultErrorContractDTO(UUID.randomUUID().toString(), Arrays.asList(projectApiErrors.getUnauthorizedApiError(), projectApiErrors.getMalformedRequestApiError()));
ErrorResponseBody result = myAdapter.prepareFrameworkRepresentation(errorContract, 42, null, null, null);
assertThat(result, instanceOf(ErrorResponseBodyImpl.class));
ErrorResponseBodyImpl adapterResult = (ErrorResponseBodyImpl) result;
assertThat(adapterResult.error_id, is(errorContract.error_id));
assertThat(adapterResult.errors, is(errorContract.errors));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class RiposteUnhandledExceptionHandlerTest method handleErrorFromNettyInterfaceWrapsRequestInfoWithAdapterBeforeContinuing.
@Test
public void handleErrorFromNettyInterfaceWrapsRequestInfoWithAdapterBeforeContinuing() {
com.nike.backstopper.handler.ErrorResponseInfo<ErrorResponseBody> backstopperResponse = new ErrorResponseInfo<>(42, mock(ErrorResponseBody.class), Collections.emptyMap());
doReturn(backstopperResponse).when(adapterSpy).handleException(any(Throwable.class), any(RequestInfoForLogging.class));
RequestInfo requestInfoMock = mock(RequestInfo.class);
adapterSpy.handleError(new Exception(), requestInfoMock);
ArgumentCaptor<RequestInfoForLogging> requestInfoForLoggingArgumentCaptor = ArgumentCaptor.forClass(RequestInfoForLogging.class);
verify(adapterSpy).handleException(any(Throwable.class), requestInfoForLoggingArgumentCaptor.capture());
RequestInfoForLogging passedArg = requestInfoForLoggingArgumentCaptor.getValue();
assertThat(passedArg, instanceOf(RequestInfoForLoggingRiposteAdapter.class));
RequestInfo embeddedRequestInfoInWrapper = (RequestInfo) Whitebox.getInternalState(passedArg, "request");
assertThat(embeddedRequestInfoInWrapper, sameInstance(requestInfoMock));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class RiposteApiExceptionHandlerTest method maybeHandleErrorFromNettyInterfaceReturnsWrapperAroundBackstopperMaybeHandleExceptionReturnValue.
@Test
public void maybeHandleErrorFromNettyInterfaceReturnsWrapperAroundBackstopperMaybeHandleExceptionReturnValue() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError {
ErrorResponseBody errorResponseBodyMock = mock(ErrorResponseBody.class);
Map<String, List<String>> headersMap = MapBuilder.<String, List<String>>builder().put("headerName", Arrays.asList("hval1", "hval2")).build();
com.nike.backstopper.handler.ErrorResponseInfo<ErrorResponseBody> backstopperResponse = new ErrorResponseInfo<>(42, errorResponseBodyMock, headersMap);
doReturn(backstopperResponse).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class));
com.nike.riposte.server.error.handler.ErrorResponseInfo riposteErrorResponseInfo = adapterSpy.maybeHandleError(new Exception(), mock(RequestInfo.class));
assertThat(riposteErrorResponseInfo, instanceOf(ErrorResponseInfoImpl.class));
assertThat(riposteErrorResponseInfo.getErrorHttpStatusCode(), is(backstopperResponse.httpStatusCode));
assertThat(riposteErrorResponseInfo.getErrorResponseBody(), is(errorResponseBodyMock));
assertThat(riposteErrorResponseInfo.getExtraHeadersToAddToResponse(), is(headersMap));
}
Aggregations