Search in sources :

Example 11 with EntityResponse

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

the class TestCustomTypesClient method testCollectionBatchGetEntity.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchGetEntity(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Request<BatchKVResponse<CustomLong, EntityResponse<Greeting>>> request = new CustomTypes2RequestBuilders(requestOptions).batchGet().ids(new CustomLong(1L), new CustomLong(2L), new CustomLong(3L)).build();
    Map<CustomLong, EntityResponse<Greeting>> greetings = getClient().sendRequest(request).getResponse().getEntity().getResults();
    Assert.assertEquals(greetings.size(), 3);
    Assert.assertEquals(greetings.get(new CustomLong(1L)).getEntity().getId().longValue(), 1L);
    Assert.assertEquals(greetings.get(new CustomLong(2L)).getEntity().getId().longValue(), 2L);
    Assert.assertEquals(greetings.get(new CustomLong(3L)).getEntity().getId().longValue(), 3L);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EntityResponse(com.linkedin.restli.common.EntityResponse) CustomTypes2RequestBuilders(com.linkedin.restli.examples.greetings.client.CustomTypes2RequestBuilders) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 12 with EntityResponse

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

the class TestEscapeCharsInStringKeys method testBatchGetEntityWithSimpleKey.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestStringKeysOptionsDataProvider")
public void testBatchGetEntityWithSimpleKey(RestliRequestOptions requestOptions) throws Exception {
    Set<String> keys = new HashSet<>();
    keys.add(key1());
    keys.add(key2());
    Request<BatchKVResponse<String, EntityResponse<Message>>> req = new StringKeysRequestBuilders(requestOptions).batchGet().ids(keys).build();
    BatchKVResponse<String, EntityResponse<Message>> response = getClient().sendRequest(req).get().getEntity();
    Map<String, EntityResponse<Message>> results = response.getResults();
    Assert.assertEquals(results.get(key1()).getEntity().getMessage(), key1(), "Message should match key for key1");
    Assert.assertEquals(results.get(key2()).getEntity().getMessage(), key2(), "Message should match key for key2");
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) EntityResponse(com.linkedin.restli.common.EntityResponse) StringKeysRequestBuilders(com.linkedin.restli.examples.greetings.client.StringKeysRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 13 with EntityResponse

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

the class TestGroupsClient method testComplexKeyBatchCreateGetUpdateDelete.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestComplexBuilderDataProvider")
public void testComplexKeyBatchCreateGetUpdateDelete(ProtocolVersion version, RootBuilderWrapper<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> builders) throws RemoteInvocationException {
    ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey1 = buildComplexKey(1, 1, 10, "String1");
    ComplexKeyGroupMembership groupMembership1 = buildComplexKeyGroupMembership(complexKey1.getKey(), "alfred@test.linkedin.com", "alfred", "hitchcock");
    ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey2 = buildComplexKey(2, 1, 20, "String2");
    ComplexKeyGroupMembership groupMembership2 = buildComplexKeyGroupMembership(complexKey2.getKey(), "bruce@test.linkedin.com", "bruce", "willis");
    ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey3 = buildComplexKey(3, 1, 30, "String3");
    ComplexKeyGroupMembership groupMembership3 = buildComplexKeyGroupMembership(complexKey3.getKey(), "carole@test.linkedin.com", "carole", "bouquet");
    Request<CollectionResponse<CreateStatus>> createRequest = builders.batchCreate().input(groupMembership1).input(groupMembership2).input(groupMembership3).build();
    Response<CollectionResponse<CreateStatus>> createResponse = getClient().sendRequest(createRequest).getResponse();
    Assert.assertEquals(createResponse.getStatus(), 200);
    final RestliRequestOptions requestOptions = builders.getRequestOptions();
    @SuppressWarnings("unchecked") Request<BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>>> request = new GroupMembershipsComplexRequestBuilders(requestOptions).batchGet().ids(complexKey1, complexKey2, complexKey3).fields(GroupMembership.fields().contactEmail()).build();
    BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>> groupMemberships = getClient().sendRequest(request).getResponse().getEntity();
    Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>> results = groupMemberships.getResults();
    ComplexKeyGroupMembership groupMembership1_ = results.get(complexKey1).getEntity();
    ComplexKeyGroupMembership groupMembership2_ = results.get(complexKey2).getEntity();
    ComplexKeyGroupMembership groupMembership3_ = results.get(complexKey3).getEntity();
    Assert.assertNotNull(groupMembership1_);
    Assert.assertEquals(groupMembership1_.getContactEmail(), "alfred@test.linkedin.com");
    Assert.assertNotNull(groupMembership2_);
    Assert.assertEquals(groupMembership2_.getContactEmail(), "bruce@test.linkedin.com");
    Assert.assertNotNull(groupMembership3_);
    Assert.assertEquals(groupMembership3_.getContactEmail(), "carole@test.linkedin.com");
    // Update and verify
    groupMembership1.setContactEmail("alfred+@test.linkedin.com");
    groupMembership2.setContactEmail("bruce+@test.linkedin.com");
    groupMembership3.setContactEmail("carole+@test.linkedin.com");
    Request<BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, UpdateStatus>> updateRequest = builders.batchUpdate().input(complexKey1, groupMembership1).input(complexKey2, groupMembership2).input(complexKey3, groupMembership3).build();
    int status = getClient().sendRequest(updateRequest).getResponse().getStatus();
    Assert.assertEquals(status, 200);
}
Also used : RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) GroupMembershipsComplexRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsComplexRequestBuilders) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) CollectionResponse(com.linkedin.restli.common.CollectionResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) Test(org.testng.annotations.Test)

Example 14 with EntityResponse

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

the class BatchGetResponseBuilder method buildResponse.

@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
    final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
    // Build the EntityResponse for each key from the merged map with mask from routingResult.
    Map<Object, EntityResponse<RecordTemplate>> entityBatchResponse = buildEntityResponse(routingResult, responses);
    PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
    final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
    @SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(entityBatchResponse, protocolVersion);
    builder.entity(response);
    return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) EntityResponse(com.linkedin.restli.common.EntityResponse) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 15 with EntityResponse

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

the class GetRequestGroup method doExecuteBatchGet.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <K, RT extends RecordTemplate> void doExecuteBatchGet(final Client client, final Batch<RestRequestBatchKey, Response<Object>> batch, final Set<Object> ids, final Set<PathSpec> fields, Function<Request<?>, RequestContext> requestContextProvider) {
    final BatchGetEntityRequestBuilder<K, RT> builder = new BatchGetEntityRequestBuilder<>(_baseUriTemplate, _resourceSpec, _requestOptions);
    builder.setHeaders(_headers);
    _queryParams.forEach((key, value) -> builder.setParam(key, value));
    _pathKeys.forEach((key, value) -> builder.pathKey(key, value));
    builder.ids((Set<K>) ids);
    if (fields != null && !fields.isEmpty()) {
        builder.fields(fields.toArray(new PathSpec[fields.size()]));
    }
    final BatchGetEntityRequest<K, RT> batchGet = builder.build();
    client.sendRequest(batchGet, requestContextProvider.apply(batchGet), new Callback<Response<BatchKVResponse<K, EntityResponse<RT>>>>() {

        @Override
        public void onSuccess(Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch) {
            final ProtocolVersion version = ProtocolVersionUtil.extractProtocolVersion(responseToBatch.getHeaders());
            batch.entries().stream().forEach(entry -> {
                try {
                    RestRequestBatchKey rrbk = entry.getKey();
                    Request request = rrbk.getRequest();
                    if (request instanceof GetRequest) {
                        successGet((GetRequest) request, responseToBatch, batchGet, entry, version);
                    } else if (request instanceof BatchGetKVRequest) {
                        successBatchGetKV((BatchGetKVRequest) request, responseToBatch, entry, version);
                    } else if (request instanceof BatchGetRequest) {
                        successBatchGet((BatchGetRequest) request, responseToBatch, entry, version);
                    } else if (request instanceof BatchGetEntityRequest) {
                        successBatchGetEntity((BatchGetEntityRequest) request, responseToBatch, entry, version);
                    } else {
                        entry.getValue().getPromise().fail(unsupportedGetRequestType(request));
                    }
                } catch (RemoteInvocationException e) {
                    entry.getValue().getPromise().fail(e);
                }
            });
        }

        @SuppressWarnings({ "deprecation" })
        private void successBatchGetEntity(BatchGetEntityRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
            Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
            DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
            BatchKVResponse br = new BatchEntityResponse<>(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
            Response rsp = new ResponseImpl(responseToBatch, br);
            entry.getValue().getPromise().done(rsp);
        }

        private void successBatchGet(BatchGetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
            Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
            DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
            BatchResponse br = new BatchResponse<>(dm, request.getResponseDecoder().getEntityClass());
            Response rsp = new ResponseImpl(responseToBatch, br);
            entry.getValue().getPromise().done(rsp);
        }

        @SuppressWarnings({ "deprecation" })
        private void successBatchGetKV(BatchGetKVRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) {
            Set<String> ids = (Set<String>) request.getObjectIds().stream().map(o -> BatchResponse.keyToString(o, version)).collect(Collectors.toSet());
            DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids);
            BatchKVResponse br = new BatchKVResponse(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
            Response rsp = new ResponseImpl(responseToBatch, br);
            entry.getValue().getPromise().done(rsp);
        }

        @SuppressWarnings({ "deprecation" })
        private void successGet(GetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, final BatchGetEntityRequest<K, RT> batchGet, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) throws RemoteInvocationException {
            String idString = BatchResponse.keyToString(request.getObjectId(), version);
            Object id = ResponseUtils.convertKey(idString, request.getResourceSpec().getKeyType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version);
            Response rsp = unbatchResponse(batchGet, responseToBatch, id);
            entry.getValue().getPromise().done(rsp);
        }

        @Override
        public void onError(Throwable e) {
            batch.failAll(e);
        }
    });
}
Also used : ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) ResourceMethod(com.linkedin.restli.common.ResourceMethod) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Callback(com.linkedin.common.callback.Callback) Function(java.util.function.Function) BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) HashSet(java.util.HashSet) DataMap(com.linkedin.data.DataMap) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) RestConstants(com.linkedin.restli.common.RestConstants) Map(java.util.Map) EntityResponse(com.linkedin.restli.common.EntityResponse) PathSpec(com.linkedin.data.schema.PathSpec) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ProtocolVersionUtil(com.linkedin.restli.internal.common.ProtocolVersionUtil) RecordTemplate(com.linkedin.data.template.RecordTemplate) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) Logger(org.slf4j.Logger) ResourceSpec(com.linkedin.restli.common.ResourceSpec) BatchEntry(com.linkedin.parseq.batching.BatchImpl.BatchEntry) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) Set(java.util.Set) Batch(com.linkedin.parseq.batching.Batch) Tuple3(com.linkedin.parseq.function.Tuple3) Tuples(com.linkedin.parseq.function.Tuples) Collectors(java.util.stream.Collectors) RequestContext(com.linkedin.r2.message.RequestContext) ErrorResponse(com.linkedin.restli.common.ErrorResponse) HttpStatus(com.linkedin.restli.common.HttpStatus) Entry(java.util.Map.Entry) ResponseUtils(com.linkedin.restli.internal.common.ResponseUtils) BatchResponse(com.linkedin.restli.common.BatchResponse) HashSet(java.util.HashSet) Set(java.util.Set) BatchResponse(com.linkedin.restli.common.BatchResponse) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) PathSpec(com.linkedin.data.schema.PathSpec) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) BatchEntry(com.linkedin.parseq.batching.BatchImpl.BatchEntry) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) BatchResponse(com.linkedin.restli.common.BatchResponse) BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse)

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