use of com.linkedin.restli.common.CreateIdStatus 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<>(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.CreateIdStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testBatchCreate.
@Test
public void testBatchCreate() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
CompletionStage<List<CreateIdStatus<Long>>> result = greetings.batchCreate(Arrays.asList(getGreeting(), getGreeting()));
CompletableFuture<List<CreateIdStatus<Long>>> future = result.toCompletableFuture();
List<CreateIdStatus<Long>> ids = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(ids.size(), 2);
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestReturnEntityWithCreate method testBatchCreateId.
/**
* Test for backward compatibility of batch create id request.
* @param restClient
* @param expectedContentType
* @param builders
* @throws RemoteInvocationException
*/
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "newBuildersClientDataDataProvider")
public void testBatchCreateId(RestClient restClient, String expectedContentType, CreateGreetingRequestBuilders builders) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("first time!");
greeting.setTone(Tone.FRIENDLY);
BatchCreateIdRequest<Long, Greeting> batchCreateIdRequest = builders.batchCreate().inputs(Arrays.asList(greeting, greeting)).build();
Response<BatchCreateIdResponse<Long>> response = restClient.sendRequest(batchCreateIdRequest).getResponse();
Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), expectedContentType);
List<CreateIdStatus<Long>> elems = response.getEntity().getElements();
Assert.assertEquals(elems.get(0).getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
Assert.assertEquals(elems.get(0).getLocation(), "/" + builders.getPrimaryResource() + "/" + elems.get(0).getKey());
Assert.assertEquals(elems.get(1).getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
Assert.assertEquals(elems.get(1).getLocation(), "/" + builders.getPrimaryResource() + "/" + elems.get(1).getKey());
}
use of com.linkedin.restli.common.CreateIdStatus 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.CreateIdStatus in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testCreateResultBuilder.
@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String uriString, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) throws URISyntaxException {
List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
BatchCreateResult<Long, Foo> results = new BatchCreateResult<>(createResponses);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ServerResourceContext mockContext = getMockResourceContext(altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
RestRequest request = new RestRequestBuilder(new URI(uriString)).build();
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, headers, Collections.emptyList());
RestLiResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<>();
for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
items.add((CreateIdStatus<Object>) item.getRecord());
}
Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<>(items));
Assert.assertEquals(expectedStatuses, items);
Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Aggregations