use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestReturnEntityWithPartialUpdate method testInvalidReturnEntityParameter.
/**
* Ensures that using an invalid value for the {@link RestConstants#RETURN_ENTITY_PARAM} query parameter results
* in a 400 bad request error response for PARTIAL_UPDATE.
*/
@Test
@SuppressWarnings({ "Duplicates" })
public void testInvalidReturnEntityParameter() throws RemoteInvocationException {
final long expectedId = 8L;
Greeting expectedGreeting = new Greeting();
expectedGreeting.setMessage("Message " + expectedId);
expectedGreeting.setTone(Tone.FRIENDLY);
final String invalidParamValue = "NOTaBoolean";
PartialUpdateEntityRequest<Greeting> request = new PartialUpdateGreetingRequestBuilders().partialUpdateAndGet().id(expectedId).input(PatchRequest.createFromEmptyPatchDocument()).setParam(RestConstants.RETURN_ENTITY_PARAM, invalidParamValue).build();
try {
getClient().sendRequest(request).getResponse();
Assert.fail(String.format("Query parameter should cause an exception: %s=%s", RestConstants.RETURN_ENTITY_PARAM, invalidParamValue));
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode(), "Invalid response status.");
Assert.assertTrue(e.getServiceErrorMessage().contains(String.format("Invalid \"%s\" parameter: %s", RestConstants.RETURN_ENTITY_PARAM, invalidParamValue)), "Invalid error response message");
}
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestStreamingGreetings method resourceMethodDoesNotAcceptAttachments.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void resourceMethodDoesNotAcceptAttachments(final RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
// Resource method does not desire request attachments. Assert that all the attachments are drained and that a 400
// bad request is observed.
final RestLiTestAttachmentDataSource greetingAttachment = new RestLiTestAttachmentDataSource("1", ByteString.copyString("clientData", Charset.defaultCharset()));
RootBuilderWrapper.MethodBuilderWrapper<Long, Greeting, Object> methodBuilderWrapper = builders.action("actionNoAttachmentsAllowed");
methodBuilderWrapper.appendSingleAttachment(greetingAttachment);
final Request<Object> request = methodBuilderWrapper.build();
try {
getClient().sendRequest(request).getResponse().getEntity();
Assert.fail();
} catch (final RestLiResponseException responseException) {
Assert.assertEquals(responseException.getStatus(), 400);
Assert.assertEquals(responseException.getServiceErrorMessage(), "Resource method endpoint invoked does not accept any request attachments.");
}
// Then verify the response and request attachments were fully absorbed.
Assert.assertTrue(greetingAttachment.finished());
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestSimpleResourceHierarchy method testSubsubsimpleResourceDelete.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestSubSubBuilderDataProvider")
public void testSubsubsimpleResourceDelete(RootBuilderWrapper<Void, Greeting> builders) throws RemoteInvocationException {
// DELETE
Request<EmptyRecord> writeRequest = builders.delete().setPathKey("subgreetingsId", 1L).build();
getClient().sendRequest(writeRequest).getResponse();
// GET again, to verify that our DELETE worked.
try {
Request<Greeting> request = builders.get().setPathKey("subgreetingsId", 1L).build();
Response<Greeting> response = getClient().sendRequest(request).getResponse();
Greeting greeting = response.getEntity();
Assert.fail("Entity should have been removed.");
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_404_NOT_FOUND.getCode());
}
// Restore initial state
testSubsubsimpleResourceUpdate(builders);
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestMockResponseBuilder method testBuild.
@Test
public void testBuild() {
MockResponseBuilder<Long, Greeting> mockResponseBuilder = new MockResponseBuilder<>();
Greeting greeting = new Greeting().setId(1L).setMessage("message");
Map<String, String> headers = Collections.singletonMap("foo", "bar");
RestLiResponseException restLiResponseException = EasyMock.createMock(RestLiResponseException.class);
EasyMock.expect(restLiResponseException.getErrorSource()).andReturn("foo").once();
EasyMock.replay(restLiResponseException);
// build a response object using all the setters. This response does not make sense logically but the goal here
// is to test that the builder is working correctly.
mockResponseBuilder.setEntity(greeting).setHeaders(headers).setCookies(Collections.singletonList(new HttpCookie("cookie", "value"))).setStatus(200).setRestLiResponseException(restLiResponseException);
Response<Greeting> response = mockResponseBuilder.build();
// when we build the Response the ID is put into the headers
Map<String, String> builtHeaders = new HashMap<>(headers);
builtHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
Assert.assertEquals(response.getEntity(), greeting);
Assert.assertEquals(response.getHeaders(), builtHeaders);
Assert.assertEquals(response.getCookies(), Collections.singletonList(new HttpCookie("cookie", "value")));
Assert.assertEquals(response.getStatus(), 200);
Assert.assertEquals(response.getError().getErrorSource(), "foo");
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class MockFailedResponseFutureBuilder method buildWithErrorResponse.
private ResponseFuture<V> buildWithErrorResponse(ProtocolVersion protocolVersion) {
int status = (_errorResponse.hasStatus()) ? _errorResponse.getStatus() : DEFAULT_HTTP_STATUS;
// create a RestLiResponseException and wrap it in an ExecutionException that will be thrown by the ResponseFuture
RestLiResponseException restLiResponseException = new MockRestliResponseExceptionBuilder().setErrorResponse(_errorResponse).setStatus(HttpStatus.fromCode(status)).setCookies(getCookies() == null ? Collections.emptyList() : getCookies()).setHeaders(getHeaders() == null ? new HashMap<>() : getHeaders()).build();
ExecutionException executionException = new ExecutionException(restLiResponseException);
Future<Response<V>> responseFuture = buildFuture(null, executionException);
return new ResponseFutureImpl<>(responseFuture, _errorHandlingBehavior);
}
Aggregations