Search in sources :

Example 66 with Callback

use of com.linkedin.common.callback.Callback in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testAsyncFinderOptionalParam.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusFinderOptionalParam")
public void testAsyncFinderOptionalParam(ProtocolVersion version, String query) throws Exception {
    RestLiCallback<?> callback = getCallback();
    ResourceModel statusResourceModel = buildResourceModel(AsyncStatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("search");
    AsyncStatusCollectionResource statusResource = getMockResource(AsyncStatusCollectionResource.class);
    statusResource.search((PagingContext) EasyMock.anyObject(), eq("linkedin"), eq(-1L), eq((StatusType) null), EasyMock.<Callback<List<Status>>>anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<List<Status>> callback = (Callback<List<Status>>) EasyMock.getCurrentArguments()[4];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(statusResource);
    checkAsyncInvocation(statusResource, callback, methodDescriptor, "GET", version, "/asyncstatuses" + query, null);
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RequestExecutionCallback(com.linkedin.restli.server.RequestExecutionCallback) StatusType(com.linkedin.restli.server.twitter.TwitterTestDataModels.StatusType) 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) List(java.util.List) EasyMock.anyObject(org.easymock.EasyMock.anyObject) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 67 with Callback

use of com.linkedin.common.callback.Callback in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testAsyncBatchPatchAssociativeResource.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchUpdateCompoundKey")
public void testAsyncBatchPatchAssociativeResource(ProtocolVersion version, String uri, String body) throws Exception {
    ResourceModel followsResourceModel = buildResourceModel(AsyncFollowsAssociativeResource.class);
    RestLiCallback<?> callback = getCallback();
    ResourceMethodDescriptor methodDescriptor;
    AsyncFollowsAssociativeResource resource;
    methodDescriptor = followsResourceModel.findMethod(ResourceMethod.BATCH_PARTIAL_UPDATE);
    resource = getMockResource(AsyncFollowsAssociativeResource.class);
    @SuppressWarnings("unchecked") BatchPatchRequest<CompoundKey, Followed> mockBatchPatchReq = (BatchPatchRequest<CompoundKey, Followed>) EasyMock.anyObject();
    resource.batchUpdate(mockBatchPatchReq, EasyMock.<Callback<BatchUpdateResult<CompoundKey, Followed>>>anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<BatchUpdateResult<CompoundKey, Followed>> callback = (Callback<BatchUpdateResult<CompoundKey, Followed>>) EasyMock.getCurrentArguments()[1];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(resource);
    checkAsyncInvocation(resource, callback, methodDescriptor, "POST", version, uri, body, buildBatchPathKeys(buildFollowsCompoundKey(1L, 2L), buildFollowsCompoundKey(3L, 4L), buildFollowsCompoundKey(5L, 6L)));
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Followed(com.linkedin.restli.server.twitter.TwitterTestDataModels.Followed) AsyncFollowsAssociativeResource(com.linkedin.restli.server.twitter.AsyncFollowsAssociativeResource) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RequestExecutionCallback(com.linkedin.restli.server.RequestExecutionCallback) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 68 with Callback

use of com.linkedin.common.callback.Callback in project rest.li by linkedin.

the class TestMultiplexedCallback method testSuccess.

@Test
public void testSuccess() throws Exception {
    FutureCallback<RestResponse> callback1 = new FutureCallback<RestResponse>();
    FutureCallback<RestResponse> callback2 = new FutureCallback<RestResponse>();
    ImmutableMap<Integer, Callback<RestResponse>> individualCallbacks = ImmutableMap.<Integer, Callback<RestResponse>>of(ID1, callback1, ID2, callback2);
    FutureCallback<MultiplexedResponse> aggregatedCallback = new FutureCallback<MultiplexedResponse>();
    TestRecord entity1 = fakeEntity(ID1);
    IndividualResponse ir1 = fakeIndividualResponse(entity1);
    TestRecord entity2 = fakeEntity(ID2);
    IndividualResponse ir2 = fakeIndividualResponse(entity2);
    MultiplexedResponseContent responseContent = new MultiplexedResponseContent().setResponses(new IndividualResponseMap(ImmutableMap.of(Integer.toString(ID1), ir1, Integer.toString(ID2), ir2)));
    MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
    multiplexedCallback.onSuccess(fakeRestResponse(responseContent));
    assertRestResponseEquals(callback1.get(), fakeRestResponse(entity1));
    assertRestResponseEquals(callback2.get(), fakeRestResponse(entity2));
    MultiplexedResponse multiplexedResponse = aggregatedCallback.get();
    Assert.assertEquals(multiplexedResponse.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(multiplexedResponse.getHeaders(), HEADERS);
}
Also used : MultiplexedResponseContent(com.linkedin.restli.common.multiplexer.MultiplexedResponseContent) RestResponse(com.linkedin.r2.message.rest.RestResponse) IndividualResponse(com.linkedin.restli.common.multiplexer.IndividualResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) TestRecord(com.linkedin.restli.client.test.TestRecord) IndividualResponseMap(com.linkedin.restli.common.multiplexer.IndividualResponseMap) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 69 with Callback

use of com.linkedin.common.callback.Callback in project rest.li by linkedin.

the class TestGetArgumentBuilder method asyncArgumentData.

@DataProvider(name = "asyncArgumentData")
private Object[][] asyncArgumentData() {
    List<Parameter<?>> callbackParams = new ArrayList<Parameter<?>>();
    callbackParams.add(getIntegerParam());
    @SuppressWarnings("rawtypes") Parameter<Callback> cParam = new Parameter<Callback>("", Callback.class, null, false, null, Parameter.ParamType.CALLBACK, false, new AnnotationSet(new Annotation[] {}));
    callbackParams.add(cParam);
    List<Parameter<?>> parSeqContextParams = new ArrayList<Parameter<?>>();
    parSeqContextParams.add(getIntegerParam());
    parSeqContextParams.add(new Parameter<Context>("", Context.class, null, false, null, Parameter.ParamType.PARSEQ_CONTEXT_PARAM, false, new AnnotationSet(new Annotation[] {})));
    List<Parameter<?>> deprecatedParSeqContextParams = new ArrayList<Parameter<?>>();
    deprecatedParSeqContextParams.add(getIntegerParam());
    @SuppressWarnings("deprecation") Parameter<Context> contextParam = new Parameter<Context>("", Context.class, null, false, null, Parameter.ParamType.PARSEQ_CONTEXT, false, new AnnotationSet(new Annotation[] {}));
    deprecatedParSeqContextParams.add(contextParam);
    return new Object[][] { { callbackParams }, { parSeqContextParams }, { deprecatedParSeqContextParams } };
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) Context(com.linkedin.parseq.Context) Callback(com.linkedin.common.callback.Callback) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) DataProvider(org.testng.annotations.DataProvider)

Example 70 with Callback

use of com.linkedin.common.callback.Callback in project rest.li by linkedin.

the class TestRestLiServer method testRestRequestAttachmentsPresent.

@Test
public void testRestRequestAttachmentsPresent() throws Exception {
    //This test verifies that a RestRequest sent to the RestLiServer throws an exception if the content type is multipart/related
    RestRequest contentTypeMultiPartRelated = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build();
    Callback<RestResponse> callback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            fail();
        }

        @Override
        public void onError(Throwable e) {
            assertTrue(e instanceof RestException);
            RestException restException = (RestException) e;
            RestResponse restResponse = restException.getResponse();
            assertEquals(restResponse.getStatus(), 415);
            assertTrue(restResponse.getEntity().length() > 0);
            assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "This server cannot handle requests with a content type of multipart/related");
        }
    };
    _server.handleRequest(contentTypeMultiPartRelated, new RequestContext(), callback);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

Callback (com.linkedin.common.callback.Callback)95 Test (org.testng.annotations.Test)64 AfterTest (org.testng.annotations.AfterTest)43 BeforeTest (org.testng.annotations.BeforeTest)43 RequestContext (com.linkedin.r2.message.RequestContext)37 RestResponse (com.linkedin.r2.message.rest.RestResponse)34 ByteString (com.linkedin.data.ByteString)29 RestRequest (com.linkedin.r2.message.rest.RestRequest)28 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)28 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)28 URI (java.net.URI)28 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)26 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)26 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)26 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)26 RequestExecutionCallback (com.linkedin.restli.server.RequestExecutionCallback)26 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)26 EasyMock.anyObject (org.easymock.EasyMock.anyObject)26 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)25 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)22