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);
}
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);
}
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));
}
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);
}
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);
}
Aggregations