Search in sources :

Example 6 with TwoPartKey

use of com.linkedin.restli.examples.greetings.api.TwoPartKey in project rest.li by linkedin.

the class ComplexKeysDataProvider method create.

public ComplexResourceKey<TwoPartKey, TwoPartKey> create(Message message) {
    TwoPartKey key = new TwoPartKey();
    key.setMajor(message.getMessage());
    key.setMinor(message.getMessage());
    _db.put(keyToString(key), message);
    return new ComplexResourceKey<TwoPartKey, TwoPartKey>(key, new TwoPartKey());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 7 with TwoPartKey

use of com.linkedin.restli.examples.greetings.api.TwoPartKey in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchGetEntityEmpty.

@SuppressWarnings("deprecation")
public void testBatchGetEntityEmpty(BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> builder) throws Exception {
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> request = builder.build();
    final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
    getClient().sendRestRequest(request, new RequestContext(), callback);
    final RestResponse result = callback.get();
    final DataMap responseMap = DataMapUtils.readMap(result);
    final DataMap resultsMap = responseMap.getDataMap(BatchKVResponse.RESULTS);
    Assert.assertNotNull(resultsMap, "Response does not contain results map");
    Assert.assertTrue(resultsMap.isEmpty());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestContext(com.linkedin.r2.message.RequestContext) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) DataMap(com.linkedin.data.DataMap)

Example 8 with TwoPartKey

use of com.linkedin.restli.examples.greetings.api.TwoPartKey in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchCreateMain.

private void testBatchCreateMain(BatchCreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText1 = "firstMessage";
    Message message1 = new Message();
    message1.setMessage(messageText1);
    final String messageText2 = "secondMessage";
    Message message2 = new Message();
    message2.setMessage(messageText2);
    List<Message> messages = new ArrayList<Message>(2);
    messages.add(message1);
    messages.add(message2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
    // test build
    Request<CollectionResponse<CreateStatus>> request = batchCreateRequestBuilder.inputs(messages).build();
    ResponseFuture<CollectionResponse<CreateStatus>> future = getClient().sendRequest(request);
    Response<CollectionResponse<CreateStatus>> response = future.getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
    expectedComplexKeys.add(expectedComplexKey1);
    expectedComplexKeys.add(expectedComplexKey2);
    for (CreateStatus createStatus : response.getEntity().getElements()) {
        @SuppressWarnings("unchecked") CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> createIdStatus = (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>>) createStatus;
        Assert.assertEquals(createIdStatus.getStatus(), new Integer(201));
        Assert.assertTrue(expectedComplexKeys.contains(createIdStatus.getKey()));
        try {
            @SuppressWarnings("deprecation") String id = createIdStatus.getId();
            Assert.fail("buildReadOnlyId should throw an exception on ComplexKeys");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        expectedComplexKeys.remove(createIdStatus.getKey());
    }
    Assert.assertTrue(expectedComplexKeys.isEmpty());
    // attempt to batch get created records
    List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
    createdKeys.add(expectedComplexKey1);
    createdKeys.add(expectedComplexKey2);
    BatchGetKVRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequest = batchGetRequestBuilder.ids(createdKeys).buildKV();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getFuture = getClient().sendRequest(getRequest);
    Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getResponse = getFuture.getResponse();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getResults = getResponse.getEntity().getResults();
    Assert.assertEquals(getResults.get(expectedComplexKey1), message1);
    Assert.assertEquals(getResults.get(expectedComplexKey2), message2);
    Assert.assertEquals(getResults.size(), 2);
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) HashSet(java.util.HashSet) TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 9 with TwoPartKey

use of com.linkedin.restli.examples.greetings.api.TwoPartKey in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchDeleteMain.

private void testBatchDeleteMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, EmptyRecord> createRequestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    String messageText = "m1";
    Message message = new Message();
    message.setMessage(messageText);
    Request<EmptyRecord> createRequest = createRequestBuilder.input(message).build();
    ResponseFuture<EmptyRecord> createFuture = getClient().sendRequest(createRequest);
    Response<EmptyRecord> createResponse = createFuture.getResponse();
    Assert.assertEquals(createResponse.getStatus(), 201);
    String messageText2 = "m2";
    message.setMessage(messageText2);
    createRequest = createRequestBuilder.input(message).build();
    createFuture = getClient().sendRequest(createRequest);
    createResponse = createFuture.getResponse();
    Assert.assertEquals(createResponse.getStatus(), 201);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(messageText, messageText);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(messageText2, messageText2);
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>();
    ids.add(key1);
    ids.add(key2);
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.ids(ids).build();
    final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
    final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
        Assert.assertEquals(resp.getValue().getStatus().intValue(), 204);
    }
    Assert.assertNotNull(response.getResults().get(key1));
    Assert.assertNotNull(response.getResults().get(key2));
    Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
    BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
    Assert.assertEquals(batchGetResponse.getResults().size(), batchGetResponse.getErrors().size());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) EmptyRecord(com.linkedin.restli.common.EmptyRecord) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Example 10 with TwoPartKey

use of com.linkedin.restli.examples.greetings.api.TwoPartKey in project rest.li by linkedin.

the class TestComplexKeysResource method testBatchPartialUpdateMain.

private void testBatchPartialUpdateMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    Message message = new Message();
    message.setTone(Tone.FRIENDLY);
    PatchRequest<Message> patch = PatchGenerator.diffEmpty(message);
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>> inputs = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>>();
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
    inputs.put(key1, patch);
    inputs.put(key2, patch);
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.patchInputs(inputs).build();
    final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
    final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
        Assert.assertTrue(inputs.containsKey(resp.getKey()));
        Assert.assertEquals(resp.getValue().getStatus().intValue(), 204);
    }
    Assert.assertNotNull(response.getResults().get(key1));
    Assert.assertNotNull(response.getResults().get(key2));
    Assert.assertTrue(response.getErrors().isEmpty());
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>();
    ids.add(key1);
    ids.add(key2);
    Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
    BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
    Assert.assertEquals(batchGetResponse.getResults().get(key1).getEntity().getTone(), Tone.FRIENDLY);
    Assert.assertEquals(batchGetResponse.getResults().get(key2).getEntity().getTone(), Tone.FRIENDLY);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Aggregations

TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)21 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)15 Message (com.linkedin.restli.examples.greetings.api.Message)13 HashMap (java.util.HashMap)7 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)5 DataMap (com.linkedin.data.DataMap)4 EntityResponse (com.linkedin.restli.common.EntityResponse)4 UpdateStatus (com.linkedin.restli.common.UpdateStatus)3 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)3 UpdateResponse (com.linkedin.restli.server.UpdateResponse)3 Test (org.testng.annotations.Test)3 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)2 EmptyRecord (com.linkedin.restli.common.EmptyRecord)2 PatchRequest (com.linkedin.restli.common.PatchRequest)2 CreateResponse (com.linkedin.restli.server.CreateResponse)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 HashSet (java.util.HashSet)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1