Search in sources :

Example 71 with RestException

use of com.linkedin.r2.message.rest.RestException 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 72 with RestException

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

Example 73 with RestException

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

the class TestRestLiServer method testRestRequestResponseAttachmentsDesired.

@Test
public void testRestRequestResponseAttachmentsDesired() throws Exception {
    // This test verifies that a RestRequest sent to the RestLiServer throws an exception if the accept type
    // includes multipart related
    RestRequest acceptTypeMultiPartRelated = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_ACCEPT, 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(), 406);
            assertTrue(restResponse.getEntity().length() > 0);
            assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "This server cannot handle requests with an accept type of multipart/related");
        }
    };
    _server.handleRequest(acceptTypeMultiPartRelated, 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 74 with RestException

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

the class TestRestLiSymbolTableRequestHandler method testNonGetRequest405.

@Test
public void testNonGetRequest405() {
    RestRequest request = new RestRequestBuilder(URI.create("/symbolTable")).setMethod(HttpMethod.POST.name()).build();
    CompletableFuture<RestResponse> future = new CompletableFuture<>();
    _requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {

        @Override
        public void onError(Throwable e) {
            future.completeExceptionally(e);
            Assert.assertEquals(((RestException) e).getResponse().getStatus(), HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode());
        }

        @Override
        public void onSuccess(RestResponse result) {
            future.complete(result);
        }
    });
    Assert.assertTrue(future.isCompletedExceptionally());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 75 with RestException

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

the class TestRestLiSymbolTableRequestHandler method testInvalidAcceptTypeRequest406.

@Test
public void testInvalidAcceptTypeRequest406() {
    RestRequest request = new RestRequestBuilder(URI.create("/symbolTable")).setHeader(RestConstants.HEADER_ACCEPT, "application/randomType").build();
    CompletableFuture<RestResponse> future = new CompletableFuture<>();
    _requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {

        @Override
        public void onError(Throwable e) {
            future.completeExceptionally(e);
            Assert.assertEquals(((RestException) e).getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
        }

        @Override
        public void onSuccess(RestResponse result) {
            future.complete(result);
        }
    });
    Assert.assertTrue(future.isCompletedExceptionally());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

RestException (com.linkedin.r2.message.rest.RestException)70 RestResponse (com.linkedin.r2.message.rest.RestResponse)61 Test (org.testng.annotations.Test)51 RestRequest (com.linkedin.r2.message.rest.RestRequest)46 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)42 ExecutionException (java.util.concurrent.ExecutionException)30 URI (java.net.URI)24 RequestContext (com.linkedin.r2.message.RequestContext)23 Callback (com.linkedin.common.callback.Callback)19 ByteString (com.linkedin.data.ByteString)18 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)16 BeforeTest (org.testng.annotations.BeforeTest)15 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)14 URISyntaxException (java.net.URISyntaxException)14 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)13 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)12 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)12 AfterTest (org.testng.annotations.AfterTest)12 StreamException (com.linkedin.r2.message.stream.StreamException)11 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)9