Search in sources :

Example 1 with CreateStatus

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

the class BatchCreateHelper method batchCreateOldBuilders.

private static <K, V extends RecordTemplate> List<CreateIdStatus<K>> batchCreateOldBuilders(RestClient restClient, BatchCreateRequestBuilder<K, V> builder, List<V> entities) throws RemoteInvocationException {
    BatchCreateRequest<V> request = builder.inputs(entities).build();
    Response<CollectionResponse<CreateStatus>> response = restClient.sendRequest(request).getResponse();
    List<CreateStatus> elements = response.getEntity().getElements();
    List<CreateIdStatus<K>> result = new ArrayList<CreateIdStatus<K>>(elements.size());
    for (CreateStatus status : elements) {
        @SuppressWarnings("unchecked") CreateIdStatus<K> createIdStatus = (CreateIdStatus<K>) status;
        result.add(createIdStatus);
    }
    return result;
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus)

Example 2 with CreateStatus

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

the class TestGreetingsClient method testOldCookbookInBatch.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testOldCookbookInBatch(RestliRequestOptions requestOptions) throws Exception {
    final GreetingsBuilders builders = new GreetingsBuilders(requestOptions);
    // GET
    final BatchGetRequestBuilder<Long, Greeting> batchGetBuilder = builders.batchGet();
    Request<BatchResponse<Greeting>> request = batchGetBuilder.ids(1L).build();
    ResponseFuture<BatchResponse<Greeting>> future = getClient().sendRequest(request);
    Response<BatchResponse<Greeting>> greetingResponse = future.getResponse();
    // PUT
    Greeting greeting = new Greeting(greetingResponse.getEntity().getResults().get("1").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<BatchResponse<Greeting>> request2 = builders.batchGet().ids(1L).build();
    ResponseFuture<BatchResponse<Greeting>> future2 = getClient().sendRequest(request2);
    greetingResponse = future2.get();
    Greeting repeatedGreeting = new Greeting();
    repeatedGreeting.setMessage("Hello Hello");
    repeatedGreeting.setTone(Tone.SINCERE);
    Request<CollectionResponse<CreateStatus>> request3 = builders.batchCreate().inputs(Arrays.asList(repeatedGreeting, repeatedGreeting)).build();
    CollectionResponse<CreateStatus> statuses = getClient().sendRequest(request3).getResponse().getEntity();
    for (CreateStatus status : statuses.getElements()) {
        Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertNotNull(id);
    }
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) BatchResponse(com.linkedin.restli.common.BatchResponse) CollectionResponse(com.linkedin.restli.common.CollectionResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) Test(org.testng.annotations.Test)

Example 3 with CreateStatus

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

the class TestRestLiValidation method testBatchCreateManualFailure.

@Test(dataProvider = "batchCreateFailureData")
public void testBatchCreateManualFailure(List<ValidationDemo> validationDemos, List<String> errorMessages) throws RemoteInvocationException {
    Response<CollectionResponse<CreateStatus>> response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    List<CreateStatus> results = response.getEntity().getElements();
    int i = 0;
    for (CreateStatus result : results) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
    response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosRequestBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    @SuppressWarnings("unchecked") List<CreateIdStatus<Integer>> results2 = ((BatchCreateIdResponse<Integer>) (Object) response.getEntity()).getElements();
    i = 0;
    for (CreateIdStatus<Integer> result : results2) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) ValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosBuilders) AutoValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders) AutoValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders) ValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosRequestBuilders) Test(org.testng.annotations.Test)

Example 4 with CreateStatus

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

the class TestComplexKeysResource method testBatchCreateMain.

private void testBatchCreateMain(BatchCreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetRequestBuilder<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
    Request<CollectionResponse<CreateStatus>> request = batchCreateRequestBuilder.inputs(messages).build();
    ResponseFuture<CollectionResponse<CreateStatus>> future = getClient().sendRequest(request);
    Response<CollectionResponse<CreateStatus>> response = future.getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
    expectedComplexKeys.add(expectedComplexKey1);
    expectedComplexKeys.add(expectedComplexKey2);
    for (CreateStatus createStatus : response.getEntity().getElements()) {
        @SuppressWarnings("unchecked") CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> createIdStatus = (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>>) createStatus;
        Assert.assertEquals(createIdStatus.getStatus(), new Integer(201));
        Assert.assertTrue(expectedComplexKeys.contains(createIdStatus.getKey()));
        try {
            @SuppressWarnings("deprecation") String id = createIdStatus.getId();
            Assert.fail("buildReadOnlyId should throw an exception on ComplexKeys");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        expectedComplexKeys.remove(createIdStatus.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);
    BatchGetKVRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequest = batchGetRequestBuilder.ids(createdKeys).buildKV();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getFuture = getClient().sendRequest(getRequest);
    Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getResponse = getFuture.getResponse();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getResults = getResponse.getEntity().getResults();
    Assert.assertEquals(getResults.get(expectedComplexKey1), message1);
    Assert.assertEquals(getResults.get(expectedComplexKey2), message2);
    Assert.assertEquals(getResults.size(), 2);
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) HashSet(java.util.HashSet) TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 5 with CreateStatus

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

the class TestCustomTypesClient method testCollectionBatchCreate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreate(RestliRequestOptions options) throws RemoteInvocationException {
    CustomTypes2Builders builders = new CustomTypes2Builders(options);
    BatchCreateRequest<Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
    Response<CollectionResponse<CreateStatus>> response = getClient().sendRequest(request).getResponse();
    List<CreateStatus> results = response.getEntity().getElements();
    Set<CustomLong> expectedKeys = new HashSet<CustomLong>();
    expectedKeys.add(new CustomLong(1L));
    expectedKeys.add(new CustomLong(2L));
    for (CreateStatus status : results) {
        @SuppressWarnings("unchecked") CreateIdStatus<CustomLong> createIdStatus = (CreateIdStatus<CustomLong>) status;
        Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
        Assert.assertTrue(expectedKeys.contains(createIdStatus.getKey()));
        @SuppressWarnings("deprecation") String id = createIdStatus.getId();
        Assert.assertEquals(BatchResponse.keyToString(createIdStatus.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
        expectedKeys.remove(createIdStatus.getKey());
    }
    Assert.assertTrue(expectedKeys.isEmpty());
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) ByteString(com.linkedin.data.ByteString) CustomTypes2Builders(com.linkedin.restli.examples.greetings.client.CustomTypes2Builders) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

CollectionResponse (com.linkedin.restli.common.CollectionResponse)7 CreateStatus (com.linkedin.restli.common.CreateStatus)7 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)5 Test (org.testng.annotations.Test)5 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)4 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ByteString (com.linkedin.data.ByteString)1 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)1 BatchResponse (com.linkedin.restli.common.BatchResponse)1 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)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