use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchUpdateArgumentBuilder method testFailure.
@Test(dataProvider = "failureData")
public void testFailure(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys, String errorMessage) {
Set<Object> batchKeys = new HashSet<Object>(Arrays.asList(keys));
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, false, false);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
RestLiArgumentBuilder argumentBuilder = new BatchUpdateArgumentBuilder();
try {
argumentBuilder.extractRequestData(routingResult, request);
fail("Expected RoutingException");
} catch (RoutingException e) {
assertTrue(e.getMessage().contains(errorMessage));
}
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchUpdateArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys) {
Set<Object> batchKeys = new HashSet<Object>(Arrays.asList(keys));
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
@SuppressWarnings("rawtypes") Parameter<BatchUpdateRequest> param = new Parameter<BatchUpdateRequest>("", BatchUpdateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, Collections.singletonList(param));
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, true, false);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
RestLiArgumentBuilder argumentBuilder = new BatchUpdateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args.length, 1);
assertTrue(args[0] instanceof BatchUpdateRequest);
Map<?, ?> data = ((BatchUpdateRequest) args[0]).getData();
assertEquals(data.size(), 2);
MyComplexKey entity1 = (MyComplexKey) data.get(keys[0]);
MyComplexKey entity2 = (MyComplexKey) data.get(keys[1]);
assertEquals(entity1.getA(), "abc");
assertEquals((long) entity1.getB(), 123L);
assertEquals(entity2.getA(), "XY");
assertEquals((long) entity2.getB(), 456L);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method testUpdateStatusInstantiation.
@Test(dataProvider = "updateStatusInstantiation")
public void testUpdateStatusInstantiation(RestLiResponseData responseData, UpdateStatus expectedResult) {
ResourceContext mockContext = getMockResourceContext(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), null);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
PartialRestResponse response = new BatchUpdateResponseBuilder(new ErrorResponseBuilder()).buildResponse(routingResult, responseData);
Assert.assertEquals(((BatchResponse) response.getEntity()).getResults().get("key"), expectedResult);
}
use of com.linkedin.restli.internal.server.RoutingResult 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<Object, RestLiServiceException>();
RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_402_PAYMENT_REQUIRED);
errors.put("foo", exception);
EasyMock.expect(context.hasParameter("altkey")).andReturn(false);
EasyMock.expect(context.getBatchKeyErrors()).andReturn(errors);
EasyMock.replay(context);
RoutingResult routingResult = new RoutingResult(context, null);
RestLiResponseData envelope = builder.buildRestLiResponseData(null, routingResult, new BatchUpdateResult<Object, Integer>(Collections.<Object, UpdateResponse>emptyMap()), Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
Assert.assertEquals(envelope.getBatchResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
Assert.assertEquals(envelope.getBatchResponseEnvelope().getBatchResponseMap().size(), 1);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method unsupportedNullKeyMapTest.
/* Note that we use also need to test using java.util.concurrent.ConcurrentHashMap. This is because rest.li checks
* for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
* is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
* java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
* way that doesn't throw an NullPointerException.
*/
@Test(dataProvider = "unsupportedNullKeyMapData")
@SuppressWarnings("unchecked")
public void unsupportedNullKeyMapTest(Object results, ProtocolVersion protocolVersion, Map<String, UpdateStatus> expectedResults) {
ResourceContext mockContext = getMockResourceContext(protocolVersion, null);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertEquals(batchResponse.getResults(), expectedResults);
}
Aggregations