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());
}
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());
}
}
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);
}
Aggregations