use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testBatchPartialUpdateAndGetWithErrors.
@Test
public void testBatchPartialUpdateAndGetWithErrors() throws Exception {
PartialUpdateGreeting greetings = new PartialUpdateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
Map<Long, PatchRequest<Greeting>> inputs = new HashMap<>();
Greeting original = getGreeting();
String message = "Edited message: fluent api test partialUpdateAndGet";
Greeting update = getGreeting(message);
inputs.put(21L, PatchGenerator.diff(original, update));
inputs.put(-2L, PatchGenerator.diff(original, update));
CompletionStage<Map<Long, UpdateEntityStatus<Greeting>>> result = greetings.batchPartialUpdateAndGet(inputs);
CompletableFuture<Map<Long, UpdateEntityStatus<Greeting>>> future = result.toCompletableFuture();
Assert.assertNotNull(future.get(5000, TimeUnit.MILLISECONDS));
Assert.assertEquals(future.get().get(21L).getStatus().intValue(), 200);
Assert.assertEquals(future.get().get(21L).getEntity().getId().longValue(), 21L);
Assert.assertEquals(future.get().get(21L).getEntity().getMessage(), message);
Assert.assertEquals(future.get().get(-2L).getStatus().intValue(), 404);
Assert.assertFalse(future.get().get(-2L).hasEntity());
}
use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testBatchPartialUpdateAndGet.
@Test
public void testBatchPartialUpdateAndGet() throws Exception {
PartialUpdateGreeting greetings = new PartialUpdateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
Map<Long, PatchRequest<Greeting>> inputs = new HashMap<>();
Greeting original = getGreeting();
String message = "Edited message: fluent api test partialUpdateAndGet";
Greeting update = getGreeting(message);
inputs.put(21L, PatchGenerator.diff(original, update));
inputs.put(22L, PatchGenerator.diff(original, update));
CompletionStage<Map<Long, UpdateEntityStatus<Greeting>>> result = greetings.batchPartialUpdateAndGet(inputs);
CompletableFuture<Map<Long, UpdateEntityStatus<Greeting>>> future = result.toCompletableFuture();
Assert.assertNotNull(future.get(5000, TimeUnit.MILLISECONDS));
Assert.assertEquals(future.get().get(21L).getEntity().getId().longValue(), 21L);
Assert.assertEquals(future.get().get(21L).getEntity().getMessage(), message);
Assert.assertEquals(future.get().get(22L).getEntity().getId().longValue(), 22L);
Assert.assertEquals(future.get().get(22L).getEntity().getMessage(), message);
}
use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.
the class TestRestLiValidationFromClient method testBatchPartialUpdate.
@Test(dataProvider = "provideBatchPartialUpdateData")
public void testBatchPartialUpdate(Object builder, Map<Integer, PatchRequest<ValidationDemo>> inputs, Map<Integer, String> errorMessages) {
for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : inputs.entrySet()) {
ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).batchPartialUpdate().validateInput(entry.getValue());
String expected = errorMessages.get(entry.getKey());
if (expected.isEmpty()) {
Assert.assertEquals(result.isValid(), true);
} else {
Assert.assertEquals(result.isValid(), false);
Assert.assertEquals(result.getMessages().toString(), expected);
}
}
}
use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testReturnEntityOnDemand.
/**
* Ensures that different usages of {@link BatchPartialUpdateEntityRequestBuilder#returnEntity(boolean)} are handled
* correctly and that the response appropriately contains the entities or nothing depending on how and if the provided
* method is used.
* @param returnEntity value of the {@link RestConstants#RETURN_ENTITY_PARAM} parameter of this request
* @param expectReturnEntities whether or not the patched entities are expected in the response
*/
@Test(dataProvider = "returnEntityOnDemandData")
public void testReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntities) throws RemoteInvocationException {
final long expectedId1 = 8L;
final long expectedId2 = 9L;
Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
patches.put(expectedId1, PatchRequest.createFromEmptyPatchDocument());
patches.put(expectedId2, PatchRequest.createFromEmptyPatchDocument());
Map<Long, Greeting> expectedGreetings = new HashMap<>();
expectedGreetings.put(expectedId1, new Greeting().setId(expectedId1).setMessage("Message " + expectedId1).setTone(Tone.FRIENDLY));
expectedGreetings.put(expectedId2, new Greeting().setId(expectedId2).setMessage("Message " + expectedId2).setTone(Tone.FRIENDLY));
BatchPartialUpdateEntityRequestBuilder<Long, Greeting> requestBuilder = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches);
if (returnEntity != null) {
requestBuilder.returnEntity(returnEntity);
}
BatchPartialUpdateEntityRequest<Long, Greeting> request = requestBuilder.build();
Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
Assert.assertNotNull(batchKVResponse);
Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
Assert.assertNotNull(greetings);
for (Long key : greetings.keySet()) {
Assert.assertTrue(expectedGreetings.containsKey(key), "Encountered unexpected ID in batch response.");
UpdateEntityStatus<Greeting> updateEntityStatus = greetings.get(key);
Assert.assertNotNull(updateEntityStatus);
Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_200_OK.getCode());
Assert.assertFalse(updateEntityStatus.hasError());
if (expectReturnEntities) {
Assert.assertTrue(updateEntityStatus.hasEntity());
Greeting returnedEntity = updateEntityStatus.getEntity();
Greeting expectedEntity = expectedGreetings.get(key);
Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
Assert.assertEquals(returnedEntity.getId(), expectedEntity.getId(), "Expected returned entity ID to match original.");
Assert.assertEquals(returnedEntity.getMessage(), expectedEntity.getMessage(), "Expected returned entity message to match original.");
Assert.assertEquals(returnedEntity.getTone(), expectedEntity.getTone(), "Expected returned entity tone to match original.");
} else {
Assert.assertFalse(updateEntityStatus.hasEntity());
Assert.assertNull(updateEntityStatus.getEntity());
}
}
}
use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateErrorMap.
/**
* Ensures that individual errors are handled correctly and sent back to the client in the batch response.
* This test coerces the server resource to return 404 errors after trying to patch nonexistent entities.
*/
@Test
public void testBatchPartialUpdateErrorMap() throws RemoteInvocationException {
Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
patches.put(2147L, PatchRequest.createFromEmptyPatchDocument());
patches.put(2148L, PatchRequest.createFromEmptyPatchDocument());
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches).returnEntity(true).build();
Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
Assert.assertNotNull(response);
BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
Assert.assertNotNull(batchKVResponse);
Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
Assert.assertNotNull(greetings);
for (UpdateEntityStatus<Greeting> updateEntityStatus : batchKVResponse.getResults().values()) {
Assert.assertFalse(updateEntityStatus.hasEntity());
Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
Assert.assertTrue(updateEntityStatus.hasError());
ErrorResponse error = updateEntityStatus.getError();
Assert.assertNotNull(error);
Assert.assertEquals(updateEntityStatus.getError().getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
}
Map<Long, ErrorResponse> errors = batchKVResponse.getErrors();
Assert.assertNotNull(errors);
for (ErrorResponse error : errors.values()) {
Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
}
}
Aggregations