Search in sources :

Example 1 with BatchKVResponse

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

the class MockBatchKVResponseFactory method createWithComplexKey.

/**
   * Create a {@link BatchKVResponse} where the key is a {@link ComplexResourceKey}
   *
   * @param valueClass the value class
   * @param keyKeyClass the class of the key part of the {@link ComplexResourceKey}
   * @param keyParamsClass the class of the params part of the {@link ComplexResourceKey}
   * @param recordTemplates the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getResults()}
   *                        NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                        instance of the params class is created with no data in it.
   * @param errorResponses the data that will be returned for a call to {@link com.linkedin.restli.client.response.BatchKVResponse#getErrors()}
   *                       NOTE: the params part of the {@link ComplexResourceKey} is removed in this map. A new
   *                       instance of the params class is created with no data in it.
   * @param <V>
   * @return
   */
@SuppressWarnings("rawtypes")
public static <KK extends RecordTemplate, KP extends RecordTemplate, V extends RecordTemplate> BatchKVResponse<ComplexResourceKey<KK, KP>, V> createWithComplexKey(Class<V> valueClass, Class<KK> keyKeyClass, Class<KP> keyParamsClass, Map<ComplexResourceKey<KK, KP>, V> recordTemplates, Map<ComplexResourceKey<KK, KP>, ErrorResponse> errorResponses) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    DataMap batchResponseDataMap = buildDataMap(recordTemplates, errorResponses, version);
    @SuppressWarnings("unchecked") BatchKVResponse<ComplexResourceKey<KK, KP>, V> response = (BatchKVResponse<ComplexResourceKey<KK, KP>, V>) (Object) new BatchKVResponse<ComplexResourceKey, V>(batchResponseDataMap, ComplexResourceKey.class, valueClass, null, keyKeyClass, keyParamsClass, version);
    return response;
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap)

Example 2 with BatchKVResponse

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

the class TestScatterGather method testBuildSGGetKVRequests.

private static void testBuildSGGetKVRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> requests = buildScatterGatherGetKVRequests(sg, ids);
    Assert.assertEquals(requests.size(), numEndpoints);
    Set<Set<String>> requestIdSets = new HashSet<Set<String>>();
    Set<Long> requestIds = new HashSet<Long>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, Greeting> requestInfo : requests) {
        //URI will be something like "greetings/?ids=21&ids=4&ids=53&ids=60&ids=66&ids=88&ids=93&foo=bar"
        BatchRequest<BatchKVResponse<Long, Greeting>> request = requestInfo.getRequest();
        Set<String> expectedParams = new HashSet<String>();
        expectedParams.add(RestConstants.QUERY_BATCH_IDS_PARAM);
        expectedParams.add("foo");
        expectedParams.add(RestConstants.FIELDS_PARAM);
        Set<PathSpec> expectedFields = Collections.singleton(new PathSpec("message"));
        testRequest(request, expectedParams, expectedFields, null, requestIdSets, requestIds);
    }
    Assert.assertTrue(requestIds.containsAll(Arrays.asList(ids)));
    Assert.assertEquals(requestIds.size(), ids.length);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Set(java.util.Set) HashSet(java.util.HashSet) PathSpec(com.linkedin.data.schema.PathSpec) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) HashSet(java.util.HashSet)

Example 3 with BatchKVResponse

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

the class TestResponseDecoder method testNonRestliServerErrorHandling.

/**
   * This test tests 2 things in combo here:
   * 1) BatchEntityResponseDecoder could be invoked in some cases to try to decode a empty response dataMap when
   * non-rest.li server error returns, in this test, we simulate that by passing a over-size URL param
   * {@link com.linkedin.restli.internal.client.ExceptionUtil#exceptionForThrowable(java.lang.Throwable, com.linkedin.restli.internal.client.RestResponseDecoder)}
   *
   * 2) CallbackAdapter and its subclasses could have error while its dealing with error itself, this test make sure it
   * pass the 'new' error to its inner callback's onError method.
   * {@link CallbackAdapter#onError(java.lang.Throwable)}
   */
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "dataProvider")
public void testNonRestliServerErrorHandling(RestliRequestOptions requestOptions) throws Exception {
    Set<String> keys = new HashSet<String>();
    keys.add(createDataSize(SERVER_HEADER_OVERLOAD_SIZE));
    BatchGetEntityRequest<String, Message> req = new StringKeysRequestBuilders(requestOptions).batchGet().ids(keys).build();
    ResponseFuture<BatchKVResponse<String, EntityResponse<Message>>> batchKVResponseResponseFuture = getClient().sendRequest(req);
    try {
        batchKVResponseResponseFuture.getResponse();
        Assert.fail("Exception should have thrown before this point!");
    } catch (Throwable e) {
        Assert.assertTrue(e instanceof RestLiResponseException);
        Assert.assertEquals(((RestLiResponseException) e).getStatus(), 414);
    }
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) StringKeysRequestBuilders(com.linkedin.restli.examples.greetings.client.StringKeysRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) HashSet(java.util.HashSet) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) Test(org.testng.annotations.Test)

Example 4 with BatchKVResponse

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

the class TestScatterGather method testSendGetKVSGRequests.

private static void testSendGetKVSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds) throws ServiceUnavailableException, InterruptedException {
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> scatterGatherRequests = buildScatterGatherGetKVRequests(sg, requestIds);
    final Map<Long, Greeting> results = new ConcurrentHashMap<Long, Greeting>();
    final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
    final List<Throwable> errors = new ArrayList<Throwable>();
    final List<BatchKVResponse<Long, Greeting>> responses = new ArrayList<BatchKVResponse<Long, Greeting>>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, Greeting> requestInfo : scatterGatherRequests) {
        Callback<Response<BatchKVResponse<Long, Greeting>>> cb = new Callback<Response<BatchKVResponse<Long, Greeting>>>() {

            @Override
            public void onSuccess(Response<BatchKVResponse<Long, 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<Long>> responseIdSets = new HashSet<Set<Long>>();
    Set<Long> responseIds = new HashSet<Long>();
    for (BatchKVResponse<Long, Greeting> 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 : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Example 5 with BatchKVResponse

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

the class TestScatterGather method testBuildSGGetEntityRequests.

private static void testBuildSGGetEntityRequests(int numEndpoints, ScatterGatherBuilder<Greeting> sg, Long[] ids) throws ServiceUnavailableException {
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> requests = buildScatterGatherGetEntityRequests(sg, ids);
    Assert.assertEquals(requests.size(), numEndpoints);
    Set<Set<String>> requestIdSets = new HashSet<Set<String>>();
    Set<Long> requestIds = new HashSet<Long>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>> requestInfo : requests) {
        //URI will be something like "greetings/?ids=21&ids=4&ids=53&ids=60&ids=66&ids=88&ids=93&foo=bar"
        BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> request = requestInfo.getRequest();
        Set<String> expectedParams = new HashSet<String>();
        expectedParams.add(RestConstants.QUERY_BATCH_IDS_PARAM);
        expectedParams.add("foo");
        expectedParams.add(RestConstants.FIELDS_PARAM);
        Set<PathSpec> expectedFields = Collections.singleton(new PathSpec("message"));
        testGetEntityRequest(request, expectedParams, expectedFields, null, requestIdSets, requestIds);
    }
    Assert.assertTrue(requestIds.containsAll(Arrays.asList(ids)));
    Assert.assertEquals(requestIds.size(), ids.length);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Set(java.util.Set) HashSet(java.util.HashSet) PathSpec(com.linkedin.data.schema.PathSpec) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) HashSet(java.util.HashSet)

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