@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersExceptionFromSecondFilter() throws Exception
{
// App stuff.
RestLiServiceException exFromApp = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "App failure");
RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
AugmentedRestLiResponseData responseAppData =
new AugmentedRestLiResponseData.Builder(ResourceMethod.GET).status(HttpStatus.S_404_NOT_FOUND).build();
// Filter stuff.
final Exception exFromSecondFilter = new RuntimeException("Runtime exception from second filter");
AugmentedRestLiResponseData responseFilterData =
new AugmentedRestLiResponseData.Builder(ResourceMethod.GET).status(HttpStatus.S_500_INTERNAL_SERVER_ERROR).build();
PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
ArgumentCaptor<RestLiServiceException> wrappedExCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
RestException restException = new RestException(new RestResponseBuilder().build());
// Setup.
when(
_responseHandler.buildErrorResponseData(eq(_restRequest), eq(_routingResult),
wrappedExCapture.capture(), anyMap())).thenReturn(responseAppData)
.thenReturn(responseFilterData);
when(_responseHandler.buildPartialResponse(_routingResult, responseFilterData)).thenReturn(partialResponse);
when(_restRequest.getHeaders()).thenReturn(null);
when(_responseHandler.buildRestException(exFromSecondFilter, partialResponse)).thenReturn(restException);
// Mock filter behavior.
doAnswer(new Answer<Object>()
{
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
Object[] args = invocation.getArguments();
FilterResponseContext context = (FilterResponseContext) args[1];
assertEquals(context.getHttpStatus(), HttpStatus.S_404_NOT_FOUND);
assertNull(context.getResponseData().getEntityResponse());
assertTrue(context.getResponseHeaders().isEmpty());
// Modify data.
context.setHttpStatus(HttpStatus.S_402_PAYMENT_REQUIRED);
return null;
}
}).doThrow(exFromSecondFilter).when(_filter)
.onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
// Invoke.
_twoFilterRestLiCallback.onError(exFromApp, executionReport);
// Verify.
verify(_responseHandler, times(2)).buildErrorResponseData(eq(_restRequest), eq(_routingResult),
wrappedExCapture.capture(), anyMap());
verify(_responseHandler).buildPartialResponse(_routingResult, responseFilterData);
verify(_responseHandler).buildRestException(exFromSecondFilter, partialResponse);
verify(_callback).onError(restException, executionReport);
verify(_restRequest, times(2)).getHeaders();
verifyZeroInteractions(_routingResult);
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
assertNotNull(responseFilterData);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, responseFilterData.getStatus());
assertTrue(responseFilterData.getHeaders().isEmpty());
assertNull(responseFilterData.getEntityResponse());
RestLiServiceException restliEx = wrappedExCapture.getAllValues().get(0);
assertNotNull(restliEx);
assertEquals(exFromApp.getStatus(), restliEx.getStatus());
assertEquals(exFromApp.getMessage(), restliEx.getMessage());
restliEx = wrappedExCapture.getAllValues().get(1);
assertNotNull(restliEx);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
assertEquals(exFromSecondFilter.getMessage(), restliEx.getMessage());
assertEquals(exFromSecondFilter, restliEx.getCause());
}