/** * Returns a {@link RiposteErrorHandler} that uses the given {@link ProjectApiErrors}, and {@link * #defaultHandlerListeners(ProjectApiErrors, ApiExceptionHandlerUtils)} for the error handler listeners. */ public static RiposteErrorHandler defaultErrorHandler(ProjectApiErrors projectApiErrors, ApiExceptionHandlerUtils utils) { return new RiposteApiExceptionHandler( projectApiErrors, defaultHandlerListeners(projectApiErrors, utils), utils ); }
@Test public void maybeHandleErrorFromNettyInterfaceReturnsNullIfBackstopperMaybeHandleExceptionReturnsNull() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError { doReturn(null).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class)); RequestInfo requestInfoMock = mock(RequestInfo.class); assertThat(adapterSpy.maybeHandleError(new Exception(), requestInfoMock), nullValue()); }
@Test public void prepareFrameworkResponseUsesErrorResponseBodyNettyAdapterWrapper() { RiposteApiExceptionHandler myAdapter = new RiposteApiExceptionHandler(projectApiErrors, validListenerList, 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)); }
@Override public ErrorResponseInfo maybeHandleError(Throwable error, RequestInfo<?> requestInfo) throws UnexpectedMajorErrorHandlingError { try { com.nike.backstopper.handler.ErrorResponseInfo<ErrorResponseBody> backstopperErrorResponseInfo = maybeHandleException(error, new RequestInfoForLoggingRiposteAdapter(requestInfo)); if (backstopperErrorResponseInfo == null) return null; return new ErrorResponseInfoImpl(backstopperErrorResponseInfo); } catch (UnexpectedMajorExceptionHandlingError ex) { throw new UnexpectedMajorErrorHandlingError("Wrapping the actual cause", ex); } }
@Test(expected = UnexpectedMajorErrorHandlingError.class) public void maybeHandleErrorExplosionThrowsUnexpectedMajorErrorHandlingError() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError { UnexpectedMajorExceptionHandlingError innerExplosion = new UnexpectedMajorExceptionHandlingError("intentional kaboom", new Exception()); doThrow(innerExplosion).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class)); RequestInfo requestInfoMock = mock(RequestInfo.class); adapterSpy.maybeHandleError(new Exception(), requestInfoMock); } }
@Test(expected = IllegalArgumentException.class) public void constructorShouldBarfIfPassedNullProjectApiErrors() { new RiposteApiExceptionHandler(null, validListenerList, utils); }
@Test public void maybeHandleErrorFromNettyInterfaceWrapsRequestInfoWithAdapterBeforeContinuing() throws UnexpectedMajorExceptionHandlingError, UnexpectedMajorErrorHandlingError { doReturn(null).when(adapterSpy).maybeHandleException(any(Throwable.class), any(RequestInfoForLogging.class)); RequestInfo requestInfoMock = mock(RequestInfo.class); adapterSpy.maybeHandleError(new Exception(), requestInfoMock); ArgumentCaptor<RequestInfoForLogging> requestInfoForLoggingArgumentCaptor = ArgumentCaptor.forClass(RequestInfoForLogging.class); verify(adapterSpy).maybeHandleException(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)); }
@Before public void beforeMethod() { adapterSpy = spy(new RiposteApiExceptionHandler(projectApiErrors, validListenerList, utils)); }
@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)); }
@Test(expected = IllegalArgumentException.class) public void constructorShouldBarfIfPassedNullListenersList() { new RiposteApiExceptionHandler(projectApiErrors, null, utils); }
@Test(expected = IllegalArgumentException.class) public void constructorShouldBarfIfPassedNullApiExceptionHandlerUtils() { new RiposteApiExceptionHandler(projectApiErrors, validListenerList, null); }
@Test public void constructorWorksIfPassedValidValues() { // when RiposteApiExceptionHandler myAdapter = new RiposteApiExceptionHandler(projectApiErrors, validListenerList, utils); // then List<ApiExceptionHandlerListener> actualListeners = (List<ApiExceptionHandlerListener>) Whitebox.getInternalState(myAdapter, "apiExceptionHandlerListenerList"); ProjectApiErrors actualProjectApiErrors = (ProjectApiErrors) Whitebox.getInternalState(myAdapter, "projectApiErrors"); ApiExceptionHandlerUtils actualUtils = (ApiExceptionHandlerUtils) Whitebox.getInternalState(myAdapter, "utils"); assertThat(actualListeners, is(validListenerList)); assertThat(actualProjectApiErrors, is(projectApiErrors)); assertThat(actualUtils, is(utils)); }