@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testCreateError(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws Exception
{
Response<EmptyRecord> response = null;
RestLiResponseException exception = null;
try
{
Request<EmptyRecord> createRequest = builders.create()
.input(new Greeting().setId(11L).setMessage("@#$%@!$%").setTone(Tone.INSULTING))
.build();
ResponseFuture<EmptyRecord> future;
if (explicit)
{
future = REST_CLIENT.sendRequest(createRequest, errorHandlingBehavior);
}
else
{
future = REST_CLIENT.sendRequest(createRequest);
}
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_406_NOT_ACCEPTABLE.getCode());
Assert.assertNull(response.getEntity());
}
Assert.assertNotNull(exception);
Assert.assertFalse(exception.hasDecodedResponse());
Assert.assertEquals(exception.getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
Assert.assertEquals(exception.getServiceErrorMessage(), "I will not tolerate your insolence!");
Assert.assertEquals(exception.getServiceErrorCode(), 999);
Assert.assertEquals(exception.getErrorSource(), RestConstants.HEADER_VALUE_ERROR);
Assert.assertEquals(exception.getErrorDetails().getString("reason"), "insultingGreeting");
Assert.assertTrue(exception.getServiceErrorStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:406, serviceErrorCode:999]: I will not tolerate your insolence!"),
"stacktrace mismatch:" + exception.getStackTrace());
}