Search in sources :

Example 36 with BatchKVResponse

use of com.linkedin.restli.client.response.BatchKVResponse in project rest.li by linkedin.

the class TestCompressionServer method getNewCookbookBatchGetResult.

private Greeting getNewCookbookBatchGetResult(RestClient client, RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request = new GreetingsRequestBuilders(requestOptions).batchGet().ids(1L).build();
    ResponseFuture<BatchKVResponse<Long, EntityResponse<Greeting>>> future = client.sendRequest(request);
    Response<BatchKVResponse<Long, EntityResponse<Greeting>>> greetingResponse = future.getResponse();
    checkContentEncodingHeaderIsAbsent(greetingResponse);
    return greetingResponse.getEntity().getResults().get(1L).getEntity();
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse)

Example 37 with BatchKVResponse

use of com.linkedin.restli.client.response.BatchKVResponse in project rest.li by linkedin.

the class TestAssociationsResource method testBatchGet.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchGet(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Request<BatchKVResponse<CompoundKey, Message>> request = new AssociationsBuilders(requestOptions).batchGet().ids(DB.keySet()).buildKV();
    ResponseFuture<BatchKVResponse<CompoundKey, Message>> responseFuture = getClient().sendRequest(request);
    Response<BatchKVResponse<CompoundKey, Message>> response = responseFuture.getResponse();
    BatchKVResponse<CompoundKey, Message> entity = response.getEntity();
    Assert.assertEquals(entity.getErrors().size(), 0);
    Assert.assertEquals(entity.getResults().size(), 2);
    for (CompoundKey id : DB.keySet()) {
        Assert.assertTrue(entity.getResults().containsKey(id));
        Assert.assertEquals(entity.getResults().get(id), DB.get(id));
    }
}
Also used : AssociationsBuilders(com.linkedin.restli.examples.greetings.client.AssociationsBuilders) Message(com.linkedin.restli.examples.greetings.api.Message) CompoundKey(com.linkedin.restli.common.CompoundKey) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 38 with BatchKVResponse

use of com.linkedin.restli.client.response.BatchKVResponse in project rest.li by linkedin.

the class TestAssociationsResource method testBatchGetEntity.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchGetEntity(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Request<BatchKVResponse<CompoundKey, EntityResponse<Message>>> request = new AssociationsRequestBuilders(requestOptions).batchGet().ids(DB.keySet()).build();
    ResponseFuture<BatchKVResponse<CompoundKey, EntityResponse<Message>>> responseFuture = getClient().sendRequest(request);
    Response<BatchKVResponse<CompoundKey, EntityResponse<Message>>> response = responseFuture.getResponse();
    BatchKVResponse<CompoundKey, EntityResponse<Message>> entityResponse = response.getEntity();
    Assert.assertEquals(entityResponse.getErrors().size(), 0);
    Assert.assertEquals(entityResponse.getResults().size(), 2);
    for (CompoundKey id : DB.keySet()) {
        EntityResponse<Message> single = entityResponse.getResults().get(id);
        Assert.assertTrue(entityResponse.getResults().containsKey(id));
        Assert.assertEquals(single.getEntity(), DB.get(id));
    }
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) CompoundKey(com.linkedin.restli.common.CompoundKey) EntityResponse(com.linkedin.restli.common.EntityResponse) AssociationsRequestBuilders(com.linkedin.restli.examples.greetings.client.AssociationsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 39 with BatchKVResponse

use of com.linkedin.restli.client.response.BatchKVResponse 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);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) 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) RequestContext(com.linkedin.r2.message.RequestContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Example 40 with BatchKVResponse

use of com.linkedin.restli.client.response.BatchKVResponse in project rest.li by linkedin.

the class TestGreetingsClient method testNewCookbookInBatch.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testNewCookbookInBatch(RestliRequestOptions requestOptions) throws Exception {
    final GreetingsRequestBuilders builders = new GreetingsRequestBuilders(requestOptions);
    // GET
    final BatchGetEntityRequestBuilder<Long, Greeting> batchGetBuilder = builders.batchGet();
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request = batchGetBuilder.ids(1L).build();
    ResponseFuture<BatchKVResponse<Long, EntityResponse<Greeting>>> future = getClient().sendRequest(request);
    Response<BatchKVResponse<Long, EntityResponse<Greeting>>> greetingResponse = future.getResponse();
    // PUT
    Greeting greeting = new Greeting(greetingResponse.getEntity().getResults().get(1L).getEntity().data().copy());
    greeting.setMessage("This is a new message!");
    Request<BatchKVResponse<Long, UpdateStatus>> writeRequest = builders.batchUpdate().input(1L, greeting).build();
    getClient().sendRequest(writeRequest).getResponse();
    // GET again, to verify that our POST worked.
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request2 = builders.batchGet().ids(1L).build();
    ResponseFuture<BatchKVResponse<Long, EntityResponse<Greeting>>> future2 = getClient().sendRequest(request2);
    greetingResponse = future2.get();
    Greeting repeatedGreeting = new Greeting();
    repeatedGreeting.setMessage("Hello Hello");
    repeatedGreeting.setTone(Tone.SINCERE);
    List<Greeting> entities = Arrays.asList(repeatedGreeting, repeatedGreeting);
    Request<BatchCreateIdResponse<Long>> request3 = builders.batchCreate().inputs(entities).build();
    BatchCreateIdResponse<Long> statuses = getClient().sendRequest(request3).getResponse().getEntity();
    for (CreateIdStatus<Long> status : statuses.getElements()) {
        Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertEquals(status.getKey().longValue(), Long.parseLong(id));
        Assert.assertNotNull(status.getKey());
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) Test(org.testng.annotations.Test)

Aggregations

BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)57 Test (org.testng.annotations.Test)40 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)31 EntityResponse (com.linkedin.restli.common.EntityResponse)18 UpdateStatus (com.linkedin.restli.common.UpdateStatus)15 HashSet (java.util.HashSet)13 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)11 Message (com.linkedin.restli.examples.greetings.api.Message)11 ArrayList (java.util.ArrayList)11 CompoundKey (com.linkedin.restli.common.CompoundKey)10 DataMap (com.linkedin.data.DataMap)8 HashMap (java.util.HashMap)8 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)7 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)6 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)5 BatchResponse (com.linkedin.restli.common.BatchResponse)5 CollectionResponse (com.linkedin.restli.common.CollectionResponse)5 PatchRequest (com.linkedin.restli.common.PatchRequest)4 GreetingsRequestBuilders (com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders)4 RequestContext (com.linkedin.r2.message.RequestContext)3