Search in sources :

Example 26 with BatchResponse

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Example 27 with BatchResponse

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);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Test(org.testng.annotations.Test)

Example 28 with BatchResponse

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);
}
Also used : BatchResponse(com.linkedin.restli.common.BatchResponse) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 29 with BatchResponse

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");
}
Also used : GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) BatchResponse(com.linkedin.restli.common.BatchResponse)

Example 30 with BatchResponse

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);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Set(java.util.Set) HashSet(java.util.HashSet) BatchResponse(com.linkedin.restli.common.BatchResponse) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) EntityResponse(com.linkedin.restli.common.EntityResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) BatchResponse(com.linkedin.restli.common.BatchResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Callback(com.linkedin.common.callback.Callback) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Aggregations

BatchResponse (com.linkedin.restli.common.BatchResponse)32 Test (org.testng.annotations.Test)23 HashMap (java.util.HashMap)11 DataMap (com.linkedin.data.DataMap)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)9 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)9 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)9 Map (java.util.Map)8 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)6 ErrorResponse (com.linkedin.restli.common.ErrorResponse)6 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)5 EntityResponse (com.linkedin.restli.common.EntityResponse)4 HashSet (java.util.HashSet)4 Callback (com.linkedin.common.callback.Callback)3 Foo (com.linkedin.pegasus.generator.examples.Foo)3 Set (java.util.Set)3 PathSpec (com.linkedin.data.schema.PathSpec)2