use of com.linkedin.restli.server.RestLiRequestData in project rest.li by linkedin.
the class GetArgumentBuilder method extractRequestData.
@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, RestRequest request) {
final RestLiRequestData reqData;
if (ArgumentUtils.hasResourceKey(routingResult)) {
Object keyValue = ArgumentUtils.getResourceKey(routingResult);
reqData = new RestLiRequestDataImpl.Builder().key(keyValue).build();
} else {
reqData = new RestLiRequestDataImpl.Builder().build();
}
return reqData;
}
use of com.linkedin.restli.server.RestLiRequestData in project rest.li by linkedin.
the class TestRestLiMethodInvocation method checkAsyncInvocation.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void checkAsyncInvocation(BaseResource resource, RestLiCallback callback, ResourceMethodDescriptor methodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, boolean isDebugMode) throws URISyntaxException {
try {
RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/x-pson").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
if (entityBody != null) {
builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
}
RestRequest request = builder.build();
RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(pathkeys, request, new RequestContext()), methodDescriptor);
RequestExecutionReportBuilder requestExecutionReportBuilder = null;
if (isDebugMode) {
requestExecutionReportBuilder = new RequestExecutionReportBuilder();
}
RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getType());
RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
_invoker.invoke(requestData, routingResult, adapter, callback, requestExecutionReportBuilder);
EasyMock.verify(resource);
EasyMock.verify(callback);
Assert.assertEquals(((ServerResourceContext) routingResult.getContext()).getResponseMimeType(), "application/x-pson");
} catch (RestLiSyntaxException e) {
throw new RoutingException("syntax exception", 400);
} finally {
EasyMock.reset(callback, resource);
callback.onSuccess(EasyMock.anyObject(), isDebugMode ? EasyMock.isA(RequestExecutionReport.class) : EasyMock.<RequestExecutionReport>isNull(), EasyMock.anyObject(RestLiResponseAttachments.class));
EasyMock.expectLastCall().once();
EasyMock.replay(callback);
}
}
use of com.linkedin.restli.server.RestLiRequestData in project rest.li by linkedin.
the class TestPatchArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Key key, String keyName, Object keyValue) {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"patch\":{\"$set\":{\"a\":\"someString\"}}}", 1);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, true);
ResourceMethodDescriptor descriptor;
if (key != null) {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params);
} else {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 1, params);
}
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
RoutingResult routingResult;
if (key != null) {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
} else {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
}
RestLiArgumentBuilder argumentBuilder = new PatchArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
if (keyValue != null) {
Assert.assertEquals(args.length, 2);
Assert.assertEquals(args[0], keyValue);
}
Assert.assertTrue(args[args.length - 1] instanceof PatchRequest);
Map<String, Object> aMap = new HashMap<String, Object>();
aMap.put("a", "someString");
Map<String, Object> setMap = new HashMap<String, Object>();
setMap.put("$set", new DataMap(aMap));
Map<String, Object> data = new HashMap<String, Object>();
data.put("patch", new DataMap(setMap));
PatchRequest<MyComplexKey> patch = new PatchRequest<MyComplexKey>(new DataMap(data));
Assert.assertEquals(args[args.length - 1], patch);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.server.RestLiRequestData in project rest.li by linkedin.
the class TestCollectionArgumentBuilder method testArrayArgument.
@Test(dataProvider = "arrayArgument")
public void testArrayArgument(Parameter<?> param, String parameterKey, List<String> parameterValues, Object[] expectedArgs) {
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, param);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(parameterKey, parameterValues, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 1);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
RestLiArgumentBuilder argumentBuilder = new CollectionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args, expectedArgs);
verify(descriptor, context, routingResult, request);
}
use of com.linkedin.restli.server.RestLiRequestData in project rest.li by linkedin.
the class TestCollectionArgumentBuilder method testProjectionParams.
@Test
public void testProjectionParams() {
List<Parameter<?>> finderWithProjectionParams = new ArrayList<Parameter<?>>();
finderWithProjectionParams.add(new Parameter<String>("key", String.class, new StringDataSchema(), false, null, Parameter.ParamType.QUERY, true, new AnnotationSet(new Annotation[] {})));
finderWithProjectionParams.add(new Parameter<PagingContext>("", PagingContext.class, null, false, new PagingContext(0, 10), Parameter.ParamType.PAGING_CONTEXT_PARAM, false, new AnnotationSet(new Annotation[] {})));
finderWithProjectionParams.add(new Parameter<MaskTree>("", MaskTree.class, null, false, null, Parameter.ParamType.PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
finderWithProjectionParams.add(new Parameter<MaskTree>("", MaskTree.class, null, false, null, Parameter.ParamType.METADATA_PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
finderWithProjectionParams.add(new Parameter<MaskTree>("", MaskTree.class, null, false, null, Parameter.ParamType.PAGING_PROJECTION_PARAM, false, new AnnotationSet(new Annotation[] {})));
Map<String, String> finderWithProjectionContextParams = new HashMap<String, String>();
finderWithProjectionContextParams.put("start", "100");
finderWithProjectionContextParams.put("count", "15");
finderWithProjectionContextParams.put("key", "keyString");
Map<String, Integer> projectionMap = new HashMap<String, Integer>();
projectionMap.put("a", 1);
Map<String, Integer> metadataMap = new HashMap<String, Integer>();
metadataMap.put("intField", 1);
metadataMap.put("booleanField", 1);
Map<String, Integer> pagingMap = new HashMap<String, Integer>();
pagingMap.put("total", 1);
MaskTree projectionMask = new MaskTree(new DataMap(projectionMap));
MaskTree metadataMask = new MaskTree(new DataMap(metadataMap));
MaskTree pagingMask = new MaskTree(new DataMap(pagingMap));
Object[] expectedArgs = new Object[] { "keyString", new PagingContext(100, 15), projectionMask, metadataMask, pagingMask };
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, 1, finderWithProjectionParams);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(finderWithProjectionContextParams, projectionMask, metadataMask, pagingMask, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 1);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
RestLiArgumentBuilder argumentBuilder = new CollectionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args, expectedArgs);
verify(descriptor, context, routingResult, request);
}
Aggregations