Search in sources :

Example 81 with Callback

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

the class TestRestLiServer method testDebugRequestHandlers.

@Test(dataProvider = "restOrStream")
public void testDebugRequestHandlers(final RestOrStream restOrStream) throws URISyntaxException {
    //Without a resource
    final Callback<RestResponse> noResourceRestResponseCallback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            assertEquals(restResponse.getStatus(), 200);
            String responseString = restResponse.getEntity().asString(Charset.defaultCharset());
            Assert.assertEquals(responseString, DEBUG_HANDLER_RESPONSE_A);
        }

        @Override
        public void onError(Throwable e) {
            fail();
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1/__debug/a/s")).build();
        _server.handleRequest(request, new RequestContext(), noResourceRestResponseCallback);
    } else {
        StreamRequest request = new StreamRequestBuilder(new URI("/statuses/1/__debug/a/s")).build(EntityStreams.emptyStream());
        Callback<StreamResponse> callback = new Callback<StreamResponse>() {

            @Override
            public void onSuccess(StreamResponse streamResponse) {
                Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        Assert.fail();
                    }

                    @Override
                    public void onSuccess(RestResponse result) {
                        noResourceRestResponseCallback.onSuccess(result);
                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                fail();
            }
        };
        _server.handleRequest(request, new RequestContext(), callback);
    }
    //With a resource this time
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
    EasyMock.replay(statusResource);
    final Callback<RestResponse> resourceRestResponseCallback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            assertEquals(restResponse.getStatus(), 200);
            String responseString = restResponse.getEntity().asString(Charset.defaultCharset());
            Assert.assertEquals(responseString, DEBUG_HANDLER_RESPONSE_B);
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }

        @Override
        public void onError(Throwable e) {
            fail();
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1/__debug/b")).build();
        _server.handleRequest(request, new RequestContext(), resourceRestResponseCallback);
    } else {
        StreamRequest request = new StreamRequestBuilder(new URI("/statuses/1/__debug/b")).build(EntityStreams.emptyStream());
        Callback<StreamResponse> callback = new Callback<StreamResponse>() {

            @Override
            public void onSuccess(StreamResponse streamResponse) {
                Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        Assert.fail();
                    }

                    @Override
                    public void onSuccess(RestResponse result) {
                        resourceRestResponseCallback.onSuccess(result);
                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                fail();
            }
        };
        _server.handleRequest(request, new RequestContext(), callback);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 82 with Callback

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

the class TestRestLiServer method testValidRequest.

private void testValidRequest(RestLiServer restLiServer, final ProtocolVersion clientProtocolVersion, boolean filters, final String headerConstant, final RestOrStream restOrStream) throws URISyntaxException {
    RestRequest request = null;
    StreamRequest streamRequest = null;
    if (clientProtocolVersion != null) {
        if (restOrStream == RestOrStream.REST) {
            request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build();
        } else {
            streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
        }
    } else {
        if (restOrStream == RestOrStream.REST) {
            request = new RestRequestBuilder(new URI("/statuses/1")).build();
        } else {
            streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).build(EntityStreams.emptyStream());
        }
    }
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
    if (filters) {
        _mockFilter.onRequest(EasyMock.anyObject(FilterRequestContext.class));
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                return CompletableFuture.completedFuture(null);
            }
        }).times(1);
        _mockFilter.onResponse(EasyMock.anyObject(FilterRequestContext.class), EasyMock.anyObject(FilterResponseContext.class));
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                return CompletableFuture.completedFuture(null);
            }
        }).times(1);
        EasyMock.replay(_mockFilter);
    }
    EasyMock.replay(statusResource);
    final Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse restResponse) {
            assertEquals(restResponse.getStatus(), 200);
            assertTrue(restResponse.getEntity().length() > 0);
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
            if (clientProtocolVersion != null) {
                assertEquals(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, headerConstant, "Rest.li protocol header name is unexpected.");
            }
        }

        @Override
        public void onError(Throwable e) {
            fail();
        }
    };
    if (restOrStream == RestOrStream.REST) {
        restLiServer.handleRequest(request, new RequestContext(), restResponseCallback);
    } else {
        Callback<StreamResponse> streamResponseCallback = new Callback<StreamResponse>() {

            @Override
            public void onSuccess(StreamResponse streamResponse) {
                Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        Assert.fail();
                    }

                    @Override
                    public void onSuccess(RestResponse result) {
                        restResponseCallback.onSuccess(result);
                    }
                });
            }

            @Override
            public void onError(Throwable e) {
                fail();
            }
        };
        restLiServer.handleRequest(streamRequest, new RequestContext(), streamResponseCallback);
    }
    if (filters) {
        EasyMock.verify(_mockFilter, _mockFilter);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) IAnswer(org.easymock.IAnswer) 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) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext)

Example 83 with Callback

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

the class TestRestLiServer method testStreamRequestMultiplexedRequestMultiPartAcceptType.

@Test
public void testStreamRequestMultiplexedRequestMultiPartAcceptType() throws Exception {
    //This test verifies that a StreamRequest sent to the RestLiServer throws an exception if the accept type contains
    //multipart/related.
    StreamRequest streamRequestMux = new StreamRequestBuilder(new URI("/mux")).setHeader(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build(EntityStreams.emptyStream());
    Callback<StreamResponse> callback = new Callback<StreamResponse>() {

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

        @Override
        public void onError(Throwable e) {
            assertTrue(e instanceof StreamException);
            StreamException streamException = (StreamException) e;
            StreamResponse streamResponse = streamException.getResponse();
            assertEquals(streamResponse.getStatus(), 406);
            final FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {

                @Override
                public void onError(Throwable e) {
                    Assert.fail();
                }

                @Override
                public void onSuccess(ByteString result) {
                    //We have the body so assert
                    assertTrue(result.length() > 0);
                    assertEquals(result.asString(Charset.defaultCharset()), "This server cannot handle multiplexed requests that have an accept type of multipart/related");
                }
            });
            streamResponse.getEntityStream().setReader(fullEntityReader);
        }
    };
    _server.handleRequest(streamRequestMux, new RequestContext(), callback);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 84 with Callback

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

the class TestRestLiServer method testRestRequestAttemptVerifyParseFailed.

@Test
public void testRestRequestAttemptVerifyParseFailed() throws Exception {
    //This test verifies that a RestRequest sent to the RestLiServer throws an exception if the content type or accept types
    //fail to parse properly. This occurs when we try to verify that the request's content type or accept types do
    //not include multipart/related.
    RestRequest invalidContentTypeRequest = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_CONTENT_TYPE, "©").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(), 400);
            assertTrue(restResponse.getEntity().length() > 0);
            assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "Unable to parse content or accept types.");
        }
    };
    _server.handleRequest(invalidContentTypeRequest, 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)

Example 85 with Callback

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

the class GreetingsResourceCodeGenerator method printArgumentList.

private void printArgumentList(final PrintStream out, final Method method) {
    final Type[] argTypes = method.getGenericParameterTypes();
    final Annotation[][] argAnnos = method.getParameterAnnotations();
    boolean first = true;
    for (int i = 0; i < argTypes.length; i++) {
        final Type argType = argTypes[i];
        if (argType == BaseResource.class)
            continue;
        if (!first)
            out.print(", ");
        first = false;
        for (final Annotation anno : argAnnos[i]) out.print(toStrAnnotation(anno) + " ");
        out.printf("final %s %s", toStrGenericType(argType), (char) ('a' + i));
    }
    if (_type == InterfaceType.CALLBACK) {
        final Type returnType = method.getGenericReturnType();
        if (!first)
            out.print(", ");
        out.printf("@%s final %s callback", className(CallbackParam.class), genericWrapper(Callback.class, returnType));
    } else if (_useParSeqCtx) {
        if (!first)
            out.print(", ");
        out.printf("@%s final %s psContext", className(ParSeqContextParam.class), className(Context.class));
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) InterfaceType(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CallbackParam(com.linkedin.restli.server.annotations.CallbackParam) Callback(com.linkedin.common.callback.Callback) Annotation(java.lang.annotation.Annotation)

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