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.");
}
}
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);
}
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()][]);
}
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);
}
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() + "\"");
}
}
Aggregations