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);
}
Aggregations