Search in sources :

Example 11 with Parameter

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

the class TestArgumentBuilder method testHeaderParamType.

@Test
public void testHeaderParamType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    HeaderParam annotation = EasyMock.createMock(HeaderParam.class);
    EasyMock.expect(annotation.value()).andReturn(testParamKey);
    AnnotationSet annotationSet = EasyMock.createMock(AnnotationSet.class);
    EasyMock.expect(annotationSet.getAll()).andReturn(new Annotation[] {});
    EasyMock.expect(annotationSet.get(HeaderParam.class)).andReturn(annotation);
    Map<String, String> headers = new HashMap<>();
    headers.put(testParamKey, expectedTestParamValue);
    EasyMock.expect(mockResourceContext.getRequestHeaders()).andReturn(headers);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    EasyMock.replay(mockResourceContext, annotation, annotationSet);
    Parameter<String> param = new Parameter<>(testParamKey, String.class, DataSchemaConstants.STRING_DATA_SCHEMA, false, null, Parameter.ParamType.HEADER, false, annotationSet);
    List<Parameter<?>> parameters = Collections.singletonList(param);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], expectedTestParamValue);
}
Also used : HeaderParam(com.linkedin.restli.server.annotations.HeaderParam) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 12 with Parameter

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

the class TestArgumentBuilder method testNoOpParamType.

@Test(dataProvider = "noOpParameterData")
public void testNoOpParamType(Class<?> dataType, Parameter.ParamType paramType) {
    String paramKey = "testParam";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter(paramKey, dataType, null, false, null, paramType, false, AnnotationSet.EMPTY);
    List<Parameter<?>> parameters = Collections.singletonList(param);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], null);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 13 with Parameter

use of com.linkedin.restli.internal.server.model.Parameter 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);
        EasyMock.expect(mockResourceContext.hasParameter(paramKey)).andReturn(false);
        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.singletonList(param);
    return ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template, getMockResourceMethodConfig(false));
}
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 14 with Parameter

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

the class TestBatchCreateArgumentBuilder method testArgumentBuilderSuccess.

@Test
public void testArgumentBuilderSuccess() throws Exception {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"elements\":[{\"b\":123,\"a\":\"abc\"},{\"b\":5678,\"a\":\"xyzw\"}]}");
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
    @SuppressWarnings("rawtypes") Parameter<BatchCreateRequest> param = new Parameter<>("", BatchCreateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, param, CollectionResourceAsyncTemplate.class.getMethod("batchCreate", BatchCreateRequest.class, Callback.class));
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 1);
    RestLiArgumentBuilder argumentBuilder = new BatchCreateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args.length, 1);
    assertTrue(args[0] instanceof BatchCreateRequest);
    @SuppressWarnings("unchecked") List<MyComplexKey> entities = ((BatchCreateRequest<Integer, MyComplexKey>) args[0]).getInput();
    assertEquals(entities.size(), 2);
    assertEquals(entities.get(0).getA(), "abc");
    assertEquals((long) entities.get(0).getB(), 123L);
    assertEquals(entities.get(1).getA(), "xyzw");
    assertEquals((long) entities.get(1).getB(), 5678L);
    verify(request, model, descriptor, context, routingResult);
}
Also used : BatchCreateRequest(com.linkedin.restli.server.BatchCreateRequest) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) CollectionResourceAsyncTemplate(com.linkedin.restli.server.resources.CollectionResourceAsyncTemplate) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 15 with Parameter

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

the class TestBatchGetArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(Set<Object> batchKeys) throws IOException {
    @SuppressWarnings("rawtypes") Parameter<Set> param = new Parameter<>("", Set.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, param);
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, batchKeys, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 2);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new BatchGetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    Object[] expectedArgs = new Object[] { batchKeys };
    assertEquals(args, expectedArgs);
    verify(descriptor, context, routingResult, request);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Aggregations

Parameter (com.linkedin.restli.internal.server.model.Parameter)42 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)29 Test (org.testng.annotations.Test)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)17 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)14 ArrayList (java.util.ArrayList)13 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)12 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)11 RestRequest (com.linkedin.r2.message.rest.RestRequest)10 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)10 Annotation (java.lang.annotation.Annotation)10 DataMap (com.linkedin.data.DataMap)9 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)8 PagingContext (com.linkedin.restli.server.PagingContext)6 Callback (com.linkedin.common.callback.Callback)5 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)5 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 Key (com.linkedin.restli.server.Key)5 HashMap (java.util.HashMap)5