Search in sources :

Example 86 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestRestLiServer method testPostProcessingException.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPostProcessingException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
    // request for nested projection within string field will generate error
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
    replay(statusResource);
    Callback<RestResponse> restResponseCallback = 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();
            try {
                assertEquals(restResponse.getStatus(), 500);
                assertTrue(restResponse.getEntity().length() > 0);
                assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
                EasyMock.verify(statusResource);
                EasyMock.reset(statusResource);
            } catch (Exception e2) {
                fail(e2.toString());
            }
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
        _server.handleRequest(request, new RequestContext(), restResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
        Callback<StreamResponse> callback = new Callback<StreamResponse>() {

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

            @Override
            public void onError(Throwable e) {
                Messages.toRestException((StreamException) e, new Callback<RestException>() {

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

                    @Override
                    public void onSuccess(RestException result) {
                        restResponseCallback.onError(result);
                    }
                });
            }
        };
        _server.handleRequest(streamRequest, new RequestContext(), callback);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestException(com.linkedin.r2.message.rest.RestException) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URISyntaxException(java.net.URISyntaxException) StreamException(com.linkedin.r2.message.stream.StreamException) ParseException(javax.mail.internet.ParseException) RestException(com.linkedin.r2.message.rest.RestException) IOException(java.io.IOException) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) 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) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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 87 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder 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(anyObject(FilterRequestContext.class));
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

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

            @Override
            public Object answer() throws Throwable {
                return CompletableFuture.completedFuture(null);
            }
        }).times(1);
        replay(_mockFilter);
    }
    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 88 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestRestLiServer method testHandleRequestWithRestLiResponseSuccess.

@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseSuccess(final RestOrStream restOrStream) throws Exception {
    Status status = new Status();
    status.data().put("test", "this is a test");
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(status).once();
    replay(statusResource);
    Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {

        @Override
        public void onSuccess(RestLiResponse restLiResponse) {
            assertEquals(restLiResponse.getDataMap(), status.data());
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }

        @Override
        public void onError(Throwable e) {
            fail("We should not get an error here. The server should have returned a 200!");
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
        _server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
        _server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
    }
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) 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) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 89 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestRestLiServer method testUnstructuredDataGetWithBody.

@Test
public void testUnstructuredDataGetWithBody() throws Exception {
    EntityStream streams = EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[] { 1, 2, 3, 4 })));
    Observer observer = new TestObserver();
    streams.addObserver(observer);
    final StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveGreetingCollectionUnstructuredData/hello")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(streams);
    final Callback<StreamResponse> callback = new Callback<StreamResponse>() {

        @Override
        public void onSuccess(StreamResponse streamResponse) {
        }

        @Override
        public void onError(Throwable e) {
        }
    };
    _server.handleRequest(streamRequest, new RequestContext(), callback);
    assertTrue(((TestObserver) observer).isDone());
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) Observer(com.linkedin.r2.message.stream.entitystream.Observer) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 90 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder 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)

Aggregations

StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)121 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)106 Test (org.testng.annotations.Test)102 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)87 URI (java.net.URI)68 RequestContext (com.linkedin.r2.message.RequestContext)56 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Callback (com.linkedin.common.callback.Callback)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ByteString (com.linkedin.data.ByteString)22 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)21 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)21 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)20 AfterTest (org.testng.annotations.AfterTest)19 BeforeTest (org.testng.annotations.BeforeTest)19 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 FutureCallback (com.linkedin.common.callback.FutureCallback)17