use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method testUpdateStatusInstantiation.
@Test(dataProvider = "updateStatusInstantiation")
public void testUpdateStatusInstantiation(RestLiResponseData<BatchUpdateResponseEnvelope> responseData, UpdateStatus expectedResult) {
ServerResourceContext mockContext = getMockResourceContext(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), null);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
RestLiResponse response = new BatchUpdateResponseBuilder(new ErrorResponseBuilder()).buildResponse(routingResult, responseData);
Assert.assertEquals(((BatchResponse<?>) response.getEntity()).getResults().get("key"), expectedResult);
}
use of com.linkedin.restli.common.BatchResponse 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.BatchResponse in project rest.li by linkedin.
the class MockBatchResponseFactory method create.
/**
* Creates a {@link com.linkedin.restli.common.BatchResponse}
*
* @param entryClass the class of the elements stored in this {@link com.linkedin.restli.common.BatchResponse}
* @param recordTemplates the elements to be stored in this {@link com.linkedin.restli.common.BatchResponse}
* @param <T> class of the elements stored in this {@link com.linkedin.restli.common.BatchResponse}
* @return a {@link com.linkedin.restli.common.BatchResponse} with the above properties
*/
public static <T extends RecordTemplate> BatchResponse<T> create(Class<T> entryClass, Map<String, T> recordTemplates) {
DataMap batchResponseDataMap = new DataMap();
DataMap rawBatchData = new DataMap();
batchResponseDataMap.put(BatchResponse.RESULTS, rawBatchData);
for (Map.Entry<String, T> entry : recordTemplates.entrySet()) {
rawBatchData.put(entry.getKey(), entry.getValue().data());
}
return new BatchResponse<>(batchResponseDataMap, entryClass);
}
use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.
the class TestCompressionServer method getOldCookbookBatchGetResult.
private Greeting getOldCookbookBatchGetResult(RestClient client, RestliRequestOptions requestOptions) throws RemoteInvocationException {
Request<BatchResponse<Greeting>> request = new GreetingsBuilders(requestOptions).batchGet().ids(1L).build();
ResponseFuture<BatchResponse<Greeting>> future = client.sendRequest(request);
Response<BatchResponse<Greeting>> greetingResponse = future.getResponse();
checkContentEncodingHeaderIsAbsent(greetingResponse);
return greetingResponse.getEntity().getResults().get("1");
}
use of com.linkedin.restli.common.BatchResponse in project rest.li by linkedin.
the class TestScatterGather method testSendGetSGRequests.
private static void testSendGetSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds) throws ServiceUnavailableException, InterruptedException {
Collection<ScatterGatherBuilder.RequestInfo<Greeting>> scatterGatherRequests = buildScatterGatherGetRequests(sg, requestIds);
final Map<String, Greeting> results = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<>();
final List<BatchResponse<Greeting>> responses = new ArrayList<>();
for (ScatterGatherBuilder.RequestInfo<Greeting> requestInfo : scatterGatherRequests) {
Callback<Response<BatchResponse<Greeting>>> cb = new Callback<Response<BatchResponse<Greeting>>>() {
@Override
public void onSuccess(Response<BatchResponse<Greeting>> 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();
}
};
REST_CLIENT.sendRequest(requestInfo.getRequest(), requestInfo.getRequestContext(), cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<String>> responseIdSets = new HashSet<>();
Set<Long> responseIds = new HashSet<>();
for (BatchResponse<Greeting> response : responses) {
Set<String> theseIds = response.getResults().keySet();
// no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (String id : theseIds) {
// no duplicate ids
Assert.assertFalse(responseIds.contains(Long.parseLong(id)));
responseIds.add(Long.parseLong(id));
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
Aggregations