use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.
the class TestExceptionsResource method testCreateError.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testCreateError(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws Exception {
Response<EmptyRecord> response = null;
RestLiResponseException exception = null;
try {
Request<EmptyRecord> createRequest = builders.create().input(new Greeting().setId(11L).setMessage("@#$%@!$%").setTone(Tone.INSULTING)).build();
ResponseFuture<EmptyRecord> future;
if (explicit) {
future = getClient().sendRequest(createRequest, errorHandlingBehavior);
} else {
future = getClient().sendRequest(createRequest);
}
response = future.getResponse();
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
Assert.fail("expected exception");
}
} catch (RestLiResponseException e) {
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
exception = e;
} else {
Assert.fail("not expected exception");
}
}
if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
Assert.assertNotNull(response);
Assert.assertTrue(response.hasError());
exception = response.getError();
Assert.assertEquals(response.getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
Assert.assertNull(response.getEntity());
}
Assert.assertNotNull(exception);
Assert.assertFalse(exception.hasDecodedResponse());
Assert.assertEquals(exception.getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
Assert.assertEquals(exception.getServiceErrorMessage(), "I will not tolerate your insolence!");
Assert.assertEquals(exception.getServiceErrorCode(), 999);
Assert.assertEquals(exception.getErrorSource(), RestConstants.HEADER_VALUE_ERROR);
Assert.assertEquals(exception.getErrorDetails().getString("reason"), "insultingGreeting");
Assert.assertTrue(exception.getServiceErrorStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:406, serviceErrorCode:999]: I will not tolerate your insolence!"), "stacktrace mismatch:" + exception.getStackTrace());
}
use of com.linkedin.restli.common.EmptyRecord 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.common.EmptyRecord 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.common.EmptyRecord in project rest.li by linkedin.
the class TestRestLiResponseData method testBatchResponseEnvelopeUpdates.
@Test(dataProvider = "batchResponseEnvelopesProvider")
public void testBatchResponseEnvelopeUpdates(RestLiResponseDataImpl responseData) {
BatchResponseEnvelope responseEnvelope = responseData.getBatchResponseEnvelope();
Assert.assertFalse(responseData.isErrorResponse());
Assert.assertNull(responseData.getServiceException());
responseData.setException(exception500);
Assert.assertNull(responseEnvelope.getBatchResponseMap());
Map<Object, BatchResponseEnvelope.BatchResponseEntry> targetMap = new HashMap<Object, BatchResponseEnvelope.BatchResponseEntry>();
responseEnvelope.setBatchResponseMap(targetMap, HttpStatus.S_200_OK);
Assert.assertNull(responseData.getServiceException());
targetMap.put("key", new BatchResponseEnvelope.BatchResponseEntry(null, new EmptyRecord()));
Assert.assertEquals(responseEnvelope.getBatchResponseMap().size(), 1);
Assert.assertEquals(responseEnvelope.getBatchResponseMap().get("key").getRecord(), new EmptyRecord());
}
use of com.linkedin.restli.common.EmptyRecord in project rest.li by linkedin.
the class TestRestLiResponseEnvelope method testSetNewEnvelopeData.
@Test(dataProvider = "envelopeResourceMethodDataProvider")
public void testSetNewEnvelopeData(RestLiResponseEnvelope responseEnvelope, ResourceMethod resourceMethod) {
ResponseType responseType = ResponseType.fromMethodType(resourceMethod);
switch(responseType) {
case SINGLE_ENTITY:
RecordResponseEnvelope recordResponseEnvelope = (RecordResponseEnvelope) responseEnvelope;
RecordTemplate oldRecord = recordResponseEnvelope.getRecord();
RecordTemplate newRecord = new AnyRecord(new DataMap());
newRecord.data().put("test", "testing");
recordResponseEnvelope.setRecord(newRecord, HttpStatus.S_200_OK);
Assert.assertNotEquals(recordResponseEnvelope.getRecord(), oldRecord);
break;
case GET_COLLECTION:
CollectionResponseEnvelope collectionResponseEnvelope = (CollectionResponseEnvelope) responseEnvelope;
List<? extends RecordTemplate> oldResponses = collectionResponseEnvelope.getCollectionResponse();
RecordTemplate oldResponseMetadata = collectionResponseEnvelope.getCollectionResponseCustomMetadata();
CollectionMetadata oldPagingMetadata = collectionResponseEnvelope.getCollectionResponsePaging();
RecordTemplate newResponseMetadata = new AnyRecord(new DataMap());
newResponseMetadata.data().put("test", "testing");
CollectionMetadata newResponsesPaging = new CollectionMetadata();
List<? extends RecordTemplate> newResponses = Arrays.asList(new AnyRecord(new DataMap()));
collectionResponseEnvelope.setCollectionResponse(newResponses, newResponsesPaging, newResponseMetadata, HttpStatus.S_200_OK);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponse(), oldResponses);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), oldResponseMetadata);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponsePaging(), oldPagingMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponse(), newResponses);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), newResponseMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponsePaging(), newResponsesPaging);
break;
case CREATE_COLLECTION:
BatchCreateResponseEnvelope batchCreateResponseEnvelope = (BatchCreateResponseEnvelope) responseEnvelope;
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> oldCreateResponses = batchCreateResponseEnvelope.getCreateResponses();
CreateIdStatus<String> newCreateIdStatus = new CreateIdStatus<String>(new DataMap(), "key");
RestLiServiceException newException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
BatchCreateResponseEnvelope.CollectionCreateResponseItem successCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newCreateIdStatus);
BatchCreateResponseEnvelope.CollectionCreateResponseItem exceptionCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newException, "id2");
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> newCreateResponses = Arrays.asList(successCreateItem, exceptionCreateItem);
batchCreateResponseEnvelope.setCreateResponse(newCreateResponses, HttpStatus.S_200_OK);
Assert.assertNotEquals(batchCreateResponseEnvelope.getCreateResponses(), oldCreateResponses);
Assert.assertEquals(batchCreateResponseEnvelope.getCreateResponses(), newCreateResponses);
BatchCreateResponseEnvelope.CollectionCreateResponseItem firstItem = batchCreateResponseEnvelope.getCreateResponses().get(0);
Assert.assertNull(firstItem.getId());
Assert.assertEquals(firstItem.getRecord(), newCreateIdStatus);
Assert.assertFalse(firstItem.isErrorResponse());
Assert.assertNull(firstItem.getException());
BatchCreateResponseEnvelope.CollectionCreateResponseItem secondItem = batchCreateResponseEnvelope.getCreateResponses().get(1);
Assert.assertEquals(secondItem.getId(), "id2");
Assert.assertNull(secondItem.getRecord());
Assert.assertTrue(secondItem.isErrorResponse());
Assert.assertEquals(secondItem.getException(), newException);
break;
case BATCH_ENTITIES:
BatchResponseEnvelope batchResponseEnvelope = (BatchResponseEnvelope) responseEnvelope;
Map<?, BatchResponseEnvelope.BatchResponseEntry> oldBatchResponses = batchResponseEnvelope.getBatchResponseMap();
RecordTemplate newResponseRecord = new EmptyRecord();
RestLiServiceException newResponseException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<String, BatchResponseEnvelope.BatchResponseEntry> newBatchResponses = new HashMap<String, BatchResponseEnvelope.BatchResponseEntry>();
newBatchResponses.put("id1", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, newResponseRecord));
newBatchResponses.put("id2", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_500_INTERNAL_SERVER_ERROR, newResponseException));
batchResponseEnvelope.setBatchResponseMap(newBatchResponses, HttpStatus.S_200_OK);
Map<?, BatchResponseEnvelope.BatchResponseEntry> envelopeMap = batchResponseEnvelope.getBatchResponseMap();
Assert.assertNotEquals(envelopeMap, oldBatchResponses);
Assert.assertEquals(envelopeMap, newBatchResponses);
BatchResponseEnvelope.BatchResponseEntry id1Entry = envelopeMap.get("id1");
Assert.assertEquals(id1Entry.getStatus(), HttpStatus.S_200_OK);
Assert.assertEquals(id1Entry.getRecord(), newResponseRecord);
Assert.assertFalse(id1Entry.hasException());
Assert.assertNull(id1Entry.getException());
BatchResponseEnvelope.BatchResponseEntry id2Entry = envelopeMap.get("id2");
Assert.assertEquals(id2Entry.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Assert.assertNull(id2Entry.getRecord());
Assert.assertTrue(id2Entry.hasException());
Assert.assertEquals(id2Entry.getException(), newResponseException);
break;
case STATUS_ONLY:
// status only envelopes are blank by default since they have no data fields
break;
default:
throw new IllegalStateException();
}
}
Aggregations