Search in sources :

Example 1 with MutablePathKeys

use of com.linkedin.restli.internal.server.MutablePathKeys in project rest.li by linkedin.

the class TestFilterRequestContextInternalImpl method testFilterRequestContextAdapter.

@Test
public void testFilterRequestContextAdapter() throws Exception {
    final String resourceName = "resourceName";
    final String resourceNamespace = "resourceNamespace";
    final ResourceMethod methodType = ResourceMethod.GET;
    final DataMap customAnnotations = new DataMap();
    customAnnotations.put("foo", "Bar");
    final ProjectionMode projectionMode = ProjectionMode.AUTOMATIC;
    final MaskTree maskTree = new MaskTree();
    final MutablePathKeys pathKeys = new PathKeysImpl();
    final Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put("Key1", "Value1");
    final URI requestUri = new URI("foo.bar.com");
    final ProtocolVersion protoVersion = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    final DataMap queryParams = new DataMap();
    queryParams.put("Param1", "Val1");
    final Map<String, Object> localAttrs = new HashMap<>();
    localAttrs.put("Key1", "Val1");
    final RequestContext r2RequestContext = new RequestContext();
    r2RequestContext.putLocalAttr("Key1", "Val1");
    final String finderName = UUID.randomUUID().toString();
    final String actionName = UUID.randomUUID().toString();
    when(resourceModel.getName()).thenReturn(resourceName);
    when(resourceModel.getNamespace()).thenReturn(resourceNamespace);
    when(resourceMethod.getResourceModel()).thenReturn(resourceModel);
    when(resourceMethod.getMethodType()).thenReturn(methodType);
    when(resourceMethod.getFinderName()).thenReturn(finderName);
    when(resourceMethod.getActionName()).thenReturn(actionName);
    when(resourceMethod.getCustomAnnotationData()).thenReturn(customAnnotations);
    when(resourceMethod.getMethod()).thenReturn(null);
    when(context.getProjectionMode()).thenReturn(projectionMode);
    when(context.getProjectionMask()).thenReturn(maskTree);
    when(context.getPathKeys()).thenReturn(pathKeys);
    when(context.getRequestHeaders()).thenReturn(requestHeaders);
    when(context.getRequestURI()).thenReturn(requestUri);
    when(context.getRestliProtocolVersion()).thenReturn(protoVersion);
    when(context.getParameters()).thenReturn(queryParams);
    when(context.getRawRequestContext()).thenReturn(r2RequestContext);
    FilterRequestContextInternalImpl filterContext = new FilterRequestContextInternalImpl(context, resourceMethod);
    Object spValue = new Object();
    String spKey = UUID.randomUUID().toString();
    filterContext.getFilterScratchpad().put(spKey, spValue);
    assertEquals(filterContext.getFilterResourceModel().getResourceName(), resourceName);
    assertEquals(filterContext.getFilterResourceModel().getResourceNamespace(), resourceNamespace);
    assertEquals(filterContext.getMethodType(), methodType);
    assertEquals(filterContext.getCustomAnnotations(), customAnnotations);
    assertEquals(filterContext.getProjectionMode(), projectionMode);
    assertEquals(filterContext.getProjectionMask(), maskTree);
    assertEquals(filterContext.getPathKeys(), pathKeys);
    assertEquals(filterContext.getRequestHeaders(), requestHeaders);
    assertEquals(filterContext.getRequestURI(), requestUri);
    assertEquals(filterContext.getRestliProtocolVersion(), protoVersion);
    assertEquals(filterContext.getQueryParameters(), queryParams);
    assertEquals(filterContext.getActionName(), actionName);
    assertEquals(filterContext.getFinderName(), finderName);
    assertEquals(filterContext.getRequestContextLocalAttrs(), localAttrs);
    assertNull(filterContext.getMethod());
    assertTrue(filterContext.getFilterScratchpad().get(spKey) == spValue);
    filterContext.getRequestHeaders().put("header2", "value2");
    assertEquals(requestHeaders.get("header2"), "value2");
    verify(resourceModel).getName();
    verify(resourceModel).getNamespace();
    verify(resourceMethod).getMethodType();
    verify(resourceMethod).getResourceModel();
    verify(resourceMethod).getCustomAnnotationData();
    verify(resourceMethod).getFinderName();
    verify(resourceMethod).getActionName();
    verify(resourceMethod).getMethod();
    verify(context).getProjectionMode();
    verify(context).getProjectionMask();
    verify(context).getPathKeys();
    verify(context, times(2)).getRequestHeaders();
    verify(context).getRequestURI();
    verify(context).getRestliProtocolVersion();
    verify(context).getParameters();
    verify(context).getRawRequestContext();
    verify(resourceMethod).getFinderMetadataType();
    verifyNoMoreInteractions(context, resourceMethod, resourceModel);
}
Also used : MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) HashMap(java.util.HashMap) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) DataMap(com.linkedin.data.DataMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) RequestContext(com.linkedin.r2.message.RequestContext) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with MutablePathKeys

use of com.linkedin.restli.internal.server.MutablePathKeys in project rest.li by linkedin.

the class TestArgumentBuilder method testBuildOptionalArg.

// utility method for reuse
private Object[] testBuildOptionalArg(Parameter<?> param) {
    String paramKey = param.getName();
    Class<?> dataType = param.getType();
    Parameter.ParamType paramType = param.getParamType();
    // mock resource context
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    DynamicRecordTemplate template = null;
    if (paramType == Parameter.ParamType.POST) {
        template = new DynamicRecordTemplate(new DataMap(), null);
    } else {
        MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
        EasyMock.expect(mockPathKeys.get(paramKey)).andReturn(null);
        EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys);
        EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
        if (DataTemplate.class.isAssignableFrom(dataType)) {
            EasyMock.expect(mockResourceContext.getStructuredParameter(paramKey)).andReturn(null);
        } else {
            EasyMock.expect(mockResourceContext.getParameter(paramKey)).andReturn(null);
        }
        EasyMock.replay(mockResourceContext);
    }
    // invoke buildArgs
    List<Parameter<?>> parameters = Collections.<Parameter<?>>singletonList(param);
    return ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template);
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap)

Example 3 with MutablePathKeys

use of com.linkedin.restli.internal.server.MutablePathKeys 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<Integer>(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<String>(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<StringArray>(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<StringArray>(param4Key, StringArray.class, DataTemplateUtil.getSchema(StringArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
    String param4Value = "param4Value";
    StringArray param4Final = new StringArray(new DataList(Collections.singletonList(param4Value)));
    // test record template
    String param5Key = "param5";
    Parameter<TestRecord> param5 = new Parameter<TestRecord>(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<TestRecordArray>(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<Parameter<?>>();
    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 = new Capture<String>();
    Capture<String> param2Capture = new Capture<String>();
    Capture<String> param3Capture = new Capture<String>();
    Capture<String> param4Capture = new Capture<String>();
    Capture<String> param5Capture = new Capture<String>();
    Capture<String> param6Capture = new Capture<String>();
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
    ResourceMethodDescriptor mockResourceMethodDescriptor = getMockResourceMethod(parameters);
    //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.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);
    EasyMock.verify(mockPathKeys, mockResourceContext, mockResourceMethodDescriptor);
    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) Capture(org.easymock.Capture) 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) TestRecord(com.linkedin.restli.server.TestRecord) Test(org.testng.annotations.Test)

Example 4 with MutablePathKeys

use of com.linkedin.restli.internal.server.MutablePathKeys in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testActionsOnResource.

@Test
public void testActionsOnResource() throws Exception {
    ResourceModel repliesResourceModel = buildResourceModel(RepliesCollectionResource.class);
    ResourceModel locationResourceModel = buildResourceModel(LocationResource.class);
    ResourceModel discoveredItemsResourceModel = buildResourceModel(DiscoveredItemsResource.class);
    ResourceMethodDescriptor methodDescriptor;
    RepliesCollectionResource repliesResource;
    LocationResource locationResource;
    DiscoveredItemsResource discoveredItemsResource;
    // #1 Action on collection resource
    methodDescriptor = repliesResourceModel.findActionMethod("replyToAll", ResourceLevel.COLLECTION);
    repliesResource = getMockResource(RepliesCollectionResource.class);
    repliesResource.replyToAll("hello");
    EasyMock.expectLastCall().once();
    String jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
    MutablePathKeys pathKeys = new PathKeysImpl();
    pathKeys.append("statusID", 1L);
    checkInvocation(repliesResource, methodDescriptor, "POST", version, "/statuses/1/replies?action=replyToAll", jsonEntityBody, pathKeys);
    // #2 Action on simple resource
    methodDescriptor = locationResourceModel.findActionMethod("new_status_from_location", ResourceLevel.ENTITY);
    locationResource = getMockResource(LocationResource.class);
    locationResource.newStatusFromLocation(eq("hello"));
    EasyMock.expectLastCall().once();
    jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
    pathKeys = new PathKeysImpl();
    pathKeys.append("statusID", 1L);
    checkInvocation(locationResource, methodDescriptor, "POST", version, "/statuses/1/location?action=new_status_from_location", jsonEntityBody, pathKeys);
    // #3 Action on complex-key resource
    methodDescriptor = discoveredItemsResourceModel.findActionMethod("purge", ResourceLevel.COLLECTION);
    discoveredItemsResource = getMockResource(DiscoveredItemsResource.class);
    discoveredItemsResource.purge(12L);
    EasyMock.expectLastCall().once();
    jsonEntityBody = RestLiTestHelper.doubleQuote("{'user': 12}");
    checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, "/discovereditems/action=purge", jsonEntityBody, buildPathKeys());
}
Also used : MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) PromiseRepliesCollectionResource(com.linkedin.restli.server.twitter.PromiseRepliesCollectionResource) RepliesCollectionResource(com.linkedin.restli.server.twitter.RepliesCollectionResource) AsyncRepliesCollectionResource(com.linkedin.restli.server.twitter.AsyncRepliesCollectionResource) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) AsyncDiscoveredItemsResource(com.linkedin.restli.server.twitter.AsyncDiscoveredItemsResource) PromiseDiscoveredItemsResource(com.linkedin.restli.server.twitter.PromiseDiscoveredItemsResource) DiscoveredItemsResource(com.linkedin.restli.server.twitter.DiscoveredItemsResource) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) LocationResource(com.linkedin.restli.server.twitter.LocationResource) PromiseLocationResource(com.linkedin.restli.server.twitter.PromiseLocationResource) AsyncLocationResource(com.linkedin.restli.server.twitter.AsyncLocationResource) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with MutablePathKeys

use of com.linkedin.restli.internal.server.MutablePathKeys in project rest.li by linkedin.

the class TestRestLiMethodInvocation method buildBatchPathKeys.

public MutablePathKeys buildBatchPathKeys(Object... batchKeys) throws RestLiSyntaxException {
    MutablePathKeys result = new PathKeysImpl();
    Set<Object> keys = new HashSet<Object>();
    for (Object batchKey : batchKeys) {
        keys.add(batchKey);
    }
    result.setBatchKeys(keys);
    return result;
}
Also used : MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) EasyMock.anyObject(org.easymock.EasyMock.anyObject) HashSet(java.util.HashSet)

Aggregations

MutablePathKeys (com.linkedin.restli.internal.server.MutablePathKeys)6 Test (org.testng.annotations.Test)4 DataMap (com.linkedin.data.DataMap)3 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)3 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 Parameter (com.linkedin.restli.internal.server.model.Parameter)3 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ArrayList (java.util.ArrayList)2 BeforeTest (org.testng.annotations.BeforeTest)2 ByteString (com.linkedin.data.ByteString)1 DataList (com.linkedin.data.DataList)1 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)1 StringArray (com.linkedin.data.template.StringArray)1 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)1 RequestContext (com.linkedin.r2.message.RequestContext)1 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)1 PathKeys (com.linkedin.restli.server.PathKeys)1 ProjectionMode (com.linkedin.restli.server.ProjectionMode)1