Search in sources :

Example 66 with RestLiResponseException

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");
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 67 with RestLiResponseException

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());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) Test(org.testng.annotations.Test)

Example 68 with RestLiResponseException

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);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 69 with RestLiResponseException

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");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) HttpCookie(java.net.HttpCookie) MockResponseBuilder(com.linkedin.restli.client.testutils.MockResponseBuilder) Test(org.testng.annotations.Test)

Example 70 with RestLiResponseException

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);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) Response(com.linkedin.restli.client.Response) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) ResponseFutureImpl(com.linkedin.restli.internal.client.ResponseFutureImpl)

Aggregations

RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)85 Test (org.testng.annotations.Test)75 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)30 StringMap (com.linkedin.data.template.StringMap)15 FlowId (org.apache.gobblin.service.FlowId)11 EmptyRecord (com.linkedin.restli.common.EmptyRecord)10 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)10 ExecutionException (java.util.concurrent.ExecutionException)9 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 FlowConfig (org.apache.gobblin.service.FlowConfig)7 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)6 ErrorResponse (com.linkedin.restli.common.ErrorResponse)5 HashMap (java.util.HashMap)5 Schedule (org.apache.gobblin.service.Schedule)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