use of com.linkedin.restli.server.twitter.AsyncRepliesCollectionResource in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAsyncPost.
@SuppressWarnings("unchecked")
@Test
public void testAsyncPost() throws Exception {
Map<String, ResourceModel> resourceModelMap = buildResourceModels(AsyncStatusCollectionResource.class, AsyncRepliesCollectionResource.class, AsyncLocationResource.class, AsyncDiscoveredItemsResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/asyncstatuses");
ResourceModel repliesResourceModel = statusResourceModel.getSubResource("asyncreplies");
ResourceModel locationResourceModel = statusResourceModel.getSubResource("asynclocation");
ResourceModel discoveredItemsResourceModel = resourceModelMap.get("/asyncdiscovereditems");
RestLiCallback callback = getCallback();
ResourceMethodDescriptor methodDescriptor;
AsyncStatusCollectionResource statusResource;
AsyncRepliesCollectionResource repliesResource;
AsyncLocationResource locationResource;
AsyncDiscoveredItemsResource discoveredItemsResource;
// #1
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.CREATE);
statusResource = getMockResource(AsyncStatusCollectionResource.class);
statusResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(statusResource);
checkAsyncInvocation(statusResource, callback, methodDescriptor, "POST", version, "/asyncstatuses", "{}", null);
// #1.1: different endpoint
methodDescriptor = repliesResourceModel.findMethod(ResourceMethod.CREATE);
repliesResource = getMockResource(AsyncRepliesCollectionResource.class);
repliesResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(repliesResource);
checkAsyncInvocation(repliesResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1/replies", "{}", buildPathKeys("statusID", 1L));
// #2: Collection Partial Update
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
statusResource = getMockResource(AsyncStatusCollectionResource.class);
PatchTree p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(42));
PatchRequest<Status> expected = PatchRequest.createFromPatchDocument(p.getDataMap());
statusResource.update(eq(1L), eq(expected), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[2];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(statusResource);
checkAsyncInvocation(statusResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1", "{\"patch\":{\"$set\":{\"foo\":42}}}", buildPathKeys("statusID", 1L));
// #3: Simple Resource Partial Update
methodDescriptor = locationResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
locationResource = getMockResource(AsyncLocationResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(51));
PatchRequest<Location> expectedLocation = PatchRequest.createFromPatchDocument(p.getDataMap());
locationResource.update(eq(expectedLocation), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(locationResource);
checkAsyncInvocation(locationResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1/asynclocation", "{\"patch\":{\"$set\":{\"foo\":51}}}", buildPathKeys("statusID", 1L));
// #4 Complex-key resource create
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.CREATE);
discoveredItemsResource = getMockResource(AsyncDiscoveredItemsResource.class);
discoveredItemsResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(discoveredItemsResource);
checkAsyncInvocation(discoveredItemsResource, callback, methodDescriptor, "POST", version, "/asyncdiscovereditems", "{}", null);
// #5 Partial update on complex-key resource
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
discoveredItemsResource = getMockResource(AsyncDiscoveredItemsResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(43));
PatchRequest<DiscoveredItem> expectedDiscoveredItem = PatchRequest.createFromPatchDocument(p.getDataMap());
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> key = getDiscoveredItemComplexKey(1L, 2, 3L);
discoveredItemsResource.update(eq(key), eq(expectedDiscoveredItem), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[2];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(discoveredItemsResource);
checkAsyncInvocation(discoveredItemsResource, callback, methodDescriptor, "POST", version, "/asyncdiscovereditems/(itemId:1,type:2,userId:3)", "{\"patch\":{\"$set\":{\"foo\":43}}}", buildPathKeys("asyncDiscoveredItemId", key));
}
Aggregations