Search in sources :

Example 1 with ResourceMethodConfig

use of com.linkedin.restli.server.config.ResourceMethodConfig in project rest.li by linkedin.

the class TestArgumentBuilder method testBuildArgsHappyPath.

@Test
public void testBuildArgsHappyPath() {
    // test integer association key integer
    String param1Key = "param1";
    Parameter<Integer> param1 = new Parameter<>(param1Key, Integer.class, DataTemplateUtil.getSchema(Integer.class), false, null, Parameter.ParamType.ASSOC_KEY_PARAM, false, AnnotationSet.EMPTY);
    Integer param1Value = 123;
    // test regular string argument
    String param2Key = "param2";
    Parameter<String> param2 = new Parameter<>(param2Key, String.class, DataTemplateUtil.getSchema(String.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    String param2Value = "param2Value";
    // test data template argument array with more than element
    String param3Key = "param3";
    Parameter<StringArray> param3 = new Parameter<>(param3Key, StringArray.class, DataTemplateUtil.getSchema(StringArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    DataList param3Value = new DataList(Arrays.asList("param3a", "param3b"));
    StringArray param3Final = new StringArray(param3Value);
    // test data template argument array with only one element
    String param4Key = "param4";
    Parameter<StringArray> param4 = new Parameter<>(param4Key, StringArray.class, DataTemplateUtil.getSchema(StringArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    String param4Value = "param4Value";
    StringArray param4Final = new StringArray(param4Value);
    // test record template
    String param5Key = "param5";
    Parameter<TestRecord> param5 = new Parameter<>(param5Key, TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    DataMap param5Value = new DataMap();
    param5Value.put("doubleField", "5.5");
    param5Value.put("floatField", "5");
    param5Value.put("intField", "5");
    param5Value.put("longField", "5");
    TestRecord param5Final = new TestRecord();
    param5Final.setDoubleField(5.5);
    param5Final.setFloatField(5F);
    param5Final.setIntField(5);
    param5Final.setLongField(5);
    // test record template array
    String param6Key = "param6";
    Parameter<TestRecordArray> param6 = new Parameter<>(param6Key, TestRecordArray.class, DataTemplateUtil.getSchema(TestRecordArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    DataList param6Value = new DataList();
    DataMap testRecordDataMap1 = new DataMap();
    testRecordDataMap1.put("doubleField", "6.6");
    testRecordDataMap1.put("floatField", "6");
    testRecordDataMap1.put("intField", "6");
    testRecordDataMap1.put("longField", "6");
    DataMap testRecordDataMap2 = new DataMap();
    testRecordDataMap2.put("doubleField", "66.6");
    testRecordDataMap2.put("floatField", "66");
    testRecordDataMap2.put("intField", "66");
    testRecordDataMap2.put("longField", "66");
    param6Value.add(testRecordDataMap1);
    param6Value.add(testRecordDataMap2);
    TestRecordArray param6Final = new TestRecordArray();
    TestRecord testRecord1 = new TestRecord();
    testRecord1.setDoubleField(6.6);
    testRecord1.setFloatField(6);
    testRecord1.setIntField(6);
    testRecord1.setLongField(6);
    TestRecord testRecord2 = new TestRecord();
    testRecord2.setDoubleField(66.6);
    testRecord2.setFloatField(66);
    testRecord2.setIntField(66);
    testRecord2.setLongField(66);
    param6Final.add(testRecord1);
    param6Final.add(testRecord2);
    List<Parameter<?>> parameters = new ArrayList<>();
    parameters.add(param1);
    parameters.add(param2);
    parameters.add(param3);
    parameters.add(param4);
    parameters.add(param5);
    parameters.add(param6);
    Object[] positionalArguments = new Object[0];
    Capture<String> param1Capture = EasyMock.newCapture();
    Capture<String> param2Capture = EasyMock.newCapture();
    Capture<String> param3Capture = EasyMock.newCapture();
    Capture<String> param4Capture = EasyMock.newCapture();
    Capture<String> param5Capture = EasyMock.newCapture();
    Capture<String> param6Capture = EasyMock.newCapture();
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
    ResourceMethodDescriptor mockResourceMethodDescriptor = getMockResourceMethod(parameters);
    ResourceMethodConfig mockResourceMethodConfig = EasyMock.createMock(ResourceMethodConfig.class);
    EasyMock.expect(mockResourceMethodConfig.shouldValidateResourceKeys()).andReturn(true).times(5);
    EasyMock.expect(mockResourceMethodConfig.shouldValidateQueryParams()).andReturn(false).times(5);
    EasyMock.replay(mockResourceMethodConfig);
    // easy mock for processing param1
    EasyMock.expect(mockPathKeys.get(EasyMock.capture(param1Capture))).andReturn(param1Value);
    EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys);
    // easy mock for processing param2
    EasyMock.expect(mockResourceContext.hasParameter(EasyMock.capture(param2Capture))).andReturn(true);
    EasyMock.expect(mockResourceContext.getParameter(EasyMock.capture(param2Capture))).andReturn(param2Value);
    // easy mock for processing param3
    EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param3Capture))).andReturn(param3Value);
    // easy mock for processing param4
    EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param4Capture))).andReturn(param4Value);
    // easy mock for processing param5
    EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param5Capture))).andReturn(param5Value);
    // easy mock for processing param6
    EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param6Capture))).andReturn(param6Value);
    EasyMock.replay(mockResourceContext, mockPathKeys);
    Object[] results = ArgumentBuilder.buildArgs(positionalArguments, mockResourceMethodDescriptor, mockResourceContext, null, mockResourceMethodConfig);
    EasyMock.verify(mockPathKeys, mockResourceContext);
    Assert.assertEquals(param1Capture.getValue(), param1Key);
    Assert.assertEquals(param2Capture.getValue(), param2Key);
    Assert.assertEquals(param3Capture.getValue(), param3Key);
    Assert.assertEquals(param4Capture.getValue(), param4Key);
    Assert.assertEquals(param5Capture.getValue(), param5Key);
    Assert.assertEquals(param6Capture.getValue(), param6Key);
    Assert.assertEquals(results[0], param1Value);
    Assert.assertEquals(results[1], param2Value);
    Assert.assertEquals(results[2], param3Final);
    Assert.assertEquals(results[3], param4Final);
    Assert.assertEquals(results[4], param5Final);
    Assert.assertEquals(results[5], param6Final);
}
Also used : MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) StringArray(com.linkedin.data.template.StringArray) TestRecordArray(com.linkedin.restli.server.TestRecordArray) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig) TestRecord(com.linkedin.restli.server.TestRecord) Test(org.testng.annotations.Test)

Example 2 with ResourceMethodConfig

use of com.linkedin.restli.server.config.ResourceMethodConfig in project rest.li by linkedin.

the class RestLiArgumentBuilderTestHelper method getMockRoutingResult.

public static RoutingResult getMockRoutingResult(ResourceMethodDescriptor descriptor, int getResourceMethodCount, ServerResourceContext context, int getContextCount) {
    RoutingResult mockRoutingResult = createMock(RoutingResult.class);
    if (descriptor != null) {
        expect(mockRoutingResult.getResourceMethod()).andReturn(descriptor).times(getResourceMethodCount);
    }
    if (context != null && getContextCount > 0) {
        expect(mockRoutingResult.getContext()).andReturn(context).times(getContextCount);
    }
    ResourceMethodConfig mockResourceMethodConfig = createMock(ResourceMethodConfig.class);
    expect(mockResourceMethodConfig.shouldValidateResourceKeys()).andReturn(false).anyTimes();
    expect(mockResourceMethodConfig.shouldValidateQueryParams()).andReturn(false).anyTimes();
    expect(mockRoutingResult.getResourceMethodConfig()).andReturn(mockResourceMethodConfig).anyTimes();
    replay(mockRoutingResult, mockResourceMethodConfig);
    return mockRoutingResult;
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig)

Example 3 with ResourceMethodConfig

use of com.linkedin.restli.server.config.ResourceMethodConfig in project rest.li by linkedin.

the class RestLiArgumentBuilderTestHelper method getMockRoutingResult.

public static RoutingResult getMockRoutingResult() {
    RoutingResult mockRoutingResult = createMock(RoutingResult.class);
    ResourceMethodConfig mockResourceMethodConfig = createMock(ResourceMethodConfig.class);
    expect(mockRoutingResult.getResourceMethodConfig()).andReturn(mockResourceMethodConfig).anyTimes();
    replay(mockRoutingResult, mockResourceMethodConfig);
    return mockRoutingResult;
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig)

Example 4 with ResourceMethodConfig

use of com.linkedin.restli.server.config.ResourceMethodConfig in project rest.li by linkedin.

the class RestLiArgumentBuilderTestHelper method getMockRoutingResult.

static RoutingResult getMockRoutingResult(ResourceMethodDescriptor descriptor, ServerResourceContext context) {
    RoutingResult mockRoutingResult = createMock(RoutingResult.class);
    if (descriptor != null) {
        expect(mockRoutingResult.getResourceMethod()).andReturn(descriptor).anyTimes();
    }
    if (context != null) {
        expect(mockRoutingResult.getContext()).andReturn(context).anyTimes();
    }
    ResourceMethodConfig mockResourceMethodConfig = createMock(ResourceMethodConfig.class);
    expect(mockResourceMethodConfig.shouldValidateResourceKeys()).andReturn(false).anyTimes();
    expect(mockRoutingResult.getResourceMethodConfig()).andReturn(mockResourceMethodConfig).anyTimes();
    replay(mockRoutingResult, mockResourceMethodConfig);
    return mockRoutingResult;
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig)

Example 5 with ResourceMethodConfig

use of com.linkedin.restli.server.config.ResourceMethodConfig in project rest.li by linkedin.

the class RestLiMethodInvoker method invoke.

/**
 * Invokes the method with the specified callback and arguments built from the request.
 */
public void invoke(final RestLiRequestData requestData, final RoutingResult invokableMethod, final RestLiArgumentBuilder restLiArgumentBuilder, final RestLiCallback callback) {
    try {
        ResourceMethodDescriptor resourceMethodDescriptor = invokableMethod.getResourceMethod();
        ResourceMethodConfig resourceMethodConfig = invokableMethod.getResourceMethodConfig();
        Object resource = _resourceFactory.create(resourceMethodDescriptor.getResourceModel().getResourceClass());
        // Acquire a handle on the ResourceContext when setting it in order to obtain any response attachments that need to
        // be streamed back.
        final ServerResourceContext resourceContext = invokableMethod.getContext();
        if (BaseResource.class.isAssignableFrom(resource.getClass())) {
            ((BaseResource) resource).setContext(resourceContext);
        }
        Object[] args = restLiArgumentBuilder.buildArguments(requestData, invokableMethod);
        // Validate the batch size for batch requests
        validateMaxBatchSize(requestData, resourceMethodDescriptor, resourceContext);
        // Now invoke the resource implementation.
        doInvoke(resourceMethodDescriptor, resourceMethodConfig, callback, resource, resourceContext, args);
    } catch (Exception e) {
        callback.onError(e);
    }
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) BaseResource(com.linkedin.restli.server.resources.BaseResource) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ResourceMethodConfig (com.linkedin.restli.server.config.ResourceMethodConfig)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)4 Test (org.testng.annotations.Test)3 ByteString (com.linkedin.data.ByteString)2 Trace (com.linkedin.parseq.trace.Trace)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 HttpStatus (com.linkedin.restli.common.HttpStatus)2 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)2 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)2 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 ResourceMethodConfigProvider (com.linkedin.restli.server.config.ResourceMethodConfigProvider)2 CustomString (com.linkedin.restli.server.custom.types.CustomString)2 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)2 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)2 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)2 AfterTest (org.testng.annotations.AfterTest)2 BeforeTest (org.testng.annotations.BeforeTest)2