Search in sources :

Example 36 with RestLiServiceException

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());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 37 with RestLiServiceException

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: "));
    }
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 38 with RestLiServiceException

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));
}
Also used : DefaultMethodAdapterProvider(com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider) IdResponse(com.linkedin.restli.common.IdResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 39 with RestLiServiceException

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));
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 40 with RestLiServiceException

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);
}
Also used : FilterResourceModelImpl(com.linkedin.restli.internal.server.filter.FilterResourceModelImpl) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiRequestDataImpl(com.linkedin.restli.server.RestLiRequestDataImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)145 Test (org.testng.annotations.Test)55 HashMap (java.util.HashMap)39 DataMap (com.linkedin.data.DataMap)29 RecordTemplate (com.linkedin.data.template.RecordTemplate)21 Map (java.util.Map)21 RoutingException (com.linkedin.restli.server.RoutingException)20 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)18 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)17 BeforeTest (org.testng.annotations.BeforeTest)17 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)14 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)13 RestRequest (com.linkedin.r2.message.rest.RestRequest)12 ByteString (com.linkedin.data.ByteString)11 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 BatchResult (com.linkedin.restli.server.BatchResult)11