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;
}
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);
}
}
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++)));
}
}
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);
}
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());
}
Aggregations