Search in sources :

Example 6 with BatchCreateIdResponse

use of com.linkedin.restli.common.BatchCreateIdResponse in project rest.li by linkedin.

the class TestCompressionServer method testNewCookbookInBatch.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCookbookDataProvider")
public void testNewCookbookInBatch(RestClient client, RestliRequestOptions requestOptions) throws Exception {
    final GreetingsRequestBuilders builders = new GreetingsRequestBuilders(requestOptions);
    // GET
    Greeting greetingResult = getNewCookbookBatchGetResult(client, requestOptions);
    // POST
    Greeting greeting = new Greeting(greetingResult.data().copy());
    greeting.setMessage("This is a new message!");
    Request<BatchKVResponse<Long, UpdateStatus>> writeRequest = builders.batchUpdate().input(1L, greeting).build();
    client.sendRequest(writeRequest).getResponse();
    // GET again, to verify that our POST worked.
    getNewCookbookBatchGetResult(client, requestOptions);
    // batch Create
    Greeting repeatedGreeting = new Greeting();
    repeatedGreeting.setMessage("Hello Hello");
    repeatedGreeting.setTone(Tone.SINCERE);
    List<Greeting> entities = Arrays.asList(repeatedGreeting, repeatedGreeting);
    Request<BatchCreateIdResponse<Long>> batchCreateRequest = builders.batchCreate().inputs(entities).build();
    List<CreateIdStatus<Long>> statuses = client.sendRequest(batchCreateRequest).getResponse().getEntity().getElements();
    for (CreateIdStatus<Long> status : statuses) {
        Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertEquals(status.getKey().longValue(), Long.parseLong(id));
        Assert.assertNotNull(status.getKey());
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) Test(org.testng.annotations.Test)

Example 7 with BatchCreateIdResponse

use of com.linkedin.restli.common.BatchCreateIdResponse in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchCreateIdMain.

private void testBatchCreateIdMain(BatchCreateIdRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText1 = "firstMessage";
    Message message1 = new Message();
    message1.setMessage(messageText1);
    final String messageText2 = "secondMessage";
    Message message2 = new Message();
    message2.setMessage(messageText2);
    List<Message> messages = new ArrayList<Message>(2);
    messages.add(message1);
    messages.add(message2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
    // test build
    BatchCreateIdRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> request = batchCreateRequestBuilder.inputs(messages).build();
    Response<BatchCreateIdResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>>> response = getClient().sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
    expectedComplexKeys.add(expectedComplexKey1);
    expectedComplexKeys.add(expectedComplexKey2);
    for (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> status : response.getEntity().getElements()) {
        Assert.assertEquals(status.getStatus(), new Integer(201));
        Assert.assertTrue(expectedComplexKeys.contains(status.getKey()));
        try {
            @SuppressWarnings("deprecation") String id = status.getId();
            Assert.fail("buildReadOnlyId should throw an exception for ComplexKeys");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        expectedComplexKeys.remove(status.getKey());
    }
    Assert.assertTrue(expectedComplexKeys.isEmpty());
    // attempt to batch get created records
    List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
    createdKeys.add(expectedComplexKey1);
    createdKeys.add(expectedComplexKey2);
    Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getRequest = batchGetRequestBuilder.ids(createdKeys).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getFuture = getClient().sendRequest(getRequest);
    Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> getResponse = getFuture.getResponse();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> getResults = getResponse.getEntity().getResults();
    Assert.assertEquals(getResults.get(expectedComplexKey1).getEntity(), message1);
    Assert.assertEquals(getResults.get(expectedComplexKey2).getEntity(), message2);
    Assert.assertEquals(getResults.size(), 2);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) HashSet(java.util.HashSet)

Example 8 with BatchCreateIdResponse

use of com.linkedin.restli.common.BatchCreateIdResponse in project rest.li by linkedin.

the class TestGreetingsClient method testNewCookbookInBatch.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testNewCookbookInBatch(RestliRequestOptions requestOptions) throws Exception {
    final GreetingsRequestBuilders builders = new GreetingsRequestBuilders(requestOptions);
    // GET
    final BatchGetEntityRequestBuilder<Long, Greeting> batchGetBuilder = builders.batchGet();
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request = batchGetBuilder.ids(1L).build();
    ResponseFuture<BatchKVResponse<Long, EntityResponse<Greeting>>> future = getClient().sendRequest(request);
    Response<BatchKVResponse<Long, EntityResponse<Greeting>>> greetingResponse = future.getResponse();
    // PUT
    Greeting greeting = new Greeting(greetingResponse.getEntity().getResults().get(1L).getEntity().data().copy());
    greeting.setMessage("This is a new message!");
    Request<BatchKVResponse<Long, UpdateStatus>> writeRequest = builders.batchUpdate().input(1L, greeting).build();
    getClient().sendRequest(writeRequest).getResponse();
    // GET again, to verify that our POST worked.
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request2 = builders.batchGet().ids(1L).build();
    ResponseFuture<BatchKVResponse<Long, EntityResponse<Greeting>>> future2 = getClient().sendRequest(request2);
    greetingResponse = future2.get();
    Greeting repeatedGreeting = new Greeting();
    repeatedGreeting.setMessage("Hello Hello");
    repeatedGreeting.setTone(Tone.SINCERE);
    List<Greeting> entities = Arrays.asList(repeatedGreeting, repeatedGreeting);
    Request<BatchCreateIdResponse<Long>> request3 = builders.batchCreate().inputs(entities).build();
    BatchCreateIdResponse<Long> statuses = getClient().sendRequest(request3).getResponse().getEntity();
    for (CreateIdStatus<Long> status : statuses.getElements()) {
        Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertEquals(status.getKey().longValue(), Long.parseLong(id));
        Assert.assertNotNull(status.getKey());
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) Test(org.testng.annotations.Test)

Example 9 with BatchCreateIdResponse

use of com.linkedin.restli.common.BatchCreateIdResponse in project rest.li by linkedin.

the class TestExceptionsResource method testBatchCreateIdErrors.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchCreateIdErrors(RestliRequestOptions requestOptions) throws Exception {
    ExceptionsRequestBuilders builders = new ExceptionsRequestBuilders(requestOptions);
    BatchCreateIdRequest<Long, Greeting> batchCreateRequest = builders.batchCreate().input(new Greeting().setId(10L).setMessage("Greetings.").setTone(Tone.SINCERE)).input(new Greeting().setId(11L).setMessage("@#$%@!$%").setTone(Tone.INSULTING)).build();
    Response<BatchCreateIdResponse<Long>> response = getClient().sendRequest(batchCreateRequest).getResponse();
    List<CreateIdStatus<Long>> createStatuses = response.getEntity().getElements();
    Assert.assertEquals(createStatuses.size(), 2);
    @SuppressWarnings("unchecked") CreateIdStatus<Long> status0 = createStatuses.get(0);
    Assert.assertEquals(status0.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(status0.getKey(), new Long(10));
    @SuppressWarnings("deprecation") String id = status0.getId();
    Assert.assertEquals(BatchResponse.keyToString(status0.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
    Assert.assertFalse(status0.hasError());
    CreateIdStatus<Long> status1 = createStatuses.get(1);
    Assert.assertEquals(status1.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertTrue(status1.hasError());
    ErrorResponse error = status1.getError();
    Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertEquals(error.getMessage(), "I will not tolerate your insolence!");
    Assert.assertEquals(error.getServiceErrorCode().intValue(), 999);
    Assert.assertEquals(error.getExceptionClass(), "com.linkedin.restli.server.RestLiServiceException");
    Assert.assertEquals(error.getErrorDetails().data().getString("reason"), "insultingGreeting");
    Assert.assertTrue(error.getStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:406, serviceErrorCode:999]: I will not tolerate your insolence!"), "stacktrace mismatch:" + error.getStackTrace());
}
Also used : BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ExceptionsRequestBuilders(com.linkedin.restli.examples.greetings.client.ExceptionsRequestBuilders) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Aggregations

BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)9 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)7 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)6 Test (org.testng.annotations.Test)6 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 GreetingsRequestBuilders (com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ByteString (com.linkedin.data.ByteString)1 CollectionResponse (com.linkedin.restli.common.CollectionResponse)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 CreateStatus (com.linkedin.restli.common.CreateStatus)1 EntityResponse (com.linkedin.restli.common.EntityResponse)1 ErrorResponse (com.linkedin.restli.common.ErrorResponse)1 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)1 Message (com.linkedin.restli.examples.greetings.api.Message)1 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)1 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)1 AutoValidationDemosBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders)1 AutoValidationDemosRequestBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders)1