use of com.linkedin.restli.server.RequestExecutionReportBuilder in project rest.li by linkedin.
the class TestRestLiMethodInvocation method checkInvocation.
private void checkInvocation(Object resource, ResourceMethodDescriptor resourceMethodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final RequestExecutionCallback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws URISyntaxException, RestLiSyntaxException {
assertNotNull(resource);
assertNotNull(resourceMethodDescriptor);
try {
EasyMock.replay(resource);
RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/json").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
if (entityBody != null) {
builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
}
RestRequest request = builder.build();
final ResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, new RequestContext(), true, expectedRequestAttachments);
resourceContext.setResponseAttachments(expectedResponseAttachments);
RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor);
FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), resourceMethodDescriptor);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(resourceMethodDescriptor.getType());
RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
filterContext.setRequestData(requestData);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
RequestExecutionReportBuilder requestExecutionReportBuilder = null;
if (isDebugMode) {
requestExecutionReportBuilder = new RequestExecutionReportBuilder();
}
RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
if (isDebugMode) {
Assert.assertNotNull(executionReport);
} else {
Assert.assertNull(executionReport);
}
if (e.getCause().getCause() instanceof RoutingException) {
expectedRoutingExceptionLatch.countDown();
}
if (callback != null) {
callback.onError(e, executionReport, null, null);
}
Assert.assertEquals(requestAttachmentReader, expectedRequestAttachments);
Assert.assertEquals(responseAttachments, expectedResponseAttachments);
latch.countDown();
}
@Override
public void onSuccess(final RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
if (isDebugMode) {
Assert.assertNotNull(executionReport);
} else {
Assert.assertNull(executionReport);
}
if (callback != null) {
callback.onSuccess(result, executionReport, null);
}
Assert.assertEquals(responseAttachments, expectedResponseAttachments);
latch.countDown();
}
};
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, _invoker, adapter, requestExecutionReportBuilder, expectedRequestAttachments, restLiResponseHandler, executionCallback);
final RestLiCallback<Object> outerCallback = new RestLiCallback<Object>(filterContext, new RestLiFilterResponseContextFactory<Object>(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
_invoker.invoke(filterContext.getRequestData(), routingResult, adapter, outerCallback, requestExecutionReportBuilder);
try {
latch.await();
if (expectRoutingException) {
expectedRoutingExceptionLatch.await();
}
} catch (InterruptedException e) {
// Ignore
}
EasyMock.verify(resource);
Assert.assertEquals(((ServerResourceContext) routingResult.getContext()).getResponseMimeType(), "application/json");
} catch (RestLiSyntaxException e) {
throw new RoutingException("syntax exception", 400);
} finally {
EasyMock.reset(resource);
EasyMock.makeThreadSafe(resource, true);
}
}
use of com.linkedin.restli.server.RequestExecutionReportBuilder in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersNotHandlingAppEx.
@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersNotHandlingAppEx() throws Exception {
Exception exFromApp = new RuntimeException("Runtime exception from app");
RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Output");
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(appException, headersFromApp, Collections.<HttpCookie>emptyList());
responseData.setResponseEnvelope(new CreateResponseEnvelope(new EmptyRecord(), responseData));
PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseData);
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
// Mock the behavior of the first filter.
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];
// Verify incoming data.
assertEquals(HttpStatus.S_404_NOT_FOUND, responseContext.getResponseData().getStatus());
assertEquals(headersFromApp, responseContext.getResponseData().getHeaders());
assertNull(responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseContext.getResponseData().getHeaders().clear();
return completedFutureWithError(responseContext.getResponseData().getServiceException());
}
}).doAnswer(new Answer<Object>() {
// Mock the behavior of the second filter.
@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];
// Verify incoming data.
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseContext.getResponseData().getStatus());
//assertTrue(responseContext.getResponseData().getHeaders().isEmpty());
assertNull(responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseContext.getResponseData().getHeaders().putAll(headersFromFilter);
return completedFutureWithError(responseContext.getResponseData().getServiceException());
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
RestException restException = new RestException(new RestResponseBuilder().build());
when(_responseHandler.buildRestException(any(RestLiServiceException.class), eq(partialResponse))).thenReturn(restException);
// 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.
assertNotNull(responseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getStatus());
assertNull(responseData.getRecordResponseEnvelope().getRecord());
assertTrue(responseData.isErrorResponse());
assertEquals(responseData.getServiceException().getErrorDetails(), appException.getErrorDetails());
assertEquals(responseData.getServiceException().getOverridingFormat(), appException.getOverridingFormat());
assertEquals(responseData.getServiceException().getServiceErrorCode(), appException.getServiceErrorCode());
assertEquals(responseData.getServiceException().getMessage(), appException.getMessage());
Map<String, String> expectedHeaders = buildErrorHeaders();
expectedHeaders.put("Key", "Output");
assertEquals(expectedHeaders, responseData.getHeaders());
ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
verify(_responseHandler, times(1)).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_responseHandler).buildRestException(exCapture.capture(), eq(partialResponse));
verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
verify(_restRequest, times(1)).getHeaders();
verifyZeroInteractions(_routingResult);
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
final RestLiServiceException restliEx1 = exCapture.getAllValues().get(0);
assertNotNull(restliEx1);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx1.getStatus());
assertEquals(exFromApp.getMessage(), restliEx1.getMessage());
assertEquals(exFromApp, restliEx1.getCause());
final RestLiServiceException restliEx2 = exCapture.getAllValues().get(1);
assertNotNull(restliEx2);
assertEquals(HttpStatus.S_403_FORBIDDEN, restliEx2.getStatus());
}
use of com.linkedin.restli.server.RequestExecutionReportBuilder 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.RequestExecutionReportBuilder 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.RequestExecutionReportBuilder 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