use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.
the class TestRestLiScatterGather method testSendSGPartialUpdateEntityRequests.
// BatchPartialUpdateEntityRequest
private static void testSendSGPartialUpdateEntityRequests(RestClient restClient, Map<Long, PatchRequest<Greeting>> inputs) throws RemoteInvocationException {
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(inputs).setParam("foo", "bar").returnEntity(true).build();
BatchKVResponse<Long, UpdateEntityStatus<Greeting>> result = restClient.sendRequest(request).getResponse().getEntity();
Assert.assertEquals(result.getResults().size(), inputs.size());
UpdateEntityStatus<Greeting> item = result.getResults().values().iterator().next();
Assert.assertNotNull(item.getEntity());
Assert.assertFalse(item.hasError());
Assert.assertEquals(result.getErrors().size(), 0);
}
use of com.linkedin.restli.common.UpdateEntityStatus 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.UpdateEntityStatus 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());
}
}
use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.
the class RestLiValidationFilter method validateBatchResponse.
private void validateBatchResponse(RestLiDataValidator validator, Map<?, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<?, ? extends BatchResponseEnvelope.BatchResponseEntry> entry : batchResponseMap.entrySet()) {
if (entry.getValue().hasException()) {
continue;
}
// The "entity" in the results map may be the raw record entity, or a wrapper containing the record entity
final RecordTemplate entity = entry.getValue().getRecord();
ValidationResult result;
if (entity instanceof UpdateEntityStatus) {
result = validator.validateOutput(((UpdateEntityStatus<? extends RecordTemplate>) entity).getEntity());
} else {
result = validator.validateOutput(entity);
}
if (!result.isValid()) {
sb.append("Key: ").append(entry.getKey()).append(", ").append(result.getMessages().toString());
}
}
if (sb.length() > 0) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, sb.toString());
}
}
use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method createBatchUpdateEntityResponse.
private static Response<BatchKVResponse<Long, UpdateEntityStatus<TestRecord>>> createBatchUpdateEntityResponse(ProtocolVersion version, Set<Long> resultKeys, Set<Long> errorKeys) {
DataMap resultMap = new DataMap();
DataMap errorMap = new DataMap();
for (Long id : resultKeys) {
resultMap.put(id.toString(), new UpdateEntityStatus<>(HttpStatus.S_200_OK.getCode(), new TestRecord().setId(id)).data());
}
for (Long id : errorKeys) {
ErrorResponse err = new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode());
errorMap.put(id.toString(), err.data());
}
DataMap responseMap = new DataMap();
responseMap.put(BatchResponse.RESULTS, resultMap);
responseMap.put(BatchResponse.ERRORS, errorMap);
DataMap mergedMap = ResponseDecoderUtil.mergeUpdateStatusResponseData(responseMap);
BatchUpdateEntityResponse<Long, TestRecord> response = new BatchUpdateEntityResponse<>(mergedMap, new TypeSpec<>(Long.class), new TypeSpec<>(TestRecord.class), Collections.emptyMap(), null, version);
return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
Aggregations