Search in sources :

Example 31 with Parameter

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

the class TestGetArgumentBuilder method testKeyArguments.

@Test(dataProvider = "keyArgumentData")
public void testKeyArguments(Parameter<?> param, String keyName, Object keyValue, final DataSchema keySchema) throws IOException {
    ResourceModel model;
    if (keyName != null) {
        Key key = new Key(keyName, keyValue.getClass(), keySchema);
        model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
    } else {
        model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, null, true);
    }
    ResourceMethodDescriptor descriptor;
    if (param != null) {
        List<Parameter<?>> paramList = new ArrayList<>();
        paramList.add(param);
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
    } else {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, null);
    }
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult;
    if (param != null) {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
    } else {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
    }
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    Object[] expectedArgs;
    if (keyValue == null) {
        expectedArgs = new Object[] {};
    } else {
        expectedArgs = new Object[] { keyValue };
    }
    assertEquals(args, expectedArgs);
    verify(model, descriptor, context, routingResult, request);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 32 with Parameter

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

the class TestGetArgumentBuilder method testHeaderArgument.

@Test
public void testHeaderArgument() throws IOException {
    String keyName = "myComplexKeyCollectionId";
    Object keyValue = Integer.valueOf(123);
    DataSchema keySchema = new IntegerDataSchema();
    Key key = new Key(keyName, keyValue.getClass(), keySchema);
    Map<String, String> headers = new HashMap<>();
    String headerString = "An extra string.";
    headers.put("extra", headerString);
    List<Parameter<?>> headerParams = new ArrayList<>();
    headerParams.add(getIntegerParam());
    HeaderParam annotation = createMock(HeaderParam.class);
    expect(annotation.value()).andReturn("extra");
    AnnotationSet annotationSet = createMock(AnnotationSet.class);
    expect(annotationSet.getAll()).andReturn(new Annotation[] {});
    expect(annotationSet.get(HeaderParam.class)).andReturn(annotation);
    replay(annotation, annotationSet);
    Parameter<String> headerParam = new Parameter<>("", String.class, null, false, null, Parameter.ParamType.HEADER, false, annotationSet);
    headerParams.add(headerParam);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, headerParams);
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, headers, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    Object[] expectedArgs = new Object[] { keyValue, headerString };
    assertEquals(args, expectedArgs);
    verify(model, descriptor, context, routingResult, request, annotation, annotationSet);
}
Also used : HeaderParam(com.linkedin.restli.server.annotations.HeaderParam) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) HashMap(java.util.HashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) DataSchema(com.linkedin.data.schema.DataSchema) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) 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) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 33 with Parameter

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

the class TestUpdateArgumentBuilder method argumentData.

@DataProvider(name = "argumentData")
private Object[][] argumentData() {
    Parameter<?> myComplexKeyParam = new Parameter<>("", MyComplexKey.class, DataTemplateUtil.getSchema(MyComplexKey.class), false, null, Parameter.ParamType.POST, false, new AnnotationSet(new Annotation[] {}));
    List<Parameter<?>> collectionResourceParams = new ArrayList<>();
    collectionResourceParams.add(new Parameter<>("myComplexKeyCollectionId", Integer.class, new IntegerDataSchema(), false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {})));
    collectionResourceParams.add(myComplexKeyParam);
    List<Parameter<?>> simpleResourceParams = new ArrayList<>();
    simpleResourceParams.add(myComplexKeyParam);
    List<Parameter<?>> associationResourceParams = new ArrayList<>();
    associationResourceParams.add(new Parameter<>("myComplexKeyAssociationId", CompoundKey.class, null, false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {})));
    associationResourceParams.add(myComplexKeyParam);
    List<Parameter<?>> complexResourceKeyParams = new ArrayList<>();
    @SuppressWarnings("rawtypes") Parameter<ComplexResourceKey> complexResourceKeyParam = new Parameter<>("complexKeyTestId", ComplexResourceKey.class, null, false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {}));
    complexResourceKeyParams.add(complexResourceKeyParam);
    complexResourceKeyParams.add(myComplexKeyParam);
    return new Object[][] { { collectionResourceParams, new Key("myComplexKeyCollectionId", Integer.class, new IntegerDataSchema()), "myComplexKeyCollectionId", 4545 }, { simpleResourceParams, null, null, null }, { associationResourceParams, new Key("myComplexKeyAssociationId", CompoundKey.class, null), "myComplexKeyAssociationId", new CompoundKey().append("string1", "apples").append("string2", "oranges") }, { complexResourceKeyParams, new Key("complexKeyTestId", ComplexResourceKey.class, null), "complexKeyTestId", new ComplexResourceKey<>(new MyComplexKey().setA("keyString").setB(1234L), new EmptyRecord()) } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) CompoundKey(com.linkedin.restli.common.CompoundKey) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Parameter(com.linkedin.restli.internal.server.model.Parameter) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) DataProvider(org.testng.annotations.DataProvider)

Example 34 with Parameter

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

the class TestArgumentBuilder method testQueryParameterValidation.

@Test(dataProvider = "validateQueryParameter")
public void testQueryParameterValidation(String paramKey, Class<?> dataType, Object paramValue, boolean isValid, String errorMessage) {
    Parameter<?> param = new Parameter<>(paramKey, dataType, DataTemplateUtil.getSchema(dataType), false, null, Parameter.ParamType.QUERY, false, AnnotationSet.EMPTY);
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null).anyTimes();
    EasyMock.expect(mockResourceContext.getStructuredParameter(paramKey)).andReturn(paramValue).anyTimes();
    EasyMock.replay(mockResourceContext);
    List<Parameter<?>> parameters = Collections.singletonList(param);
    try {
        ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(true));
        assert (isValid);
    } catch (Exception e) {
        assert (!isValid);
        assert (e.getMessage().equals(errorMessage));
    }
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) Test(org.testng.annotations.Test)

Example 35 with Parameter

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

the class TestArgumentBuilder method testPathKeyParameterType.

@Test
@SuppressWarnings("deprecation")
public void testPathKeyParameterType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    // mock setup
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
    EasyMock.expect(mockPathKeys.get(testParamKey)).andReturn(expectedTestParamValue).anyTimes();
    EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys).anyTimes();
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    EasyMock.replay(mockResourceContext, mockPathKeys);
    List<Parameter<?>> parameters = new ArrayList<>();
    Parameter<String> param1 = new Parameter<>(testParamKey, String.class, null, false, null, Parameter.ParamType.KEY, false, AnnotationSet.EMPTY);
    Parameter<String> param2 = new Parameter<>(testParamKey, String.class, null, false, null, Parameter.ParamType.ASSOC_KEY_PARAM, false, AnnotationSet.EMPTY);
    Parameter<PathKeys> param3 = new Parameter<>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS, false, AnnotationSet.EMPTY);
    Parameter<PathKeys> param4 = new Parameter<>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS_PARAM, false, AnnotationSet.EMPTY);
    parameters.add(param1);
    parameters.add(param2);
    parameters.add(param3);
    parameters.add(param4);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], expectedTestParamValue);
    Assert.assertEquals(results[1], expectedTestParamValue);
    Assert.assertEquals(results[2], mockPathKeys);
    Assert.assertEquals(results[3], mockPathKeys);
}
Also used : PathKeys(com.linkedin.restli.server.PathKeys) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) 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