Search in sources :

Example 36 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestExceptionsResource method testException.

@SuppressWarnings("deprecation")
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testException(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Response<Greeting> response = null;
    RestLiResponseException exception = null;
    try {
        Request<Greeting> readRequest = builders.get().id(1L).build();
        ResponseFuture<Greeting> future;
        if (explicit) {
            future = getClient().sendRequest(readRequest, errorHandlingBehavior);
        } else {
            future = getClient().sendRequest(readRequest);
        }
        response = future.getResponse();
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            Assert.fail("expected exception");
        }
    } catch (RestLiResponseException e) {
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            exception = e;
        } else {
            Assert.fail("not expected exception");
        }
    }
    if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
        Assert.assertNotNull(response);
        Assert.assertTrue(response.hasError());
        exception = response.getError();
        Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
        Assert.assertNull(response.getEntity());
    }
    Assert.assertNotNull(exception);
    Assert.assertFalse(exception.hasDecodedResponse());
    Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
    Assert.assertEquals(exception.getServiceErrorCode(), 42);
    Assert.assertEquals(exception.getServiceErrorMessage(), "error processing request");
    Assert.assertTrue(exception.getServiceErrorStackTrace().contains("at com.linkedin.restli.examples.greetings.server.ExceptionsResource.get("));
    Assert.assertEquals(exception.getCode(), "PROCESSING_ERROR");
    Assert.assertEquals(exception.getDocUrl(), "https://example.com/errors/processing-error");
    Assert.assertEquals(exception.getRequestId(), "xyz123");
    Assert.assertEquals(exception.getErrorDetailType(), Greeting.class.getCanonicalName());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 37 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestGreetingsClient method getBatchTestDataSerially.

/**
 * Fetches Greetings from the server serially.
 *
 * @param idsToGet the ids for the Greetings to fetch
 *
 * @return the fetched Greetings
 * @throws RemoteInvocationException
 */
private List<Greeting> getBatchTestDataSerially(RootBuilderWrapper<Long, Greeting> builders, List<Long> idsToGet) throws RemoteInvocationException {
    List<Greeting> fetchedGreetings = new ArrayList<>();
    for (int i = 0; i < idsToGet.size(); i++) {
        try {
            Long id = idsToGet.get(i);
            Request<Greeting> request = builders.get().id(id).build();
            ResponseFuture<Greeting> future = getClient().sendRequest(request);
            Response<Greeting> greetingResponse = future.getResponse();
            Greeting fetchedGreeting = greetingResponse.getEntity();
            fetchedGreetings.add(fetchedGreeting);
        } catch (RestLiResponseException ex) {
            if (ex.getStatus() == HttpStatus.S_404_NOT_FOUND.getCode()) {
                fetchedGreetings.add(null);
            } else {
                throw ex;
            }
        }
    }
    return fetchedGreetings;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ArrayList(java.util.ArrayList) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException)

Example 38 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestFilters method testGetOldBuilders.

/**
 * This is a simple test that verifies the behavior of request and response filters. This test
 * hooks up two filters, one request filter and one response filter to the greeting resource.
 *
 * The behavior of the request filter is such that if the incoming request is of type create, the
 * filter modifies the incoming create request as follows:
 *
 * 1. If the tone of the incoming greeting is friendly, the filter modifies it to sincere.
 *
 * 2. If the tone of the incoming greeting is sincere, the filter modifies it to insulting.
 *
 * 3. If the tone of the incoming greeting is insulting, the filter throws an exception saying
 * that creation of a greeting with an insulting tone is not permitted. The HTTP status code is
 * set to 403.
 *
 * The behavior of the response filter is as follows:
 *
 * 1. If the response is an error, and the HTTP status code is 403, the filter updates the
 * outgoing error message and sets the status code to 400.
 *
 * 2. If the response is not an error, and the incoming request is a get, then the response filter
 * modifies the tone of the outgoing greeting message as follows:
 *
 * a. If the tone of the outgoing greeting from the resource is sincere, the filter modifies it to
 * friendly.
 *
 * b. If the tone of the outgoing greeting from the resource is insulting, the filter modifies it
 * to sincere.
 *
 * @param builders type of request builder.
 * @param tone tone of the greeting to be created.
 * @param responseFilter flag indicating whether or not the response filter is to be hooked up. NOTE: The
 *        request filter is always hooked up.
 * @param responseFilterException the exception the response filter will throw.
 * @throws Exception if anything unexpected happens.
 */
@Test(dataProvider = "requestBuilderDataProvider")
public void testGetOldBuilders(RootBuilderWrapper<Long, Greeting> builders, Tone tone, boolean responseFilter, RuntimeException responseFilterException) throws Exception {
    setupFilters(responseFilter, responseFilterException);
    Greeting greeting = generateTestGreeting("Test greeting.....", tone);
    Long createdId = null;
    try {
        createdId = createTestData(builders, greeting);
    } catch (RestLiResponseException e) {
        if (tone != Tone.INSULTING) {
            fail();
        }
        if (responseFilter) {
            assertEquals(e.getServiceErrorMessage(), RESP_FILTER_ERROR_MESSAGE);
            assertEquals(e.getResponse().getStatus(), RESP_FILTER_ERROR_STATUS.getCode());
        } else {
            assertEquals(e.getServiceErrorMessage(), REQ_FILTER_ERROR_MESSAGE);
            assertEquals(e.getResponse().getStatus(), REQ_FILTER_ERROR_STATUS.getCode());
        }
        verifyFilters(tone, responseFilter);
        return;
    }
    if (tone == Tone.INSULTING) {
        fail();
    }
    if (!responseFilter) {
        greeting.setTone(mapToneForIncomingRequest(tone));
    }
    greeting.setId(createdId);
    Request<Greeting> getRequest = builders.get().id(createdId).build();
    Greeting getReturnedGreeting = getClient().sendRequest(getRequest).getResponse().getEntity();
    ValidateDataAgainstSchema.validate(getReturnedGreeting.data(), getReturnedGreeting.schema(), new ValidationOptions());
    assertEquals(getReturnedGreeting, greeting);
    deleteAndVerifyTestData(builders, createdId);
    verifyFilters(tone, responseFilter);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) Test(org.testng.annotations.Test)

Example 39 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestExceptionsResource2 method testGet.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testGet(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Response<Greeting> response = null;
    RestLiResponseException exception = null;
    try {
        final Request<Greeting> req = builders.get().id(1L).build();
        ResponseFuture<Greeting> future;
        if (explicit) {
            future = getClient().sendRequest(req, errorHandlingBehavior);
        } else {
            future = getClient().sendRequest(req);
        }
        response = future.getResponse();
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            Assert.fail("expected exception");
        }
    } catch (RestLiResponseException e) {
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            exception = e;
        } else {
            Assert.fail("not expected exception");
        }
    }
    if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
        Assert.assertNotNull(response);
        Assert.assertTrue(response.hasError());
        exception = response.getError();
        Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
        Assert.assertNotNull(response.getEntity());
        Assert.assertEquals(response.getEntity(), new Greeting().setMessage("Hello, sorry for the mess"));
    }
    Assert.assertNotNull(exception);
    Assert.assertTrue(exception.hasDecodedResponse());
    Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
    final DataMap respEntityMap = DataMapUtils.readMap(exception.getResponse());
    Assert.assertEquals(respEntityMap, new Greeting().setMessage("Hello, sorry for the mess").data());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 40 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestExceptionsResource2 method testExceptionWithoutValue.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testExceptionWithoutValue(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Response<Void> response = null;
    RestLiResponseException exception = null;
    final Request<Void> req = builders.<Void>action("ExceptionWithoutValue").build();
    try {
        ResponseFuture<Void> future;
        if (explicit) {
            future = getClient().sendRequest(req, errorHandlingBehavior);
        } else {
            future = getClient().sendRequest(req);
        }
        response = future.getResponse();
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            Assert.fail("expected exception");
        }
    } catch (RestLiResponseException e) {
        if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
            exception = e;
        } else {
            Assert.fail("not expected exception");
        }
    }
    if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
        Assert.assertNotNull(response);
        Assert.assertTrue(response.hasError());
        exception = response.getError();
        Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
        Assert.assertNull(response.getEntity());
    }
    Assert.assertNotNull(exception);
    Assert.assertTrue(exception.hasDecodedResponse());
    Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
}
Also used : RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Aggregations

RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)77 Test (org.testng.annotations.Test)67 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)30 EmptyRecord (com.linkedin.restli.common.EmptyRecord)10 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)10 FlowId (org.apache.gobblin.service.FlowId)10 ExecutionException (java.util.concurrent.ExecutionException)9 StringMap (com.linkedin.data.template.StringMap)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)6 FlowConfig (org.apache.gobblin.service.FlowConfig)6 ErrorResponse (com.linkedin.restli.common.ErrorResponse)5 HashMap (java.util.HashMap)5 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)4 MockRestliResponseExceptionBuilder (com.linkedin.restli.client.testutils.MockRestliResponseExceptionBuilder)4 CollectionResponse (com.linkedin.restli.common.CollectionResponse)4 AutoValidationWithProjectionBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationWithProjectionBuilders)4 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)4 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)4 GroupMembershipParam (com.linkedin.restli.examples.groups.api.GroupMembershipParam)4