Search in sources :

Example 6 with ErrorResponseBody

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));
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) ErrorResponseInfo(com.nike.riposte.server.error.handler.ErrorResponseInfo) FullResponseInfo(com.nike.riposte.server.http.impl.FullResponseInfo) ErrorResponseInfo(com.nike.riposte.server.error.handler.ErrorResponseInfo) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) IncompleteHttpCallTimeoutException(com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException) TooManyOpenChannelsException(com.nike.riposte.server.error.exception.TooManyOpenChannelsException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) Test(org.junit.Test)

Example 7 with ErrorResponseBody

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();
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) Matchers.anyObject(org.mockito.Matchers.anyObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 8 with ErrorResponseBody

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));
}
Also used : ErrorResponseBodyImpl(com.nike.backstopper.model.riposte.ErrorResponseBodyImpl) DefaultErrorContractDTO(com.nike.backstopper.model.DefaultErrorContractDTO) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) Test(org.junit.Test)

Example 9 with ErrorResponseBody

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));
}
Also used : ErrorResponseInfo(com.nike.backstopper.handler.ErrorResponseInfo) RequestInfoForLogging(com.nike.backstopper.handler.RequestInfoForLogging) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Example 10 with ErrorResponseBody

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));
}
Also used : RequestInfoForLogging(com.nike.backstopper.handler.RequestInfoForLogging) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) RequestInfo(com.nike.riposte.server.http.RequestInfo) ErrorResponseInfo(com.nike.backstopper.handler.ErrorResponseInfo) List(java.util.List) ErrorResponseInfoImpl(com.nike.backstopper.model.riposte.ErrorResponseInfoImpl) Test(org.junit.Test)

Aggregations

ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)24 Test (org.junit.Test)18 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 IncompleteHttpCallTimeoutException (com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException)6 TooManyOpenChannelsException (com.nike.riposte.server.error.exception.TooManyOpenChannelsException)6 ErrorResponseInfo (com.nike.riposte.server.error.handler.ErrorResponseInfo)6 FullResponseInfo (com.nike.riposte.server.http.impl.FullResponseInfo)6 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)5 List (java.util.List)5 ErrorResponseInfo (com.nike.backstopper.handler.ErrorResponseInfo)4 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)4 RequestInfoForLogging (com.nike.backstopper.handler.RequestInfoForLogging)3 DefaultErrorContractDTO (com.nike.backstopper.model.DefaultErrorContractDTO)2 ErrorResponseBodyImpl (com.nike.backstopper.model.riposte.ErrorResponseBodyImpl)2 ErrorResponseInfoImpl (com.nike.backstopper.model.riposte.ErrorResponseInfoImpl)2 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)2 RiposteErrorHandler (com.nike.riposte.server.error.handler.RiposteErrorHandler)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2