use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testBatchPatchComplexKey.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchUpdateComplexKey")
public void testBatchPatchComplexKey(ProtocolVersion version, String uri, String body) throws Exception {
ResourceModel discoveredItemsResourceModel = buildResourceModel(DiscoveredItemsResource.class);
ResourceMethodDescriptor methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.BATCH_PARTIAL_UPDATE);
DiscoveredItemsResource discoveredItemsResource = getMockResource(DiscoveredItemsResource.class);
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> keyA = getDiscoveredItemComplexKey(1L, 2, 3L);
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> keyB = getDiscoveredItemComplexKey(4L, 5, 6L);
BatchPatchRequest<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>, DiscoveredItem> batchPatchRequest = EasyMock.anyObject();
@SuppressWarnings("unchecked") BatchUpdateResult<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>, DiscoveredItem> batchUpdateResult = discoveredItemsResource.batchUpdate(batchPatchRequest);
EasyMock.expect(batchUpdateResult).andReturn(null).once();
checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, uri, body, buildBatchPathKeys(keyA, keyB));
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testCustomTypeParametersCustomLongArray.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "customLongArray")
public void testCustomTypeParametersCustomLongArray(ProtocolVersion version, String uri) throws Exception {
ResourceModel repliesResourceModel = buildResourceModel(RepliesCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = repliesResourceModel.findNamedMethod("customLongArray");
RepliesCollectionResource repliesResource = getMockResource(RepliesCollectionResource.class);
CustomLong[] longs = { new CustomLong(100L), new CustomLong(200L) };
repliesResource.customLongArray(EasyMock.aryEq(longs));
EasyMock.expectLastCall().andReturn(null).once();
checkInvocation(repliesResource, methodDescriptor, "GET", version, uri);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testActionsOnResource.
@Test
public void testActionsOnResource() throws Exception {
ResourceModel repliesResourceModel = buildResourceModel(RepliesCollectionResource.class);
ResourceModel locationResourceModel = buildResourceModel(LocationResource.class);
ResourceModel discoveredItemsResourceModel = buildResourceModel(DiscoveredItemsResource.class);
ResourceMethodDescriptor methodDescriptor;
RepliesCollectionResource repliesResource;
LocationResource locationResource;
DiscoveredItemsResource discoveredItemsResource;
// #1 Action on collection resource
methodDescriptor = repliesResourceModel.findActionMethod("replyToAll", ResourceLevel.COLLECTION);
repliesResource = getMockResource(RepliesCollectionResource.class);
repliesResource.replyToAll("hello");
EasyMock.expectLastCall().once();
String jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
MutablePathKeys pathKeys = new PathKeysImpl();
pathKeys.append("statusID", 1L);
checkInvocation(repliesResource, methodDescriptor, "POST", version, "/statuses/1/replies?action=replyToAll", jsonEntityBody, pathKeys);
// #2 Action on simple resource
methodDescriptor = locationResourceModel.findActionMethod("new_status_from_location", ResourceLevel.ENTITY);
locationResource = getMockResource(LocationResource.class);
locationResource.newStatusFromLocation(eq("hello"));
EasyMock.expectLastCall().once();
jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
pathKeys = new PathKeysImpl();
pathKeys.append("statusID", 1L);
checkInvocation(locationResource, methodDescriptor, "POST", version, "/statuses/1/location?action=new_status_from_location", jsonEntityBody, pathKeys);
// #3 Action on complex-key resource
methodDescriptor = discoveredItemsResourceModel.findActionMethod("purge", ResourceLevel.COLLECTION);
discoveredItemsResource = getMockResource(DiscoveredItemsResource.class);
discoveredItemsResource.purge(12L);
EasyMock.expectLastCall().once();
jsonEntityBody = RestLiTestHelper.doubleQuote("{'user': 12}");
checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, "/discovereditems/action=purge", jsonEntityBody, buildPathKeys());
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testPromisePost.
@Test
public void testPromisePost() throws Exception {
Map<String, ResourceModel> resourceModelMap = buildResourceModels(PromiseStatusCollectionResource.class, PromiseRepliesCollectionResource.class, PromiseLocationResource.class, PromiseDiscoveredItemsResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/promisestatuses");
ResourceModel repliesResourceModel = statusResourceModel.getSubResource("promisereplies");
ResourceModel locationResourceModel = statusResourceModel.getSubResource("promiselocation");
ResourceModel discoveredItemsResourceModel = resourceModelMap.get("/promisediscovereditems");
ResourceMethodDescriptor methodDescriptor;
PromiseStatusCollectionResource statusResource;
PromiseRepliesCollectionResource repliesResource;
PromiseLocationResource locationResource;
PromiseDiscoveredItemsResource discoveredItemsResource;
// #1
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.CREATE);
statusResource = getMockResource(PromiseStatusCollectionResource.class);
EasyMock.expect(statusResource.create((Status) EasyMock.anyObject())).andReturn(Promises.<CreateResponse>value(null)).once();
checkInvocation(statusResource, methodDescriptor, "POST", version, "/promisestatuses", "{}");
// #1.1: different endpoint
methodDescriptor = repliesResourceModel.findMethod(ResourceMethod.CREATE);
repliesResource = getMockResource(PromiseRepliesCollectionResource.class);
EasyMock.expect(repliesResource.create((Status) EasyMock.anyObject())).andReturn(Promises.<CreateResponse>value(null)).once();
checkInvocation(repliesResource, methodDescriptor, "POST", version, "/promisestatuses/1/replies", "{}", buildPathKeys("statusID", 1L));
// #1.2: invalid entity
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.CREATE);
statusResource = getMockResource(PromiseStatusCollectionResource.class);
EasyMock.expect(statusResource.create((Status) EasyMock.anyObject())).andReturn(Promises.<CreateResponse>value(null)).once();
try {
checkInvocation(statusResource, methodDescriptor, "POST", version, "/promisestatuses", "{");
fail("Expected exception");
} catch (RoutingException e) {
// expected
EasyMock.reset(statusResource);
}
// #2: Collection Partial Update
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
statusResource = getMockResource(PromiseStatusCollectionResource.class);
PatchTree p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(Integer.valueOf(42)));
PatchRequest<Status> expected = PatchRequest.createFromPatchDocument(p.getDataMap());
EasyMock.expect(statusResource.update(eq(1L), eq(expected))).andReturn(Promises.<UpdateResponse>value(null)).once();
checkInvocation(statusResource, methodDescriptor, "POST", version, "/promisestatuses/1", "{\"patch\":{\"$set\":{\"foo\":42}}}", buildPathKeys("statusID", 1L));
// #3: Simple Resource Partial Update
methodDescriptor = locationResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
locationResource = getMockResource(PromiseLocationResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(Integer.valueOf(51)));
PatchRequest<Location> expectedLocation = PatchRequest.createFromPatchDocument(p.getDataMap());
EasyMock.expect(locationResource.update(eq(expectedLocation))).andReturn(Promises.<UpdateResponse>value(null)).once();
checkInvocation(locationResource, methodDescriptor, "POST", version, "/promisestatuses/1/promiselocation", "{\"patch\":{\"$set\":{\"foo\":51}}}", buildPathKeys("statusID", 1L));
// #4 Complex key resource create
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.CREATE);
discoveredItemsResource = getMockResource(PromiseDiscoveredItemsResource.class);
EasyMock.expect(discoveredItemsResource.create((DiscoveredItem) EasyMock.anyObject())).andReturn(Promises.<CreateResponse>value(null)).once();
checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, "/promisediscovereditems", "{}");
// #5 Complex key resource partial update
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
discoveredItemsResource = getMockResource(PromiseDiscoveredItemsResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(Integer.valueOf(43)));
PatchRequest<DiscoveredItem> expectedDiscoveredItem = PatchRequest.createFromPatchDocument(p.getDataMap());
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> key = getDiscoveredItemComplexKey(1L, 2, 3L);
EasyMock.expect(discoveredItemsResource.update(eq(key), eq(expectedDiscoveredItem))).andReturn(Promises.<UpdateResponse>value(null)).once();
checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, "/promisediscovereditems/(itemId:1,type:2,userId:3)", "{\"patch\":{\"$set\":{\"foo\":43}}}", buildPathKeys("promiseDiscoveredItemId", key));
// TODO would be nice to verify that posting an invalid record type fails
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAction_BadArrayElements.
@Test
public void testAction_BadArrayElements() throws Exception {
ResourceModel accountsResourceModel = buildResourceModel(TwitterAccountsResource.class);
ResourceMethodDescriptor methodDescriptor;
// #1 no defaults provided
methodDescriptor = accountsResourceModel.findActionMethod("spamTweets", ResourceLevel.COLLECTION);
String jsonEntityBody = RestLiTestHelper.doubleQuote("{'statuses':[1,2,3]}");
RestRequest request = new RestRequestBuilder(new URI("/accounts?action=spamTweets")).setMethod("POST").setEntity(jsonEntityBody.getBytes(Data.UTF_8_CHARSET)).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString()).build();
RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(null, request, new RequestContext()), methodDescriptor);
final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), routingResult.getResourceMethod());
RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getType());
try {
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
_invoker.invoke(filterContext.getRequestData(), routingResult, _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()), null, null);
Assert.fail("expected routing exception");
} catch (RoutingException e) {
Assert.assertEquals(e.getStatus(), 400);
}
}
Aggregations