use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestCreateResponseBuilder method testCreateResponseException.
@Test
public void testCreateResponseException() throws URISyntaxException {
CreateResponse createResponse = new CreateResponse(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
RestLiResponseData<?> envelope = new CreateResponseBuilder().buildRestLiResponseData(restRequest, null, createResponse, Collections.emptyMap(), Collections.emptyList());
Assert.assertTrue(envelope.getResponseEnvelope().isErrorResponse());
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestCreateResponseBuilder method testBuilderException.
@Test
public void testBuilderException() throws URISyntaxException {
CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
CreateResponse createResponse = new CreateResponse(compoundKey, null);
RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
ProtocolVersion protocolVersion = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
ServerResourceContext mockContext = getMockResourceContext(protocolVersion, null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
try {
createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.emptyList());
Assert.fail("buildRestLiResponseData should have thrown an exception because the status is null!");
} catch (RestLiServiceException e) {
Assert.assertTrue(e.getMessage().contains("Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: "));
}
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestResponseMetadata method testMetadata.
@Test(dataProvider = "dataProvider")
public void testMetadata(ResourceMethod resourceMethod, Object responseObject, boolean hasEntity, String responseString) throws Exception {
final RestRequest mockRequest = mock(RestRequest.class);
final RoutingResult mockRoutingResult = mock(RoutingResult.class);
final ResourceMethodDescriptor mockResourceMethodDescriptor = mock(ResourceMethodDescriptor.class);
final ServerResourceContext mockContext = mock(ServerResourceContext.class);
final ProtocolVersion mockProtocolVersion = AllProtocolVersions.LATEST_PROTOCOL_VERSION;
final URI mockURI = new URI("http://fake.com");
when(mockRequest.getURI()).thenReturn(mockURI);
when(mockResourceMethodDescriptor.getMethodType()).thenReturn(resourceMethod);
when(mockRoutingResult.getResourceMethod()).thenReturn(mockResourceMethodDescriptor);
when(mockResourceMethodDescriptor.getType()).thenReturn(resourceMethod);
when(mockRoutingResult.getContext()).thenReturn(mockContext);
when(mockContext.getRestliProtocolVersion()).thenReturn(mockProtocolVersion);
when(mockContext.getRawRequestContext()).thenReturn(new RequestContext());
final ErrorResponseBuilder errorResponseBuilder = new ErrorResponseBuilder();
final RestLiResponseHandler responseHandler = new RestLiResponseHandler(new DefaultMethodAdapterProvider(errorResponseBuilder), errorResponseBuilder);
// Test success path
RestLiResponseData<?> responseData = responseHandler.buildRestLiResponseData(mockRequest, mockRoutingResult, responseObject);
responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
RestLiResponse response = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertEquals(response.getEntity() != null, hasEntity);
if (hasEntity) {
if (response.getEntity() instanceof IdResponse) {
assertNotNull(((IdResponse) response.getEntity()).getId());
} else {
assertEquals(response.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD), TEST_META_DATA);
assertEquals(response.getEntity().toString(), responseString);
}
}
// Verify metadata is cleared when an exception is set by a filter
responseData.getResponseEnvelope().setExceptionInternal(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
assertEquals(responseData.getResponseEnvelope().getResponseMetadata().size(), 0);
RestLiResponse errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
// Test case where resource method returns exception path
RestLiResponseData<?> errorResponseData = responseHandler.buildExceptionResponseData(mockRoutingResult, new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR), new HashMap<>(), new ArrayList<>());
responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testBuilderExceptions.
@Test(dataProvider = "exceptionTestData")
public void testBuilderExceptions(Object result, String expectedErrorMessage) throws URISyntaxException {
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
ServerResourceContext mockContext = getMockResourceContext(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
try {
responseBuilder.buildRestLiResponseData(request, routingResult, result, headers, Collections.emptyList());
Assert.fail("buildRestLiResponseData should have thrown an exception because of null elements");
} catch (RestLiServiceException e) {
Assert.assertTrue(e.getMessage().contains(expectedErrorMessage));
}
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestRestLiValidationFilter method testHandleProjection.
/**
* Ensures that the validation filter safely and correctly reacts to projections given a variety of resource types,
* resource methods, and projection masks. This was motivated by a bug that caused an NPE in the validation filter
* when the resource being queried was a {@link RestLiActions} resource and thus had no value class.
*/
@Test(dataProvider = "validateWithProjectionData")
@SuppressWarnings({ "unchecked" })
public void testHandleProjection(ResourceModel resourceModel, RestLiResponseData<RestLiResponseEnvelope> responseData, MaskTree projectionMask, boolean expectError) {
ResourceMethod resourceMethod = responseData.getResourceMethod();
when(filterRequestContext.getRequestData()).thenReturn(new RestLiRequestDataImpl.Builder().entity(makeTestRecord()).build());
when(filterRequestContext.getMethodType()).thenReturn(resourceMethod);
when(filterRequestContext.getFilterResourceModel()).thenReturn(new FilterResourceModelImpl(resourceModel));
when(filterRequestContext.getProjectionMask()).thenReturn(projectionMask);
when(filterResponseContext.getResponseData()).thenReturn((RestLiResponseData) responseData);
RestLiValidationFilter validationFilter = new RestLiValidationFilter();
try {
validationFilter.onRequest(filterRequestContext);
if (expectError) {
Assert.fail("Expected an error to be thrown on request in the validation filter, but none was thrown.");
}
} catch (RestLiServiceException e) {
if (expectError) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST);
return;
} else {
Assert.fail("An unexpected exception was thrown on request in the validation filter.", e);
}
}
validationFilter.onResponse(filterRequestContext, filterResponseContext);
}
Aggregations