Search in sources :

Example 21 with FilterRequestContext

use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.

the class TestRestLiCallback method testOnErrorWithFiltersExceptionFromSecondFilter.

@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersExceptionFromSecondFilter() throws Exception {
    // App stuff.
    RestLiServiceException exFromApp = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "App failure");
    RestLiResponseData<CreateResponseEnvelope> responseAppData = ResponseDataBuilderUtil.buildCreateResponseData(exFromApp);
    // Filter stuff.
    final Exception exFromSecondFilter = new RuntimeException("Runtime exception from second filter");
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, exFromSecondFilter);
    RestLiResponseData<CreateResponseEnvelope> responseFilterData = ResponseDataBuilderUtil.buildCreateResponseData(exception);
    RestLiResponse partialResponse = new RestLiResponse.Builder().build();
    // Setup.
    when(_responseHandler.buildExceptionResponseData(eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseAppData).thenReturn(responseFilterData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseAppData)).thenReturn(partialResponse);
    when(_restRequest.getHeaders()).thenReturn(null);
    // Mock filter behavior.
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Throwable t = (Throwable) args[0];
            FilterRequestContext requestContext = (FilterRequestContext) args[1];
            FilterResponseContext responseContext = (FilterResponseContext) args[2];
            RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
            assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_404_NOT_FOUND);
            assertNull(responseData.getResponseEnvelope().getRecord());
            assertTrue(responseData.getHeaders().isEmpty());
            // Modify data.
            setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
            return completedFutureWithError(t);
        }
    }).doThrow(exFromSecondFilter).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
    // invoke request filters so cursor is in correct place
    when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
    _twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
    // Invoke.
    _twoFilterRestLiCallback.onError(exFromApp);
    // Verify.
    ArgumentCaptor<RestLiServiceException> wrappedExCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), wrappedExCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseAppData);
    ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
    verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
    verify(_restRequest).getHeaders();
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    assertNotNull(responseAppData);
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, responseAppData.getResponseEnvelope().getStatus());
    assertNull(responseAppData.getResponseEnvelope().getRecord());
    final RestLiServiceException restliEx1 = wrappedExCapture.getAllValues().get(0);
    assertEquals(exFromApp, restliEx1);
    RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
    assertEquals(restLiResponseException.getRestLiResponse(), partialResponse);
    assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
    RestLiServiceException restliEx2 = (RestLiServiceException) restLiResponseException.getCause();
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx2.getStatus());
    assertEquals(exFromSecondFilter.getMessage(), restliEx2.getMessage());
    assertEquals(exFromSecondFilter, restliEx2.getCause());
}
Also used : RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestException(com.linkedin.r2.message.rest.RestException) Answer(org.mockito.stubbing.Answer) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)21 Test (org.testng.annotations.Test)17 BeforeTest (org.testng.annotations.BeforeTest)16 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)14 RoutingException (com.linkedin.restli.server.RoutingException)12 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)12 RestException (com.linkedin.r2.message.rest.RestException)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)9 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 Answer (org.mockito.stubbing.Answer)6 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)4 DataMap (com.linkedin.data.DataMap)3 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)3 FilterChainDispatcher (com.linkedin.restli.internal.server.filter.FilterChainDispatcher)3 RestLiFilterChain (com.linkedin.restli.internal.server.filter.RestLiFilterChain)3 RestLiFilterResponseContextFactory (com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory)3 RestLiArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder)3 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)3 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)3