Search in sources :

Example 31 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestArgumentBuilder method testRestLiAttachmentsParamResourceNotExpect.

@Test
public void testRestLiAttachmentsParamResourceNotExpect() {
    // This test makes sure that if the resource method did not expect attachments but there were attachments present
    // in the request, that an exception is thrown.
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    final RestLiAttachmentReader restLiAttachmentReader = new RestLiAttachmentReader(null);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(restLiAttachmentReader);
    EasyMock.replay(mockResourceContext);
    List<Parameter<?>> parameters = Collections.emptyList();
    try {
        ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
        Assert.fail();
    } catch (RestLiServiceException restLiServiceException) {
        Assert.assertEquals(restLiServiceException.getStatus(), HttpStatus.S_400_BAD_REQUEST);
        Assert.assertEquals(restLiServiceException.getMessage(), "Resource method endpoint invoked does not accept any request attachments.");
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 32 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method testContextErrors.

@Test
public void testContextErrors() {
    BatchGetResponseBuilder builder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    ServerResourceContext context = EasyMock.createMock(ServerResourceContext.class);
    Map<Object, RestLiServiceException> errors = new HashMap<>();
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_402_PAYMENT_REQUIRED);
    errors.put("foo", exception);
    EasyMock.expect(context.hasParameter("altkey")).andReturn(false).anyTimes();
    EasyMock.expect(context.getBatchKeyErrors()).andReturn(errors).anyTimes();
    EasyMock.expect(context.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.replay(context);
    RoutingResult routingResult = new RoutingResult(context, null);
    RestLiResponseData<BatchGetResponseEnvelope> responseData = builder.buildRestLiResponseData(null, routingResult, new BatchResult<>(Collections.emptyMap(), Collections.emptyMap()), Collections.emptyMap(), Collections.emptyList());
    Assert.assertEquals(responseData.getResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
    Assert.assertEquals(responseData.getResponseEnvelope().getBatchResponseMap().size(), 1);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 33 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchUpdateResponseBuilder method dataProvider.

@DataProvider(name = "testData")
public Object[][] dataProvider() {
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
    CompoundKey c3 = new CompoundKey().append("a", "a3").append("b", 3);
    Map<CompoundKey, UpdateResponse> results = new HashMap<>();
    results.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
    results.put(c2, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
    RestLiServiceException restLiServiceException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
    Map<CompoundKey, RestLiServiceException> errors = Collections.singletonMap(c3, restLiServiceException);
    BatchUpdateResult<CompoundKey, Foo> batchUpdateResult = new BatchUpdateResult<>(results, errors);
    Map<CompoundKey, UpdateResponse> keyOverlapResults = new HashMap<>();
    keyOverlapResults.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
    keyOverlapResults.put(c2, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
    keyOverlapResults.put(c3, new UpdateResponse(HttpStatus.S_404_NOT_FOUND));
    BatchUpdateResult<CompoundKey, Foo> keyOverlapBatchUpdateResult = new BatchUpdateResult<>(keyOverlapResults, errors);
    UpdateStatus updateStatus = new UpdateStatus().setStatus(202);
    ErrorResponse errorResponse = new ErrorResponse().setStatus(404);
    Map<String, UpdateStatus> expectedProtocol1Results = new HashMap<>();
    expectedProtocol1Results.put("a=a1&b=1", updateStatus);
    expectedProtocol1Results.put("a=a2&b=2", updateStatus);
    Map<String, ErrorResponse> expectedProtocol1Errors = new HashMap<>();
    expectedProtocol1Errors.put("a=a3&b=3", errorResponse);
    Map<String, UpdateStatus> expectedProtocol2Results = new HashMap<>();
    expectedProtocol2Results.put("(a:a1,b:1)", updateStatus);
    expectedProtocol2Results.put("(a:a2,b:2)", updateStatus);
    Map<String, ErrorResponse> expectedProtocol2Errors = new HashMap<>();
    expectedProtocol2Errors.put("(a:a3,b:3)", errorResponse);
    Map<String, UpdateStatus> expectedAltKeyResults = new HashMap<>();
    expectedAltKeyResults.put("aa1xb1", updateStatus);
    expectedAltKeyResults.put("aa2xb2", updateStatus);
    Map<String, ErrorResponse> expectedAltKeyErrors = new HashMap<>();
    expectedAltKeyErrors.put("aa3xb3", errorResponse);
    Map<String, AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<>();
    alternativeKeyMap.put("alt", new AlternativeKey<>(new TestKeyCoercer(), String.class, new StringDataSchema()));
    List<Object[]> data = new ArrayList<>();
    for (ResourceMethod resourceMethod : BUILDERS.keySet()) {
        data.add(new Object[] { batchUpdateResult, null, null, expectedProtocol1Results, expectedProtocol1Errors, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), resourceMethod });
        data.add(new Object[] { batchUpdateResult, null, null, expectedProtocol2Results, expectedProtocol2Errors, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), resourceMethod });
        data.add(new Object[] { batchUpdateResult, "alt", alternativeKeyMap, expectedAltKeyResults, expectedAltKeyErrors, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), resourceMethod });
        data.add(new Object[] { batchUpdateResult, "alt", alternativeKeyMap, expectedAltKeyResults, expectedAltKeyErrors, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), resourceMethod });
        data.add(new Object[] { keyOverlapBatchUpdateResult, null, null, expectedProtocol2Results, expectedProtocol2Errors, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), resourceMethod });
    }
    return data.toArray(new Object[data.size()][]);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) ErrorResponse(com.linkedin.restli.common.ErrorResponse) StringDataSchema(com.linkedin.data.schema.StringDataSchema) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) AlternativeKey(com.linkedin.restli.server.AlternativeKey) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataProvider(org.testng.annotations.DataProvider)

Example 34 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestBatchUpdateResponseBuilder method testContextErrors.

@Test
public void testContextErrors() {
    BatchUpdateResponseBuilder builder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
    ServerResourceContext context = EasyMock.createMock(ServerResourceContext.class);
    Map<Object, RestLiServiceException> errors = new HashMap<>();
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_402_PAYMENT_REQUIRED);
    errors.put("foo", exception);
    EasyMock.expect(context.hasParameter("altkey")).andReturn(false).anyTimes();
    EasyMock.expect(context.getBatchKeyErrors()).andReturn(errors).anyTimes();
    EasyMock.expect(context.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.replay(context);
    RoutingResult routingResult = new RoutingResult(context, getMockResourceMethodDescriptor(null));
    RestLiResponseData<BatchUpdateResponseEnvelope> responseData = builder.buildRestLiResponseData(null, routingResult, new BatchUpdateResult<>(Collections.emptyMap()), Collections.emptyMap(), Collections.emptyList());
    Assert.assertEquals(responseData.getResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
    Assert.assertEquals(responseData.getResponseEnvelope().getBatchResponseMap().size(), 1);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 35 with RestLiServiceException

use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.

the class TestCreateResponseBuilder method testReturnEntityException.

/**
 * We want to ensure that trying to create a Rest.li response from an empty {@link CreateKVResponse} causes an exception.
 */
@Test
public void testReturnEntityException() throws URISyntaxException {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(true);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.replay(mockContext);
    RoutingResult routingResult = new RoutingResult(mockContext, null);
    CreateKVResponse<Integer, Foo> createKVResponse = new CreateKVResponse<>(null, null);
    try {
        CreateResponseBuilder responseBuilder = new CreateResponseBuilder();
        RestLiResponseData<CreateResponseEnvelope> envelope = responseBuilder.buildRestLiResponseData(new RestRequestBuilder(new URI("/foo")).build(), routingResult, createKVResponse, Collections.emptyMap(), Collections.emptyList());
        Assert.fail("Attempting to build RestLi response data with a null entity here should cause an exception.");
    } catch (RestLiServiceException e) {
        Assert.assertEquals(e.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR, "");
        Assert.assertTrue(e.getMessage().contains("Unexpected null encountered. Entity is null inside of a CreateKVResponse"), "Invalid exception message: \"" + e.getMessage() + "\"");
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Foo(com.linkedin.pegasus.generator.examples.Foo) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) URI(java.net.URI) 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