use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithUnstructuredDataResponse.
@Test
@SuppressWarnings("unchecked")
public void testOnSuccessWithUnstructuredDataResponse() throws Exception {
String result = "foo";
RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Set up.
when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
// Invoke.
_noFilterRestLiCallback.onSuccess(result);
// Verify.
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_callback).onSuccess(partialResponse);
verifyZeroInteractions(_restRequest);
verifyNoMoreInteractions(_responseHandler, _callback);
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersSuccessfullyHandlingAppEx.
@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersSuccessfullyHandlingAppEx() throws Exception {
Exception exFromApp = new RuntimeException("Runtime exception from app");
RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final RecordTemplate entityFromFilter = Foo.createFoo("Key", "Two");
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Output");
RestLiResponseData<CreateResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(appException, false), headersFromApp, Collections.emptyList());
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exCapture.capture(), 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.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_404_NOT_FOUND, responseData.getResponseEnvelope().getStatus());
assertEquals(headersFromApp, responseData.getHeaders());
assertNull(responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseData.getHeaders().clear();
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
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());
assertNull(responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseData.getResponseEnvelope().setRecord(entityFromFilter, HttpStatus.S_403_FORBIDDEN);
responseData.getHeaders().putAll(headersFromFilter);
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.
_twoFilterRestLiCallback.onError(exFromApp);
// Verify.
assertNotNull(responseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getResponseEnvelope().getStatus());
assertEquals(entityFromFilter, responseData.getResponseEnvelope().getRecord());
assertEquals(headersFromFilter, responseData.getHeaders());
verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), exCapture.capture(), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_callback).onSuccess(partialResponse);
verify(_restRequest, times(1)).getHeaders();
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
RestLiServiceException restliEx = exCapture.getValue();
assertNotNull(restliEx);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
assertEquals(exFromApp.getMessage(), restliEx.getMessage());
assertEquals(exFromApp, restliEx.getCause());
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersNotHandlingAppEx.
@SuppressWarnings({ "unchecked", "deprecation" })
@Test
public void testOnErrorWithFiltersNotHandlingAppEx() throws Exception {
Exception exFromApp = new RuntimeException("Runtime exception from app");
RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Output");
RestLiResponseData<CreateResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(appException, false), headersFromApp, Collections.emptyList());
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
when(_responseHandler.buildExceptionResponseData(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.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_404_NOT_FOUND, responseData.getResponseEnvelope().getStatus());
assertEquals(headersFromApp, responseData.getHeaders());
assertNull(responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseData.getHeaders().clear();
return completedFutureWithError(responseData.getResponseEnvelope().getException());
}
}).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.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseData.getResponseEnvelope().getStatus());
assertNull(responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseData.getHeaders().putAll(headersFromFilter);
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.
assertNotNull(responseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getResponseEnvelope().getStatus());
assertNull(responseData.getResponseEnvelope().getRecord());
assertTrue(responseData.getResponseEnvelope().isErrorResponse());
assertEquals(responseData.getResponseEnvelope().getException().getErrorDetails(), appException.getErrorDetails());
assertEquals(responseData.getResponseEnvelope().getException().getOverridingFormat(), appException.getOverridingFormat());
assertEquals(responseData.getResponseEnvelope().getException().getServiceErrorCode(), appException.getServiceErrorCode());
assertEquals(responseData.getResponseEnvelope().getException().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(_routingResult), exCapture.capture(), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verify(_restRequest, times(1)).getHeaders();
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());
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertEquals(restLiResponseException.getRestLiResponse(), partialResponse);
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
RestLiServiceException restliEx2 = (RestLiServiceException) restLiResponseException.getCause();
assertNotNull(restliEx2);
assertEquals(HttpStatus.S_403_FORBIDDEN, restliEx2.getStatus());
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessBuildPartialResponseFailure.
@Test
@SuppressWarnings("unchecked")
public void testOnSuccessBuildPartialResponseFailure() throws Exception {
String result = "foo";
RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
RestResponse restResponse = new RestResponseBuilder().build();
// Set up.
when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
Exception e = new RuntimeException("Error1");
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenThrow(e);
// Invoke.
_noFilterRestLiCallback.onSuccess(result);
// Verify.
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verifyZeroInteractions(_restRequest);
verifyNoMoreInteractions(_responseHandler, _callback);
RestLiResponse restLiResponse = partialRestResponseExceptionCaptor.getValue().getRestLiResponse();
assertEquals(restLiResponse.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertEquals(RestConstants.HEADER_VALUE_ERROR, restLiResponse.getHeader(HeaderUtil.getErrorResponseHeaderName(Collections.emptyMap())));
}
use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessNoFilters.
@Test
@SuppressWarnings("unchecked")
public void testOnSuccessNoFilters() throws Exception {
String result = "foo";
RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Set up.
when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
// Invoke.
_noFilterRestLiCallback.onSuccess(result);
// Verify.
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_callback).onSuccess(partialResponse);
verifyZeroInteractions(_restRequest);
verifyNoMoreInteractions(_responseHandler, _callback);
}
Aggregations