Search in sources :

Example 46 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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)

Example 47 with RoutingResult

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

the class TestBatchUpdateArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys) throws Exception {
    Set<Object> batchKeys = new HashSet<>(Arrays.asList(keys));
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
    @SuppressWarnings("rawtypes") Parameter<BatchUpdateRequest> param = new Parameter<>("", BatchUpdateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, Collections.singletonList(param), CollectionResourceAsyncTemplate.class.getMethod("batchUpdate", BatchUpdateRequest.class, Callback.class));
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, version, true, false);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
    RestLiArgumentBuilder argumentBuilder = new BatchUpdateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args.length, 1);
    assertTrue(args[0] instanceof BatchUpdateRequest);
    Map<?, ?> data = ((BatchUpdateRequest) args[0]).getData();
    assertEquals(data.size(), 2);
    MyComplexKey entity1 = (MyComplexKey) data.get(keys[0]);
    MyComplexKey entity2 = (MyComplexKey) data.get(keys[1]);
    assertEquals(entity1.getA(), "abc");
    assertEquals((long) entity1.getB(), 123L);
    assertEquals(entity2.getA(), "XY");
    assertEquals((long) entity2.getB(), 456L);
    verify(request, model, descriptor, context, routingResult);
}
Also used : MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) BatchUpdateRequest(com.linkedin.restli.server.BatchUpdateRequest) 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) HashSet(java.util.HashSet) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 48 with RoutingResult

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

the class TestBatchUpdateArgumentBuilder method testFailure.

@Test(dataProvider = "failureData")
public void testFailure(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys, String errorMessage) throws Exception {
    Set<Object> batchKeys = new HashSet<>(Arrays.asList(keys));
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, null, null, CollectionResourceAsyncTemplate.class.getMethod("batchUpdate", BatchUpdateRequest.class, Callback.class));
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, version, false, false);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
    RestLiArgumentBuilder argumentBuilder = new BatchUpdateArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertTrue(e.getMessage().contains(errorMessage));
        assertEquals(HttpStatus.S_400_BAD_REQUEST.getCode(), e.getStatus());
    }
    verify(request, model, descriptor, context, routingResult);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) BatchUpdateRequest(com.linkedin.restli.server.BatchUpdateRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) CollectionResourceAsyncTemplate(com.linkedin.restli.server.resources.CollectionResourceAsyncTemplate) 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) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 49 with RoutingResult

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

the class TestGetArgumentBuilder method testAsyncArguments.

@Test(dataProvider = "asyncArgumentData")
public void testAsyncArguments(List<Parameter<?>> paramList) throws IOException {
    String keyName = "myComplexKeyCollectionId";
    Object keyValue = Integer.valueOf(123);
    DataSchema keySchema = new IntegerDataSchema();
    Key key = new Key(keyName, keyValue.getClass(), keySchema);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, 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, null };
    assertEquals(args, expectedArgs);
    verify(model, descriptor, context, routingResult, request);
}
Also used : IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) 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) 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 50 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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

RoutingResult (com.linkedin.restli.internal.server.RoutingResult)114 Test (org.testng.annotations.Test)89 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)72 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)71 RestRequest (com.linkedin.r2.message.rest.RestRequest)56 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)28 RequestContext (com.linkedin.r2.message.RequestContext)24 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)20 URI (java.net.URI)20 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)19 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 RoutingException (com.linkedin.restli.server.RoutingException)18 HashMap (java.util.HashMap)17 DataMap (com.linkedin.data.DataMap)16 Foo (com.linkedin.pegasus.generator.examples.Foo)16 RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)14 Parameter (com.linkedin.restli.internal.server.model.Parameter)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 Callback (com.linkedin.common.callback.Callback)10