Search in sources :

Example 1 with SubgreetingsFluentClient

use of com.linkedin.restli.examples.greetings.client.SubgreetingsFluentClient in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testSubResource_noPathKey.

/**
 * Test {@link com.linkedin.restli.examples.greetings.server.CollectionUnderSimpleResource}
 * A complete set of request tests were tested in {@link TestSimpleResourceHierarchy}
 */
@Test
public void testSubResource_noPathKey() throws Exception {
    SubgreetingsFluentClient subgreetingsFluentClient = new SubgreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    CompletableFuture<Greeting> future = subgreetingsFluentClient.get(1L).toCompletableFuture();
    Greeting greeting = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(greeting.hasId());
    Assert.assertEquals((Long) 1L, greeting.getId());
    List<Long> ids = Arrays.asList(1L, 2L, 3L, 4L);
    Map<Long, EntityResponse<Greeting>> response = subgreetingsFluentClient.batchGet(new HashSet<>(ids)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(response.size(), ids.size());
    // Update
    String updatedMessage = "updated";
    greeting.setMessage(updatedMessage);
    CompletionStage<Void> updateStage = subgreetingsFluentClient.update(1L, greeting).thenRun(() -> {
        try {
            Assert.assertEquals(subgreetingsFluentClient.get(1L).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS), greeting);
        } catch (Exception e) {
            Assert.fail("Unexpected error");
        }
    });
    updateStage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertFalse(updateStage.toCompletableFuture().isCompletedExceptionally());
    // Partial update
    Greeting update = greeting.copy();
    String partialUpdateMessage = "Partial update message";
    update.setMessage(partialUpdateMessage);
    CompletionStage<Void> partialUpdateResult = subgreetingsFluentClient.partialUpdate(1L, PatchGenerator.diff(greeting, update));
    partialUpdateResult.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(subgreetingsFluentClient.get(1L).toCompletableFuture().get(500, TimeUnit.MILLISECONDS).getMessage(), partialUpdateMessage);
    Assert.assertFalse(partialUpdateResult.toCompletableFuture().isCompletedExceptionally());
    // create
    String msg = Double.toString(Math.random());
    CompletionStage<Long> result = subgreetingsFluentClient.create(getGreeting(msg));
    CompletableFuture<Long> createFuture = result.toCompletableFuture();
    Long createdId = createFuture.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(subgreetingsFluentClient.get(createdId).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS).getMessage().equals(msg));
    // batch create
    String msg1 = Double.toString(Math.random());
    String msg2 = Double.toString(Math.random());
    CompletionStage<List<CreateIdStatus<Long>>> batchCreateStage = subgreetingsFluentClient.batchCreate(Arrays.asList(getGreeting(msg1), getGreeting(msg2)));
    CompletableFuture<List<CreateIdStatus<Long>>> batchCreateFuture = batchCreateStage.toCompletableFuture();
    List<CreateIdStatus<Long>> createdList = batchCreateFuture.get(5000, TimeUnit.MILLISECONDS);
    CompletionStage<Map<Long, EntityResponse<Greeting>>> batchGetStage = subgreetingsFluentClient.batchGet(createdList.stream().map(CreateIdStatus::getKey).collect(Collectors.toSet()));
    Map<Long, EntityResponse<Greeting>> entities = batchGetStage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(entities.size(), 2);
}
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) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) SubgreetingsFluentClient(com.linkedin.restli.examples.greetings.client.SubgreetingsFluentClient) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) Test(org.testng.annotations.Test)

Aggregations

StringMap (com.linkedin.data.template.StringMap)1 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)1 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)1 EntityResponse (com.linkedin.restli.common.EntityResponse)1 IdEntityResponse (com.linkedin.restli.common.IdEntityResponse)1 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)1 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)1 SubgreetingsFluentClient (com.linkedin.restli.examples.greetings.client.SubgreetingsFluentClient)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.testng.annotations.Test)1