use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testCreateAndThenBatchDeleteWithFailures.
@Test
public void testCreateAndThenBatchDeleteWithFailures() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
// Create entities first so we don't delete those used by other tests.
CompletionStage<List<CreateIdStatus<Long>>> createResult = greetings.batchCreate(Arrays.asList(getGreeting(), getGreeting()));
CompletionStage<Map<Long, UpdateStatus>> result = createResult.thenCompose(ids -> {
Set<Long> deleteIds = Sets.newHashSet(ids.stream().map(CreateIdStatus::getKey).collect(Collectors.toList()));
deleteIds.add(-1L);
return greetings.batchDelete(deleteIds);
});
CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
Map<Long, UpdateStatus> ids = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(ids.size(), 3);
Assert.assertEquals(ids.remove(-1L).getStatus().intValue(), 404);
for (UpdateStatus status : ids.values()) {
Assert.assertEquals(status.getStatus().intValue(), 204);
}
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testComplexKey_partialUpdate.
@Test
public void testComplexKey_partialUpdate() throws Exception {
ComplexKeys complexKeyClient = new ComplexKeysFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
Message message = new Message();
message.setTone(Tone.FRIENDLY);
PatchRequest<Message> patch = PatchGenerator.diffEmpty(message);
final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>> inputs = new HashMap<>();
ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
inputs.put(key1, patch);
inputs.put(key2, patch);
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> result = complexKeyClient.batchPartialUpdate(inputs).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
// Update return valid result
Assert.assertEquals(result.get(key1).getStatus().intValue(), 204);
Assert.assertEquals(result.get(key2).getStatus().intValue(), 204);
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> getResult = complexKeyClient.batchGet(new HashSet<>(Arrays.asList(key1, key2))).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(getResult.get(key1).getEntity().getTone(), Tone.FRIENDLY);
Assert.assertEquals(getResult.get(key2).getEntity().getTone(), Tone.FRIENDLY);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testAssociateResourceBatchPartialUpdate.
@Test
public void testAssociateResourceBatchPartialUpdate() throws Exception {
Associations associations = new AssociationsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
Map<CompoundKey, PatchRequest<Message>> patches = new HashMap<>();
patches.put(getAssociateResourceUrlKey(associations), new PatchRequest<>());
patches.put(getAssociateResourceSimpleKey(associations), new PatchRequest<>());
Map<CompoundKey, UpdateStatus> ids = associations.batchPartialUpdate(patches).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
for (CompoundKey id : ids.keySet()) {
Assert.assertEquals(ids.get(id).getStatus().intValue(), 204);
}
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class BatchResponseBuilder method buildRestLiResponseData.
/**
* {@inheritDoc}
*
* @param result The result of a Rest.li BATCH_UPDATE, BATCH_PARTIAL_UPDATE, or BATCH_DELETE method. It is a
* {@link BatchUpdateResult} object.
*/
@SuppressWarnings("unchecked")
@Override
public D buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
@SuppressWarnings({ "unchecked" }) final BatchUpdateResult<Object, ?> /* constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchUpdate(java.util.Map)} */
updateResult = (BatchUpdateResult<Object, ?>) result;
final Map<Object, UpdateResponse> results = updateResult.getResults();
// Verify the map is not null. If so, this is a developer error.
if (results == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
}
final Map<Object, RestLiServiceException> serviceErrors = updateResult.getErrors();
if (serviceErrors == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null errors Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
}
TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
Map<Object, BatchResponseEntry> batchResponseMap = new HashMap<>();
for (Map.Entry<Object, UpdateResponse> entry : results.entrySet()) {
if (entry.getKey() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
}
if (!serviceErrors.containsKey(entry.getKey())) {
Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
UpdateStatus updateStatus = buildUpdateStatus(routingResult.getContext(), entry.getValue());
batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), updateStatus));
}
}
TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
for (Map.Entry<Object, RestLiServiceException> entry : serviceErrors.entrySet()) {
if (entry.getKey() == null || entry.getValue() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key or value inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
}
Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
}
for (Map.Entry<Object, RestLiServiceException> entry : routingResult.getContext().getBatchKeyErrors().entrySet()) {
Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
}
return buildResponseData(HttpStatus.S_200_OK, batchResponseMap, headers, cookies);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class BatchResponseBuilder method toBatchResponse.
/**
* Helper method for {@link #buildResponse} that splits the merged {@link UpdateStatus} map into a "statuses" map and
* an "errors" map, uses this to construct a data map that properly matches the over-the-wire format, and wraps it
* in a {@link BatchResponse}.
* @param statuses map of {@link UpdateStatus} objects
* @param protocolVersion
* @param <K> key type
* @return batch response
*/
private static <K> BatchResponse<AnyRecord> toBatchResponse(Map<K, UpdateStatus> statuses, ProtocolVersion protocolVersion) {
final DataMap splitStatuses = new DataMap();
final DataMap splitErrors = new DataMap();
for (Map.Entry<K, UpdateStatus> statusEntry : statuses.entrySet()) {
final DataMap statusData = statusEntry.getValue().data();
final String stringKey = URIParamUtils.encodeKeyForBody(statusEntry.getKey(), false, protocolVersion);
final DataMap error = statusData.getDataMap("error");
if (error == null) {
// status and error should be mutually exclusive for now
CheckedUtil.putWithoutChecking(splitStatuses, stringKey, statusData);
} else {
CheckedUtil.putWithoutChecking(splitErrors, stringKey, error);
}
}
final DataMap splitResponseData = new DataMap();
CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.RESULTS, splitStatuses);
CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.ERRORS, splitErrors);
return new BatchResponse<>(splitResponseData, AnyRecord.class);
}
Aggregations