Search in sources :

Example 16 with ResourceModel

use of com.linkedin.restli.internal.server.model.ResourceModel 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 17 with ResourceModel

use of com.linkedin.restli.internal.server.model.ResourceModel 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 18 with ResourceModel

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

the class TestRestLiResourceModels method testTwitterStatusModel.

@Test
public void testTwitterStatusModel() throws Exception {
    ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
    checkCollectionModel(statusResourceModel, "statuses", Long.class, "statusID", Status.class, false, StatusCollectionResource.class);
    assertHasMethods(statusResourceModel, ResourceMethod.CREATE, ResourceMethod.FINDER, ResourceMethod.BATCH_GET);
    ResourceMethodDescriptor methodDescriptor;
    methodDescriptor = checkFinderMethod(statusResourceModel, "public_timeline", 1);
    methodDescriptor = checkFinderMethod(statusResourceModel, "user_timeline", 2);
    methodDescriptor = checkFinderMethod(statusResourceModel, "search", 3);
    checkParam(methodDescriptor, "keywords", String.class, null);
    checkParam(methodDescriptor, "since", long.class, -1L);
    checkParam(methodDescriptor, "type", StatusType.class, null);
    assertNull(methodDescriptor.getParameter("foo"));
    assertNull(statusResourceModel.findActionMethod("foo", ResourceLevel.COLLECTION));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.BATCH_GET));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.CREATE));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.BATCH_CREATE));
    checkEntityModel(statusResourceModel, Long.class, "statusID", Status.class, Collections.<String, Class<?>>emptyMap());
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.GET));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.FINDER));
    assertNotNull(statusResourceModel.findMethod(ResourceMethod.BATCH_UPDATE));
}
Also used : 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) Test(org.testng.annotations.Test)

Example 19 with ResourceModel

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

the class TestRestLiResourceModels method testAnnotatedCrudMethods.

@Test
public void testAnnotatedCrudMethods() throws Exception {
    // #1 Verify collection with annotated CRUD methods.
    ResourceModel collectionModelAnnotatedCrud = buildResourceModel(CombinedResources.CollectionWithAnnotatedCrudMethods.class);
    checkCollectionModel(collectionModelAnnotatedCrud, "test", String.class, "testId", Foo.class, false, CombinedResources.CollectionWithAnnotatedCrudMethods.class);
    checkEntityModel(collectionModelAnnotatedCrud, String.class, "testId", Foo.class, Collections.<String, Class<?>>emptyMap());
    assertHasMethods(collectionModelAnnotatedCrud, ResourceMethod.GET, ResourceMethod.CREATE, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.BATCH_GET, ResourceMethod.BATCH_CREATE, ResourceMethod.BATCH_DELETE, ResourceMethod.BATCH_UPDATE, ResourceMethod.BATCH_PARTIAL_UPDATE);
    // #2 Verify simple resource with annotated CRUD methods.
    ResourceModel simpleResourceModelAnnotatedCrud = buildResourceModel(CombinedResources.SimpleResourceWithAnnotatedCrudMethods.class);
    checkSimpleResourceModel(simpleResourceModelAnnotatedCrud, "test", Foo.class, false, CombinedResources.SimpleResourceWithAnnotatedCrudMethods.class);
    checkEntityModel(simpleResourceModelAnnotatedCrud, null, null, Foo.class, Collections.<String, Class<?>>emptyMap());
    assertHasMethods(simpleResourceModelAnnotatedCrud, ResourceMethod.GET, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.ACTION);
    // #3 Verify complex key collection with annotated CRUD methods.
    ResourceModel complexKeyCollectionModelAnnotatedCrud = buildResourceModel(CombinedResources.ComplexKeyResourceWithAnnotatedCrudMethods.class);
    checkCollectionModel(complexKeyCollectionModelAnnotatedCrud, "test", ComplexResourceKey.class, "testId", Foo.class, false, CombinedResources.ComplexKeyResourceWithAnnotatedCrudMethods.class);
    checkEntityModel(complexKeyCollectionModelAnnotatedCrud, ComplexResourceKey.class, "testId", Foo.class, Collections.<String, Class<?>>emptyMap());
    assertHasMethods(complexKeyCollectionModelAnnotatedCrud, ResourceMethod.GET, ResourceMethod.CREATE, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.BATCH_GET, ResourceMethod.BATCH_CREATE, ResourceMethod.BATCH_DELETE, ResourceMethod.BATCH_UPDATE, ResourceMethod.BATCH_PARTIAL_UPDATE);
}
Also used : CombinedResources(com.linkedin.restli.server.combined.CombinedResources) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Test(org.testng.annotations.Test)

Example 20 with ResourceModel

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

the class TestRestLiResourceModels method testExceptionMethods.

@Test
public void testExceptionMethods() throws Exception {
    final ResourceModel resourceModel = buildResourceModel(ExceptionsResource.class);
    assertEquals(resourceModel.getResourceType(), ResourceType.COLLECTION);
    assertEquals(resourceModel.getResourceMethodDescriptors().size(), 2);
    final ResourceMethodDescriptor getMethod = resourceModel.findMethod(ResourceMethod.GET);
    assertNotNull(getMethod);
    final ResourceMethodDescriptor actionMethod = resourceModel.findActionMethod("exception", ResourceLevel.COLLECTION);
    assertNotNull(actionMethod);
    final Class<?> returnClass = actionMethod.getActionReturnType();
    assertSame(returnClass, Integer.class);
}
Also used : 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) Test(org.testng.annotations.Test)

Aggregations

ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)223 Test (org.testng.annotations.Test)190 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)161 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)139 AfterTest (org.testng.annotations.AfterTest)114 BeforeTest (org.testng.annotations.BeforeTest)114 ByteString (com.linkedin.data.ByteString)50 PromiseStatusCollectionResource (com.linkedin.restli.server.twitter.PromiseStatusCollectionResource)50 CustomString (com.linkedin.restli.server.custom.types.CustomString)43 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)40 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)37 RestRequest (com.linkedin.r2.message.rest.RestRequest)36 Callback (com.linkedin.common.callback.Callback)34 RestLiConfig (com.linkedin.restli.server.RestLiConfig)34 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)32 RequestContext (com.linkedin.r2.message.RequestContext)29 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)28 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)28 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)28 TaskStatusCollectionResource (com.linkedin.restli.server.twitter.TaskStatusCollectionResource)28