Search in sources :

Example 1 with RestException

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

the class RestClientTest method testRestLiRemoteInvocationException.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiRemoteInvocationException(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
    final int HTTP_CODE = 404;
    final String ERR_MSG = "WHOOPS!";
    RestClient client = mockClient(HTTP_CODE, ERR_MSG, protocolVersion);
    Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
    RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
    FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
    try {
        sendRequest(option, client, request, requestBuilder, callback);
        Long l = timeoutOption._l;
        TimeUnit timeUnit = timeoutOption._timeUnit;
        Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
        Assert.fail("Should have thrown");
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        Assert.assertTrue(cause instanceof RemoteInvocationException, "Expected RemoteInvocationException not " + cause.getClass().getName());
        RemoteInvocationException rlre = (RemoteInvocationException) cause;
        Assert.assertTrue(rlre.getMessage().startsWith("Received error " + HTTP_CODE + " from server"));
        Throwable rlCause = rlre.getCause();
        Assert.assertTrue(rlCause instanceof RestException, "Excepted RestException not " + rlCause.getClass().getName());
        RestException rle = (RestException) rlCause;
        Assert.assertEquals(ERR_MSG, rle.getResponse().getEntity().asString("UTF-8"));
        Assert.assertEquals(HTTP_CODE, rle.getResponse().getStatus());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 2 with RestException

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

the class RestClientTest method testRestLiResponseExceptionCallback.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiResponseExceptionCallback(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
    final String ERR_KEY = "someErr";
    final String ERR_VALUE = "WHOOPS!";
    final String ERR_MSG = "whoops2";
    final int HTTP_CODE = 400;
    final int APP_CODE = 666;
    RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
    Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
    RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
    FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
    try {
        sendRequest(option, client, request, requestBuilder, callback);
        Long l = timeoutOption._l;
        TimeUnit timeUnit = timeoutOption._timeUnit;
        Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
        Assert.fail("Should have thrown");
    } catch (ExecutionException e) {
        // New
        Throwable cause = e.getCause();
        Assert.assertTrue(cause instanceof RestLiResponseException, "Expected RestLiResponseException not " + cause.getClass().getName());
        RestLiResponseException rlre = (RestLiResponseException) cause;
        Assert.assertEquals(HTTP_CODE, rlre.getStatus());
        Assert.assertEquals(ERR_VALUE, rlre.getErrorDetails().get(ERR_KEY));
        Assert.assertEquals(APP_CODE, rlre.getServiceErrorCode());
        Assert.assertEquals(ERR_MSG, rlre.getServiceErrorMessage());
        // Old
        Assert.assertTrue(cause instanceof RestException, "Expected RestException not " + cause.getClass().getName());
        RestException re = (RestException) cause;
        RestResponse r = re.getResponse();
        ErrorResponse er = new EntityResponseDecoder<ErrorResponse>(ErrorResponse.class).decodeResponse(r).getEntity();
        Assert.assertEquals(HTTP_CODE, r.getStatus());
        Assert.assertEquals(ERR_VALUE, er.getErrorDetails().data().getString(ERR_KEY));
        Assert.assertEquals(APP_CODE, er.getServiceErrorCode().intValue());
        Assert.assertEquals(ERR_MSG, er.getMessage());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with RestException

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

the class TestServerTimeout method testServerThrowButShouldNotTimeout.

@Test
public void testServerThrowButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, THROW_BUT_SHOULD_NOT_TIMEOUT_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Server throw for test."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 4 with RestException

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

the class TestServerTimeout method testFilterThrowButShouldNotTimeout.

@Test
public void testFilterThrowButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_FILTER_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Buggy filter throws."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 5 with RestException

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

the class TestServerTimeoutAsyncEvent method testServerThrowButShouldNotTimeout.

@Test
public void testServerThrowButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, THROW_BUT_SHOULD_NOT_TIMEOUT_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(ASYNC_EVENT_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Server throw for test."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) 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