use of com.linkedin.restli.examples.greetings.client.GreetingsFluentClient in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testCollectionEntityActionWithReturn.
// ----- Tests with actions ------
@Test
public void testCollectionEntityActionWithReturn() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
Greeting newGreeting;
newGreeting = greetings.updateTone(1L, param -> param.setNewTone(Tone.SINCERE).setDelOld(false)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(newGreeting);
Assert.assertEquals(newGreeting.getId().longValue(), 1L);
Assert.assertEquals(newGreeting.getTone(), Tone.SINCERE);
newGreeting = greetings.updateTone(1L, param -> param.setNewTone(Tone.INSULTING)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertNotNull(newGreeting);
Assert.assertEquals(newGreeting.getId().longValue(), 1L);
Assert.assertEquals(newGreeting.getTone(), Tone.INSULTING);
}
use of com.linkedin.restli.examples.greetings.client.GreetingsFluentClient in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testDeleteFailure.
@Test
public void testDeleteFailure() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
CompletionStage<Void> result = greetings.delete(-1L);
CompletableFuture<Void> future = result.toCompletableFuture();
try {
future.get(5000, TimeUnit.MILLISECONDS);
Assert.fail("Expected failure");
} catch (ExecutionException e) {
Assert.assertEquals(((RestLiResponseException) e.getCause()).getStatus(), 404);
}
}
use of com.linkedin.restli.examples.greetings.client.GreetingsFluentClient in project rest.li by linkedin.
the class TestParSeqBasedFluentClientApiWithExecutionGroup method testBatchGet.
/**
* This test is to test if the batch method has been triggered.
*
* Note: Currently ParSeqRestClient only support "batch" for gets.
*/
@Test
public void testBatchGet() throws Exception {
// Test 3+1 = 4 calls using the "runBatchOnClient" method in the fluent Client
GreetingsFluentClient greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
final List<CompletionStage<Greeting>> results = new ArrayList<>(TEST_BATCH_SIZE + 1);
greetings.runBatchOnClient(() -> {
try {
for (int i = 0; i < OVER_BATCH_SIZE_CALLS; i++) {
results.add(greetings.get((long) (i + 1)));
}
} catch (Exception e) {
throw new RuntimeException();
}
});
for (CompletionStage<Greeting> stage : results) {
stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
}
assert (results.get(0) instanceof ParSeqBasedCompletionStage);
Task<Greeting> t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
Assert.assertEquals(findTaskOccurrenceInTrace("greetings batch_get", t.getTrace()), OVER_BATCH_SIZE_CALLS / TEST_BATCH_SIZE);
Assert.assertEquals(findTaskOccurrenceInTrace("greetings get", t.getTrace()), OVER_BATCH_SIZE_CALLS + 1);
// Test 3+1 = 4 calls generating a new ExecutionGroup and explicitly call the methods with ExecutionGroup
results.clear();
ExecutionGroup eg = greetings.generateExecutionGroup();
for (int i = 0; i < OVER_BATCH_SIZE_CALLS; i++) {
results.add(greetings.get((long) (i + 1), eg));
}
assert (results.get(0) instanceof ParSeqBasedCompletionStage);
t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
Assert.assertFalse(t.isDone());
Assert.assertFalse(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
eg.execute();
for (CompletionStage<Greeting> stage : results) {
stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
}
Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
Assert.assertEquals(findTaskOccurrenceInTrace("greetings batch_get", t.getTrace()), OVER_BATCH_SIZE_CALLS / TEST_BATCH_SIZE);
Assert.assertEquals(findTaskOccurrenceInTrace("greetings get", t.getTrace()), OVER_BATCH_SIZE_CALLS + 1);
// Test 2( less than 3 ) calls using client's batchOn
results.clear();
greetings.runBatchOnClient(() -> {
try {
for (int i = 0; i < UNDER_BATCH_SIZE_CALLS; i++) {
results.add(greetings.get((long) (i + 1)));
}
} catch (Exception e) {
throw new RuntimeException();
}
});
for (CompletionStage<Greeting> stage : results) {
stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
}
assert (results.get(0) instanceof ParSeqBasedCompletionStage);
t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
// Test 2( less than 3 ) calls execution group
results.clear();
eg = greetings.generateExecutionGroup();
for (int i = 0; i < UNDER_BATCH_SIZE_CALLS; i++) {
results.add(greetings.get((long) (i + 1), eg));
}
assert (results.get(0) instanceof ParSeqBasedCompletionStage);
t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
Assert.assertFalse(t.isDone());
Assert.assertFalse(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
eg.execute();
for (CompletionStage<Greeting> stage : results) {
stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
}
Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
// Testing read the completion Stage within the runnable: Not allowed; should fail
results.clear();
try {
greetings.runBatchOnClient(() -> {
try {
results.add(greetings.get(1L));
results.get(0).toCompletableFuture().get(500, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Assert.fail("Should fail: the CompletionStage cannot be read ");
} catch (Exception e) {
Assert.assertTrue(e.getCause() instanceof TimeoutException);
}
}
use of com.linkedin.restli.examples.greetings.client.GreetingsFluentClient in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testCreateAndThenBatchDelete.
@Test
public void testCreateAndThenBatchDelete() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
// Create entities first so we don't delete those used by other tests.
CompletionStage<List<CreateIdStatus<Long>>> createResult = greetings.batchCreate(Arrays.asList(getGreeting(), getGreeting()));
CompletionStage<Map<Long, UpdateStatus>> result = createResult.thenCompose(ids -> greetings.batchDelete(Sets.newHashSet(ids.stream().map(CreateIdStatus::getKey).collect(Collectors.toList()))));
CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
Map<Long, UpdateStatus> ids = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(ids.size(), 2);
for (UpdateStatus status : ids.values()) {
Assert.assertEquals(status.getStatus().intValue(), 204);
}
}
use of com.linkedin.restli.examples.greetings.client.GreetingsFluentClient in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testPartialUpdateAfterCreateAndGet.
@Test
public void testPartialUpdateAfterCreateAndGet() throws Exception {
Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
// Create a new greeting to use for partial update test.
CompletionStage<Long> createResult = greetings.create(getGreeting());
CompletionStage<Void> updateResult = createResult.thenCompose(greetings::get).thenCompose(greeting -> {
try {
Greeting update = greeting.copy();
greeting.setMessage("Partial update message");
return greetings.partialUpdate(1L, PatchGenerator.diff(greeting, update));
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<Void> future = updateResult.toCompletableFuture();
future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertFalse(future.isCompletedExceptionally());
}
Aggregations