use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testBatchUpdateWithErrors.
@Test
public void testBatchUpdateWithErrors() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
final Map<Long, Greeting> inputs = new HashMap<>();
inputs.put(-6L, getGreeting());
CompletionStage<Long> createResult = greetings.create(getGreeting());
CompletionStage<Map<Long, UpdateStatus>> result = createResult.thenCompose(id -> {
inputs.put(id, getGreeting("Batch update test"));
return greetings.batchUpdate(inputs);
});
CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
Map<Long, UpdateStatus> ids = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(ids.size(), 2);
Assert.assertEquals(ids.get(createResult.toCompletableFuture().get()).getStatus().intValue(), 204);
Assert.assertEquals(ids.get(-6L).getStatus().intValue(), 404);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method createBatchKVResponse.
private static Response<BatchKVResponse<Long, UpdateStatus>> createBatchKVResponse(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 UpdateStatus().setStatus(HttpStatus.S_200_OK.getCode()).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);
BatchKVResponse<Long, UpdateStatus> response = new BatchKVResponse<>(mergedMap, new TypeSpec<>(Long.class), new TypeSpec<>(UpdateStatus.class), Collections.emptyMap(), version);
return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
use of com.linkedin.restli.common.UpdateStatus in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method testUnsupportedNullKeyMap.
/* Note that we use also need to test using java.util.concurrent.ConcurrentHashMap. This is because rest.li checks
* for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
* is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
* java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
* way that doesn't throw an NullPointerException.
*/
@Test(dataProvider = "unsupportedNullKeyMapData")
@SuppressWarnings("unchecked")
public void testUnsupportedNullKeyMap(Object results, ProtocolVersion protocolVersion, Map<String, UpdateStatus> expectedResults) {
ServerResourceContext mockContext = getMockResourceContext(protocolVersion, null);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData<BatchUpdateResponseEnvelope> responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.emptyList());
RestLiResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertEquals(batchResponse.getResults(), expectedResults);
}
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> 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<>();
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 TestRestLiScatterGather method testSendSGDeleteRequests.
// BatchDeleteRequest
private static void testSendSGDeleteRequests(RestClient restClient, Long[] requestIds, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
@SuppressWarnings("unchecked") BatchDeleteRequest<Long, Greeting> request = (BatchDeleteRequest<Long, Greeting>) builders.batchDelete().ids(requestIds).setParam("foo", "bar").build();
BatchKVResponse<Long, UpdateStatus> result = restClient.sendRequest(request).getResponse().getEntity();
Assert.assertEquals(result.getResults().size(), requestIds.length);
UpdateStatus item = result.getResults().values().iterator().next();
Assert.assertFalse(item.hasError());
Assert.assertEquals(result.getErrors().size(), 0);
}
Aggregations