Search in sources :

Example 21 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData 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

RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)21 Test (org.testng.annotations.Test)20 BeforeTest (org.testng.annotations.BeforeTest)14 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)11 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)11 RestException (com.linkedin.r2.message.rest.RestException)10 RoutingException (com.linkedin.restli.server.RoutingException)10 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)7 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)4 Answer (org.mockito.stubbing.Answer)4 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 DataMap (com.linkedin.data.DataMap)2 Foo (com.linkedin.pegasus.generator.examples.Foo)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 BatchResponse (com.linkedin.restli.common.BatchResponse)2