use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandleEx.
@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandleEx() throws Exception {
// App stuff.
final RecordTemplate entityFromApp = Foo.createFoo("Key", "Two");
RestLiResponseData<CreateResponseEnvelope> appResponseData = ResponseDataBuilderUtil.buildCreateResponseData(HttpStatus.S_200_OK, entityFromApp);
// Filter stuff.
ArgumentCaptor<RestLiServiceException> exFromFilterCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Error from filter");
RestLiServiceException exceptionFromFilter = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
RestLiResponseData<?> responseErrorData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(exceptionFromFilter, false), headersFromFilter, Collections.emptyList());
RestLiResponse partialFilterErrorResponse = new RestLiResponse.Builder().build();
final Exception exFromFilter = new RuntimeException("Exception From Filter");
Map<String, String> errorHeaders = buildErrorHeaders();
// Common stuff.
RestException finalRestException = new RestException(new RestResponseBuilder().build());
// Setup.
when((RestLiResponseData<CreateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, entityFromApp)).thenReturn(appResponseData);
when(_restRequest.getHeaders()).thenReturn(null);
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exFromFilterCapture.capture(), anyMap(), anyList())).thenReturn(responseErrorData);
when(_responseHandler.buildPartialResponse(_routingResult, responseErrorData)).thenReturn(partialFilterErrorResponse);
// Mock filter behavior.
doThrow(exFromFilter).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
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];
// The second filter should be invoked with details of the exception thrown by the first
// filter.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertNull(responseData.getResponseEnvelope().getRecord());
assertEquals(responseData.getHeaders(), errorHeaders);
assertEquals(responseData.getResponseEnvelope().getException().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
return completedFutureWithError(responseData.getResponseEnvelope().getException());
}
}).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.onSuccess(entityFromApp);
// Verify.
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, entityFromApp);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verifyNoMoreInteractions(_responseHandler, _callback);
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
RestLiServiceException restliEx1 = (RestLiServiceException) restLiResponseException.getCause();
assertNotNull(restliEx1);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, restliEx1.getStatus());
// exceptions should not be equal because in new logic we are replacing the exception with new one
assertNotEquals(exFromFilter.getMessage(), restliEx1.getMessage());
assertNotEquals(exFromFilter, restliEx1.getCause());
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, appResponseData.getResponseEnvelope().getStatus());
assertEquals(appResponseData.getHeaders(), errorHeaders);
assertNull(appResponseData.getResponseEnvelope().getRecord());
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersSuccessful.
@Test
@SuppressWarnings("unchecked")
public void testOnSuccessWithFiltersSuccessful() throws Exception {
String result = "foo";
final RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
final RecordTemplate entityFromApp = Foo.createFoo("Key", "One");
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final RecordTemplate entityFromFilter1 = Foo.createFoo("Key", "Two");
final RecordTemplate entityFromFilter2 = Foo.createFoo("Key", "Three");
final Map<String, String> headersFromFilters = Maps.newHashMap();
headersFromFilters.put("Key", "Output");
RestLiResponseData<CreateResponseEnvelope> appResponseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(HttpStatus.S_200_OK, entityFromApp, false), headersFromApp, Collections.emptyList());
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Setup.
when((RestLiResponseData<CreateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(appResponseData);
when(_responseHandler.buildPartialResponse(_routingResult, appResponseData)).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();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_200_OK, responseData.getResponseEnvelope().getStatus());
assertEquals(headersFromApp, responseData.getHeaders());
assertEquals(entityFromApp, responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseData.getResponseEnvelope().setRecord(entityFromFilter1, HttpStatus.S_400_BAD_REQUEST);
responseData.getHeaders().clear();
return CompletableFuture.completedFuture(null);
}
}).doAnswer(new Answer<Object>() {
// Mock the behavior of the second filter.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseData.getResponseEnvelope().getStatus());
assertTrue(responseData.getHeaders().isEmpty());
assertEquals(responseData.getResponseEnvelope().getRecord(), entityFromFilter1);
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseData.getResponseEnvelope().setRecord(entityFromFilter2, HttpStatus.S_403_FORBIDDEN);
responseData.getHeaders().putAll(headersFromFilters);
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onResponse(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 with some response attachments.
_twoFilterRestLiCallback.onSuccess(result);
// Verify.
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, appResponseData.getResponseEnvelope().getStatus());
assertEquals(entityFromFilter2, appResponseData.getResponseEnvelope().getRecord());
assertEquals(headersFromFilters, appResponseData.getHeaders());
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
verify(_callback).onSuccess(partialResponse);
verifyZeroInteractions(_restRequest);
verifyNoMoreInteractions(_responseHandler, _callback);
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandle.
@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandle() 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 exFromFirstFilter = new RuntimeException("Runtime exception from first filter");
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Setup.
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseAppData);
when(_responseHandler.buildPartialResponse(_routingResult, responseAppData)).thenReturn(partialResponse);
when(_restRequest.getHeaders()).thenReturn(null);
Map<String, String> errorHeaders = buildErrorHeaders();
// Mock filter behavior.
doThrow(exFromFirstFilter).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];
// The second filter should be invoked with original exception
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertNull(responseData.getResponseEnvelope().getRecord());
assertEquals(responseData.getHeaders(), errorHeaders);
assertTrue(responseData.getResponseEnvelope().isErrorResponse());
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
// filter.
return completedFutureWithError(responseData.getResponseEnvelope().getException());
}
}).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> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), exCapture.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_402_PAYMENT_REQUIRED, responseAppData.getResponseEnvelope().getStatus());
assertEquals(responseAppData.getHeaders(), errorHeaders);
assertNull(responseAppData.getResponseEnvelope().getRecord());
RestLiServiceException restliEx = exCapture.getAllValues().get(0);
assertNotNull(restliEx);
assertEquals(exFromApp.getStatus(), restliEx.getStatus());
assertEquals(exFromApp.getMessage(), restliEx.getMessage());
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertEquals(restLiResponseException.getRestLiResponse(), partialResponse);
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
restliEx = (RestLiServiceException) restLiResponseException.getCause();
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, restliEx.getStatus());
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersExceptionFromSecondFilter.
@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithFiltersExceptionFromSecondFilter() throws Exception {
// App stuff.
String result = "foo";
RestLiResponseData<GetResponseEnvelope> appResponseData = ResponseDataBuilderUtil.buildGetResponseData(HttpStatus.S_200_OK, null);
// Filter stuff.
ArgumentCaptor<RestLiServiceException> exFromFilterCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Error from filter");
RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
RestLiResponseData<?> filterResponseData = new RestLiResponseDataImpl<>(new GetResponseEnvelope(exception), headersFromFilter, Collections.emptyList());
RestLiResponse partialFilterErrorResponse = new RestLiResponse.Builder().build();
final Exception exFromFilter = new RuntimeException("Exception From Filter");
// Common stuff.
RestException finalRestException = new RestException(new RestResponseBuilder().build());
// Setup.
when((RestLiResponseData<GetResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(appResponseData);
when(_restRequest.getHeaders()).thenReturn(null);
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exFromFilterCapture.capture(), anyMap(), anyList())).thenReturn(filterResponseData);
when(_responseHandler.buildPartialResponse(_routingResult, appResponseData)).thenReturn(partialFilterErrorResponse);
// Mock filter behavior.
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
RestLiResponseData<GetResponseEnvelope> responseData = (RestLiResponseData<GetResponseEnvelope>) responseContext.getResponseData();
// The second filter should be invoked with details of the exception thrown by the first
// filter. Verify incoming data.
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_200_OK);
assertNull(responseData.getResponseEnvelope().getRecord());
assertTrue(responseData.getHeaders().isEmpty());
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
responseData.getHeaders().put("first-filter", "success");
return CompletableFuture.completedFuture(null);
}
}).doThrow(exFromFilter).when(_filter).onResponse(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.onSuccess(result);
// Verify.
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verifyNoMoreInteractions(_responseHandler, _callback);
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
RestLiServiceException restliEx1 = (RestLiServiceException) restLiResponseException.getCause();
assertNotNull(restliEx1);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx1.getStatus());
assertEquals(exFromFilter.getMessage(), restliEx1.getMessage());
assertEquals(exFromFilter, restliEx1.getCause());
Map<String, String> expectedHeaders = buildErrorHeaders();
expectedHeaders.put("first-filter", "success");
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, appResponseData.getResponseEnvelope().getStatus());
assertEquals(appResponseData.getHeaders(), expectedHeaders);
assertNull(appResponseData.getResponseEnvelope().getRecord());
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiFilterResponseContextFactory method testFromResult.
@Test
@SuppressWarnings("unchecked")
public void testFromResult() throws Exception {
DataMap dataMap = new DataMap();
dataMap.put("foo", "bar");
Map<String, String> headers = Maps.newHashMap();
headers.put("x", "y");
RecordTemplate entity1 = new Foo(dataMap);
RestLiResponseData<GetResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new GetResponseEnvelope(HttpStatus.S_200_OK, entity1), headers, Collections.emptyList());
when((RestLiResponseData<GetResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, entity1)).thenReturn(responseData);
FilterResponseContext responseContext = _filterResponseContextFactory.fromResult(entity1);
assertEquals(responseContext.getResponseData(), responseData);
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, entity1);
}
Aggregations