use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method unsupportedNullKeyMapData.
@DataProvider(name = "unsupportedNullKeyMapData")
public Object[][] unsupportedNullKeyMapData() {
final CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
final Map<CompoundKey, UpdateResponse> results = new ConcurrentHashMap<CompoundKey, UpdateResponse>();
results.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
final BatchUpdateResult<CompoundKey, Foo> batchUpdateResult = new BatchUpdateResult<CompoundKey, Foo>(results, new ConcurrentHashMap<CompoundKey, RestLiServiceException>());
final UpdateStatus updateStatus = new UpdateStatus().setStatus(202);
final Map<String, UpdateStatus> expectedProtocol1Results = new HashMap<String, UpdateStatus>();
expectedProtocol1Results.put("a=a1&b=1", updateStatus);
final Map<String, UpdateStatus> expectedProtocol2Results = new HashMap<String, UpdateStatus>();
expectedProtocol2Results.put("(a:a1,b:1)", updateStatus);
return new Object[][] { { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), expectedProtocol1Results }, { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), expectedProtocol2Results } };
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class BatchUpdateResponseBuilder method toBatchResponse.
private static <K> BatchResponse<AnyRecord> toBatchResponse(Map<K, UpdateStatus> statuses, ProtocolVersion protocolVersion) {
final DataMap splitResponseData = new DataMap();
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);
}
}
CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.RESULTS, splitStatuses);
CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.ERRORS, splitErrors);
return new BatchResponse<AnyRecord>(splitResponseData, AnyRecord.class);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class BatchUpdateResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
Map<Object, UpdateStatus> mergedResults = new HashMap<Object, UpdateStatus>();
final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
generateResultEntityResponse(routingResult, responses, mergedResults);
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
@SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(mergedResults, protocolVersion);
return builder.entity(response).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestComplexKeysResource method testBatchUpdateMain.
private void testBatchUpdateMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
final String messageText = StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2;
final Message message = new Message();
message.setId(StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2);
message.setMessage(messageText);
message.setTone(Tone.INSULTING);
final String messageText2 = StringTestKeys.URL + " " + StringTestKeys.URL2;
final Message message2 = new Message();
message2.setId(StringTestKeys.URL + " " + StringTestKeys.URL2);
message2.setMessage(messageText2);
message2.setTone(Tone.INSULTING);
final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> inputs = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>();
ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
ComplexResourceKey<TwoPartKey, TwoPartKey> key3 = getComplexKey(StringTestKeys.ERROR, StringTestKeys.ERROR);
inputs.put(key1, message);
inputs.put(key2, message2);
// key3 should error anyway, so message is irrelevant.
inputs.put(key3, message);
final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.inputs(inputs).build();
final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
Assert.assertEquals(response.getResults().size(), inputs.size());
for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
Assert.assertTrue(inputs.containsKey(resp.getKey()));
final UpdateStatus status = resp.getValue();
if (status.hasError()) {
Assert.assertTrue(status.getStatus() == status.getError().getStatus(), "Update status should match the status of the error, if there is any.");
} else {
Assert.assertEquals((int) status.getStatus(), 200);
}
}
Assert.assertNotNull(response.getResults().get(key1));
Assert.assertNotNull(response.getResults().get(key2));
Assert.assertNotNull(response.getErrors().get(key3));
ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>();
ids.add(key1);
ids.add(key2);
BatchGetEntityRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
Assert.assertEquals(batchGetResponse.getResults().get(key1).getEntity().getTone(), Tone.INSULTING);
Assert.assertEquals(batchGetResponse.getResults().get(key2).getEntity().getTone(), Tone.INSULTING);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestScatterGather method testSendSGKVRequests.
private static void testSendSGKVRequests(Collection<ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus>> scatterGatherRequests, Long[] requestIds) throws InterruptedException {
final Map<Long, UpdateStatus> results = new ConcurrentHashMap<Long, UpdateStatus>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<Throwable>();
final List<BatchKVResponse<Long, UpdateStatus>> responses = new ArrayList<BatchKVResponse<Long, UpdateStatus>>();
for (ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus> requestInfo : scatterGatherRequests) {
Callback<Response<BatchKVResponse<Long, UpdateStatus>>> cb = new Callback<Response<BatchKVResponse<Long, UpdateStatus>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<Long, UpdateStatus>> response) {
results.putAll(response.getEntity().getResults());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
};
BatchRequest<BatchKVResponse<Long, UpdateStatus>> request = requestInfo.getRequest();
RequestContext requestContext = requestInfo.getRequestContext();
REST_CLIENT.sendRequest(request, requestContext, cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
Set<Long> responseIds = new HashSet<Long>();
for (BatchKVResponse<Long, UpdateStatus> response : responses) {
Set<Long> theseIds = response.getResults().keySet();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (Long id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(id));
responseIds.add(id);
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
Aggregations