use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor 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((Status) EasyMock.anyObject(), EasyMock.<Callback<CreateResponse>>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((Status) EasyMock.anyObject(), (Callback<CreateResponse>) 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(Integer.valueOf(42)));
PatchRequest<Status> expected = PatchRequest.createFromPatchDocument(p.getDataMap());
statusResource.update(eq(1L), eq(expected), EasyMock.<Callback<UpdateResponse>>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(Integer.valueOf(51)));
PatchRequest<Location> expectedLocation = PatchRequest.createFromPatchDocument(p.getDataMap());
locationResource.update(eq(expectedLocation), EasyMock.<Callback<UpdateResponse>>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((DiscoveredItem) EasyMock.anyObject(), EasyMock.<Callback<CreateResponse>>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(Integer.valueOf(43)));
PatchRequest<DiscoveredItem> expectedDiscoveredItem = PatchRequest.createFromPatchDocument(p.getDataMap());
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> key = getDiscoveredItemComplexKey(1L, 2, 3L);
discoveredItemsResource.update(eq(key), eq(expectedDiscoveredItem), EasyMock.<Callback<UpdateResponse>>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));
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testFinderOptionalParam.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusFinderOptionalParam")
public void testFinderOptionalParam(ProtocolVersion version, String query) throws Exception {
ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("search");
StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.search(eq("linkedin"), eq(-1L), eq((StatusType) null))).andReturn(null).once();
checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses" + query);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testPagingContextUserTimelineStartAndCount.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusUserTimelineStartAndCount")
public void testPagingContextUserTimelineStartAndCount(ProtocolVersion version, String query) throws Exception {
ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("user_timeline");
StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.getUserTimeline(eq(true), eq(new PagingContext(0, 20, true, true)))).andReturn(null).once();
checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses" + query);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAction_BadParameterTypes.
@Test
public void testAction_BadParameterTypes() throws Exception {
ResourceModel accountsResourceModel = buildResourceModel(TwitterAccountsResource.class);
ResourceMethodDescriptor methodDescriptor;
// #1 no defaults provided
methodDescriptor = accountsResourceModel.findActionMethod("register", ResourceLevel.COLLECTION);
String jsonEntityBody = RestLiTestHelper.doubleQuote("{'first': 42, 'last': 42, 'email': 42, " + "'company': 42, 'openToMarketingEmails': 'false'}");
RestRequest request = new RestRequestBuilder(new URI("/accounts?action=register")).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());
try {
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
filterContext.setRequestData(_methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()).extractRequestData(routingResult, request));
_invoker.invoke(null, routingResult, _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()), null, null);
Assert.fail("expected routing exception");
} catch (RoutingException e) {
Assert.assertEquals(e.getStatus(), 400);
}
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testFinderOptionalBooleanParam.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusFinderOptionalBooleanParam")
public void testFinderOptionalBooleanParam(ProtocolVersion version, String query) throws Exception {
ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("user_timeline");
StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.getUserTimeline(eq(false), (PagingContext) EasyMock.anyObject())).andReturn(null).once();
checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses" + query);
}
Aggregations