Search in sources :

Example 21 with RestLiResponseAttachments

use of com.linkedin.restli.server.RestLiResponseAttachments 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");
    RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
    RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
    RestLiResponseDataImpl responseAppData = new RestLiResponseDataImpl(exFromApp, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    responseAppData.setResponseEnvelope(new CreateResponseEnvelope(null, responseAppData));
    // Filter stuff.
    final Exception exFromSecondFilter = new RuntimeException("Runtime exception from second filter");
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, exFromSecondFilter);
    RestLiResponseDataImpl responseFilterData = new RestLiResponseDataImpl(exception, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    responseFilterData.setResponseEnvelope(new CreateResponseEnvelope(null, responseFilterData));
    PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
    RestException restException = new RestException(new RestResponseBuilder().build());
    // Setup.
    when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
    when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseAppData).thenReturn(responseFilterData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseAppData)).thenReturn(partialResponse);
    when(_restRequest.getHeaders()).thenReturn(null);
    when(_responseHandler.buildRestException(any(RestLiServiceException.class), eq(partialResponse))).thenReturn(restException);
    // 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];
            assertEquals(responseContext.getResponseData().getStatus(), HttpStatus.S_404_NOT_FOUND);
            assertNull(responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
            assertTrue(responseContext.getResponseData().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, executionReport, _requestAttachmentReader, responseAttachments);
    // Verify.
    ArgumentCaptor<RestLiServiceException> wrappedExCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), wrappedExCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseAppData);
    verify(_responseHandler).buildRestException(wrappedExCapture.capture(), eq(partialResponse));
    verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
    verify(_restRequest).getHeaders();
    verifyZeroInteractions(_routingResult);
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    assertNotNull(responseAppData);
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, responseAppData.getStatus());
    assertNull(responseAppData.getRecordResponseEnvelope().getRecord());
    final RestLiServiceException restliEx1 = wrappedExCapture.getAllValues().get(0);
    assertEquals(exFromApp, restliEx1);
    final RestLiServiceException restliEx2 = wrappedExCapture.getAllValues().get(1);
    assertNotNull(restliEx2);
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx2.getStatus());
    assertEquals(exFromSecondFilter.getMessage(), restliEx2.getMessage());
    assertEquals(exFromSecondFilter, restliEx2.getCause());
}
Also used : RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestException(com.linkedin.r2.message.rest.RestException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) 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) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 22 with RestLiResponseAttachments

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

the class TestRestLiCallback method testOnErrorOtherExceptionNoFilters.

@SuppressWarnings("unchecked")
@Test(dataProvider = "provideExceptions")
public void testOnErrorOtherExceptionNoFilters(Exception ex) throws Exception {
    ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
    RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
    PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
    RestLiServiceException wrappedEx = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, ex);
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(wrappedEx, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    responseData.setResponseEnvelope(new GetResponseEnvelope(new EmptyRecord(), responseData));
    RestException restException = new RestException(new RestResponseBuilder().build());
    Map<String, String> inputHeaders = Maps.newHashMap();
    inputHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, "2.0.0");
    // Set up.
    when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
    when(_restRequest.getHeaders()).thenReturn(inputHeaders);
    when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList())).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    when(_responseHandler.buildRestException(wrappedEx, partialResponse)).thenReturn(restException);
    // Invoke.
    _noFilterRestLiCallback.onError(ex, executionReport, _requestAttachmentReader, responseAttachments);
    // Verify.
    verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestException(wrappedEx, partialResponse);
    verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
    verify(_restRequest, times(1)).getHeaders();
    verifyZeroInteractions(_routingResult);
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    RestLiServiceException restliEx = exCapture.getValue();
    assertNotNull(restliEx);
    if (ex instanceof RoutingException) {
        assertEquals(HttpStatus.fromCode(((RoutingException) ex).getStatus()), restliEx.getStatus());
    } else if (ex instanceof RestLiServiceException) {
        assertEquals(((RestLiServiceException) ex).getStatus(), restliEx.getStatus());
    } else {
        assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
    }
    assertEquals(ex.getMessage(), restliEx.getMessage());
    if (ex instanceof RestLiServiceException) {
        assertEquals(ex, restliEx);
    } else {
        assertEquals(ex, restliEx.getCause());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RoutingException(com.linkedin.restli.server.RoutingException) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 23 with RestLiResponseAttachments

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

the class TestRestLiCallback method testOnSuccessNoFilters.

@Test
public void testOnSuccessNoFilters() throws Exception {
    String result = "foo";
    RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
    RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
    RestLiResponseData responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
    RestResponse restResponse = new RestResponseBuilder().build();
    // Set up.
    when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
    when(_responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    when(_responseHandler.buildResponse(_routingResult, partialResponse)).thenReturn(restResponse);
    // Invoke.
    _noFilterRestLiCallback.onSuccess(result, executionReport, restLiResponseAttachments);
    // Verify.
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
    verify(_responseHandler).buildResponse(_routingResult, partialResponse);
    verify(_callback).onSuccess(restResponse, executionReport, restLiResponseAttachments);
    verifyZeroInteractions(_restRequest, _routingResult);
    verifyNoMoreInteractions(_responseHandler, _callback);
}
Also used : RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)23 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)20 Test (org.testng.annotations.Test)20 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)19 BeforeTest (org.testng.annotations.BeforeTest)19 RestException (com.linkedin.r2.message.rest.RestException)17 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)17 RoutingException (com.linkedin.restli.server.RoutingException)15 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)15 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)14 RestResponse (com.linkedin.r2.message.rest.RestResponse)10 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)10 EmptyRecord (com.linkedin.restli.common.EmptyRecord)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)7 RecordTemplate (com.linkedin.data.template.RecordTemplate)6 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)6 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)6 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)6 EasyMock.anyObject (org.easymock.EasyMock.anyObject)6