use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestUpdateArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Key key, String keyName, Object keyValue) throws IOException, NoSuchMethodException {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}");
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, key, true);
ResourceMethodDescriptor descriptor;
if (key != null) {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
} else {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
}
ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
RoutingResult routingResult;
if (key != null) {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 5, context, 2);
} else {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 1);
}
RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
if (keyValue != null) {
assertEquals(args.length, 2);
assertEquals(args[0], keyValue);
}
assertTrue(args[args.length - 1] instanceof MyComplexKey);
assertEquals(((MyComplexKey) args[args.length - 1]).getA(), "xyz");
assertEquals((long) ((MyComplexKey) args[args.length - 1]).getB(), 123L);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestUpdateArgumentBuilder method testUnstructuredDataArgumentBuilder.
@Test(dataProvider = "argumentData")
public void testUnstructuredDataArgumentBuilder(List<Parameter<?>> params, Key key, String keyName, Object keyValue) throws IOException, NoSuchMethodException {
params.remove(0);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}");
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, key, true);
ResourceMethodDescriptor descriptor;
if (key != null) {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, params, UnstructuredDataCollectionResourceReactive.class.getMethod("update", Object.class, UnstructuredDataReactiveReader.class, Callback.class));
} else {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params, UnstructuredDataCollectionResourceReactive.class.getMethod("update", Object.class, UnstructuredDataReactiveReader.class, Callback.class));
}
ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
RoutingResult routingResult;
if (key != null) {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 5, context, 2);
} else {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 1);
}
RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
if (keyValue != null) {
assertEquals(args.length, 1);
assertEquals(args[0], keyValue);
}
assertEquals(requestData.hasEntity(), false);
}
use of com.linkedin.restli.internal.server.RoutingResult 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.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testCreateResultBuilder.
@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String uriString, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) throws URISyntaxException {
List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
BatchCreateResult<Long, Foo> results = new BatchCreateResult<>(createResponses);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ServerResourceContext mockContext = getMockResourceContext(altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
RestRequest request = new RestRequestBuilder(new URI(uriString)).build();
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, headers, Collections.emptyList());
RestLiResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<>();
for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
items.add((CreateIdStatus<Object>) item.getRecord());
}
Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<>(items));
Assert.assertEquals(expectedStatuses, items);
Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testProjectionInBuildRestLiResponseData.
@Test
@SuppressWarnings("unchecked")
public void testProjectionInBuildRestLiResponseData() throws URISyntaxException {
MaskTree maskTree = new MaskTree();
maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false).atLeastOnce();
EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(true);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
EasyMock.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>();
Foo foo = new Foo();
foo.setStringField("foo1");
foo.setFruitsField(Fruits.APPLE);
createKVResponses.add(new CreateKVResponse<>(1L, foo));
BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, Collections.emptyMap(), Collections.emptyList());
Assert.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
CreateIdEntityStatus<Long, Foo> item = (CreateIdEntityStatus<Long, Foo>) responseData.getResponseEnvelope().getCreateResponses().get(0).getRecord();
Assert.assertEquals(item.getLocation(), "/foo/1");
DataMap dataMap = item.data().getDataMap("entity");
Assert.assertEquals(dataMap.size(), 1);
Assert.assertEquals(dataMap.get("fruitsField"), Fruits.APPLE.toString());
EasyMock.verify(mockContext);
}
Aggregations