Search in sources :

Example 6 with Parameter

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

the class TestArgumentBuilder method testPagingContextParamType.

@Test
@SuppressWarnings("deprecation")
public void testPagingContextParamType() {
    String testParamKey = "testParam";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    PagingContext pagingContext = new PagingContext(RestConstants.DEFAULT_START, RestConstants.DEFAULT_COUNT, false, false);
    EasyMock.expect(mockResourceContext.getParameter(RestConstants.START_PARAM)).andReturn(null).anyTimes();
    EasyMock.expect(mockResourceContext.getParameter(RestConstants.COUNT_PARAM)).andReturn(null).anyTimes();
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    EasyMock.replay(mockResourceContext);
    List<Parameter<?>> parameters = new ArrayList<>();
    Parameter<PagingContext> param1 = new Parameter<>(testParamKey, PagingContext.class, null, false, null, Parameter.ParamType.PAGING_CONTEXT_PARAM, false, AnnotationSet.EMPTY);
    Parameter<PagingContext> param2 = new Parameter<>(testParamKey, PagingContext.class, null, false, null, Parameter.ParamType.CONTEXT, false, AnnotationSet.EMPTY);
    parameters.add(param1);
    parameters.add(param2);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], pagingContext);
    Assert.assertEquals(results[1], pagingContext);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PagingContext(com.linkedin.restli.server.PagingContext) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 7 with Parameter

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

the class TestArgumentBuilder method testResourceContextParameterType.

@Test
@SuppressWarnings("deprecation")
public void testResourceContextParameterType() {
    String testParamKey = "testParam";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    List<Parameter<?>> parameters = new ArrayList<>();
    Parameter<ResourceContext> param1 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT, false, AnnotationSet.EMPTY);
    Parameter<ResourceContext> param2 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT_PARAM, false, AnnotationSet.EMPTY);
    parameters.add(param1);
    parameters.add(param2);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], mockResourceContext);
    Assert.assertEquals(results[1], mockResourceContext);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 8 with Parameter

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

the class TestArgumentBuilder method testRestLiAttachmentsParamResourceNotExpect.

@Test
public void testRestLiAttachmentsParamResourceNotExpect() {
    // This test makes sure that if the resource method did not expect attachments but there were attachments present
    // in the request, that an exception is thrown.
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    final RestLiAttachmentReader restLiAttachmentReader = new RestLiAttachmentReader(null);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(restLiAttachmentReader);
    EasyMock.replay(mockResourceContext);
    List<Parameter<?>> parameters = Collections.emptyList();
    try {
        ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
        Assert.fail();
    } catch (RestLiServiceException restLiServiceException) {
        Assert.assertEquals(restLiServiceException.getStatus(), HttpStatus.S_400_BAD_REQUEST);
        Assert.assertEquals(restLiServiceException.getMessage(), "Resource method endpoint invoked does not accept any request attachments.");
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 9 with Parameter

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

the class TestCollectionArgumentBuilder method argumentData.

@DataProvider(name = "argumentData")
private Object[][] argumentData() {
    List<Parameter<?>> getAllParams = new ArrayList<>();
    getAllParams.add(getPagingContextParam());
    Map<String, String> getAllContextParams = new HashMap<>();
    getAllContextParams.put("start", "33");
    getAllContextParams.put("count", "444");
    Map<String, String> finderContextParams = new HashMap<>();
    finderContextParams.put("start", "33");
    finderContextParams.put("count", "444");
    finderContextParams.put("required", "777");
    finderContextParams.put("optional", null);
    Map<String, String> finderContextParamsWithOptionalString = new HashMap<>(finderContextParams);
    finderContextParamsWithOptionalString.put("optional", "someString");
    List<Parameter<?>> finderWithAssocKeyParams = new ArrayList<>();
    finderWithAssocKeyParams.add(new Parameter<>("string1", String.class, new StringDataSchema(), false, null, Parameter.ParamType.ASSOC_KEY_PARAM, true, new AnnotationSet(new Annotation[] {})));
    return new Object[][] { { getAllParams, getAllContextParams, null, new Object[] { new PagingContext(33, 444) } }, { getFinderParams(), finderContextParams, null, new Object[] { new PagingContext(33, 444), Integer.valueOf(777), null } }, { getFinderParams(), finderContextParamsWithOptionalString, null, new Object[] { new PagingContext(33, 444), Integer.valueOf(777), "someString" } }, { finderWithAssocKeyParams, null, new PathKeysImpl().append("string1", "testString"), new Object[] { "testString" } } };
}
Also used : StringDataSchema(com.linkedin.data.schema.StringDataSchema) HashMap(java.util.HashMap) PagingContext(com.linkedin.restli.server.PagingContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) DataProvider(org.testng.annotations.DataProvider)

Example 10 with Parameter

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

the class TestGetArgumentBuilder method testFailure.

@Test(dataProvider = "failureData")
public void testFailure(Parameter<?> param, String errorMessage) throws IOException {
    String keyName = "myComplexKeyCollectionId";
    Key key = new Key(keyName, Integer.class, new IntegerDataSchema());
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, true);
    List<Parameter<?>> paramList = Collections.<Parameter<?>>singletonList(param);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
    ServerResourceContext context = EasyMock.createMock(ServerResourceContext.class);
    MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
    EasyMock.expect(mockPathKeys.get(keyName)).andReturn(null).anyTimes();
    EasyMock.expect(context.getPathKeys()).andReturn(mockPathKeys).anyTimes();
    EasyMock.replay(context, mockPathKeys);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
    RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
    try {
        argumentBuilder.buildArguments(requestData, routingResult);
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertEquals(e.getMessage(), errorMessage);
    }
    verify(descriptor, context, routingResult, request);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) 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)

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