Search in sources :

Example 16 with RestLiRequestData

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;
}
Also used : RestLiRequestDataImpl(com.linkedin.restli.server.RestLiRequestDataImpl) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 17 with RestLiRequestData

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);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) URI(java.net.URI) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 18 with RestLiRequestData

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);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) HashMap(java.util.HashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 19 with RestLiRequestData

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 20 with RestLiRequestData

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);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) StringDataSchema(com.linkedin.data.schema.StringDataSchema) RestRequest(com.linkedin.r2.message.rest.RestRequest) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PagingContext(com.linkedin.restli.server.PagingContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Aggregations

RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)27 RestRequest (com.linkedin.r2.message.rest.RestRequest)24 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)24 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)22 Test (org.testng.annotations.Test)22 ResourceContext (com.linkedin.restli.server.ResourceContext)20 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)11 Parameter (com.linkedin.restli.internal.server.model.Parameter)10 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)9 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)9 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)8 RoutingException (com.linkedin.restli.server.RoutingException)7 Annotation (java.lang.annotation.Annotation)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 CompoundKey (com.linkedin.restli.common.CompoundKey)4 Key (com.linkedin.restli.server.Key)4 HashSet (java.util.HashSet)4 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)3 RequestContext (com.linkedin.r2.message.RequestContext)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3