Search in sources :

Example 1 with EntityResponse

use of com.linkedin.restli.common.EntityResponse in project parseq by linkedin.

the class GetRequestGroup method unbatchResponse.

private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException {
    final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity();
    final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
    if (errorResponse != null) {
        throw new RestLiResponseException(errorResponse);
    }
    final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id);
    if (entityResponse != null) {
        final RT entityResult = entityResponse.getEntity();
        if (entityResult != null) {
            return new ResponseImpl<>(batchResponse, entityResult);
        }
    }
    LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id);
    throw NOT_FOUND_EXCEPTION;
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 2 with EntityResponse

use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.

the class TestBatchEntityResponseDecoder method testDecoding.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchEntityResponseDataProvider")
public void testDecoding(List<String> keys, ProtocolVersion protocolVersion) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    final String resultKey = keys.get(0);
    final String statusKey = keys.get(1);
    final String errorKey = keys.get(2);
    final DataMap resultData = new DataMap();
    resultData.put(resultKey, _record.data());
    final DataMap statusData = new DataMap();
    statusData.put(statusKey, _status.getCode());
    final DataMap errorData = new DataMap();
    errorData.put(errorKey, _error.data());
    final DataMap data = new DataMap();
    data.put(BatchResponse.RESULTS, resultData);
    data.put(BatchResponse.STATUSES, statusData);
    data.put(BatchResponse.ERRORS, errorData);
    final BatchEntityResponseDecoder<String, TestRecord> decoder = new BatchEntityResponseDecoder<>(new TypeSpec<>(TestRecord.class), new TypeSpec<>(String.class), Collections.<String, CompoundKey.TypeInfo>emptyMap(), null);
    final BatchKVResponse<String, EntityResponse<TestRecord>> response = decoder.wrapResponse(data, Collections.<String, String>emptyMap(), protocolVersion);
    final Map<String, EntityResponse<TestRecord>> results = response.getResults();
    final Map<String, ErrorResponse> errors = response.getErrors();
    final Collection<String> uniqueKeys = new HashSet<>(keys);
    Assert.assertEquals(results.size(), uniqueKeys.size());
    Assert.assertEquals(errors.size(), 1);
    Assert.assertEquals(results.get(resultKey).getEntity(), _record);
    Assert.assertEquals(results.get(statusKey).getStatus(), _status);
    Assert.assertEquals(results.get(errorKey).getError(), _error);
    Assert.assertEquals(errors.get(errorKey), _error);
    // Check that the response still contains the original data map
    Assert.assertEquals(response.data(), data);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with EntityResponse

use of com.linkedin.restli.common.EntityResponse 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<Long> requestIds = new HashSet<>();
    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<>();
        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) EntityResponse(com.linkedin.restli.common.EntityResponse) HashSet(java.util.HashSet)

Example 4 with EntityResponse

use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testBatchGetRequestWithPartialErrors.

@Test
public void testBatchGetRequestWithPartialErrors() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Set<Long> ids = Sets.newHashSet(Arrays.asList(-1L, -2L, 1L, 2L, 3L));
    CompletionStage<Map<Long, EntityResponse<Greeting>>> result = greetings.batchGet(ids);
    CompletableFuture<Map<Long, EntityResponse<Greeting>>> future = result.toCompletableFuture();
    Map<Long, EntityResponse<Greeting>> resultMap = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(resultMap.size(), ids.size());
    for (Long id : ids) {
        EntityResponse<Greeting> g = resultMap.get(id);
        Assert.assertNotNull(g);
        if (id > 0) {
            Assert.assertTrue(g.hasEntry());
            Assert.assertEquals(id, g.getEntity().getId());
        } else {
            Assert.assertTrue(g.hasError());
        }
    }
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) Test(org.testng.annotations.Test)

Example 5 with EntityResponse

use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testBatchGetRequest.

@Test
public void testBatchGetRequest() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Set<Long> ids = Sets.newHashSet(Arrays.asList(1L, 2L, 3L));
    CompletionStage<Map<Long, EntityResponse<Greeting>>> result = greetings.batchGet(ids);
    CompletableFuture<Map<Long, EntityResponse<Greeting>>> future = result.toCompletableFuture();
    Map<Long, EntityResponse<Greeting>> resultMap = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(resultMap.size(), ids.size());
    for (Long id : ids) {
        EntityResponse<Greeting> g = resultMap.get(id);
        Assert.assertNotNull(g);
        Assert.assertTrue(g.hasEntry());
        Assert.assertEquals(id, g.getEntity().getId());
    }
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) Test(org.testng.annotations.Test)

Aggregations

EntityResponse (com.linkedin.restli.common.EntityResponse)37 Test (org.testng.annotations.Test)21 HashMap (java.util.HashMap)18 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)17 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)14 Map (java.util.Map)12 DataMap (com.linkedin.data.DataMap)11 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)11 Message (com.linkedin.restli.examples.greetings.api.Message)10 IdEntityResponse (com.linkedin.restli.common.IdEntityResponse)9 ErrorResponse (com.linkedin.restli.common.ErrorResponse)7 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)7 ArrayList (java.util.ArrayList)7 CompoundKey (com.linkedin.restli.common.CompoundKey)6 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 HashSet (java.util.HashSet)6 HttpStatus (com.linkedin.restli.common.HttpStatus)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)4 ComplexKeys (com.linkedin.restli.examples.greetings.client.ComplexKeys)4