Search in sources :

Example 11 with GreetingsFluentClient

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);
}
Also used : ComplexKeysSubFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysSubFluentClient) Arrays(java.util.Arrays) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Date(java.util.Date) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Test(org.testng.annotations.Test) AssociationsSubFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsSubFluentClient) GreetingFluentClient(com.linkedin.restli.examples.greetings.client.GreetingFluentClient) AssociationsAssociationsSubFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsAssociationsSubFluentClient) TransferOwnershipRequest(com.linkedin.restli.examples.groups.api.TransferOwnershipRequest) Groups(com.linkedin.restli.examples.groups.client.Groups) PatchGenerator(com.linkedin.restli.client.util.PatchGenerator) EntityResponse(com.linkedin.restli.common.EntityResponse) Map(java.util.Map) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) BatchfindersFluentClient(com.linkedin.restli.examples.greetings.client.BatchfindersFluentClient) AssociationAltKey(com.linkedin.restli.examples.greetings.client.AssociationAltKey) MessageCriteriaArray(com.linkedin.restli.examples.greetings.api.MessageCriteriaArray) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) BeforeClass(org.testng.annotations.BeforeClass) Set(java.util.Set) Collectors(java.util.stream.Collectors) Associations(com.linkedin.restli.examples.greetings.client.Associations) GroupsFluentClient(com.linkedin.restli.examples.groups.client.GroupsFluentClient) ParSeqRestliClientConfigBuilder(com.linkedin.restli.client.ParSeqRestliClientConfigBuilder) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ParSeqRestliClientBuilder(com.linkedin.restli.client.ParSeqRestliClientBuilder) Actions(com.linkedin.restli.examples.greetings.client.Actions) CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) SubgreetingsFluentClient(com.linkedin.restli.examples.greetings.client.SubgreetingsFluentClient) ComplexKeys(com.linkedin.restli.examples.greetings.client.ComplexKeys) TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) AssociationsAssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsAssociationsFluentClient) MessageCriteria(com.linkedin.restli.examples.greetings.api.MessageCriteria) AssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsFluentClient) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BooleanArray(com.linkedin.data.template.BooleanArray) ArrayList(java.util.ArrayList) CustomTypes2(com.linkedin.restli.examples.greetings.client.CustomTypes2) HashSet(java.util.HashSet) PatchRequest(com.linkedin.restli.common.PatchRequest) CustomTypes3(com.linkedin.restli.examples.greetings.client.CustomTypes3) CustomTypes3FluentClient(com.linkedin.restli.examples.greetings.client.CustomTypes3FluentClient) CustomTypes2FluentClient(com.linkedin.restli.examples.greetings.client.CustomTypes2FluentClient) Assert(org.testng.Assert) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) StringMap(com.linkedin.data.template.StringMap) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ParSeqUnitTestHelper(com.linkedin.parseq.ParSeqUnitTestHelper) ComplexKeysFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysFluentClient) Tone(com.linkedin.restli.examples.greetings.api.Tone) PartialUpdateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) AfterClass(org.testng.annotations.AfterClass) ActionsFluentClient(com.linkedin.restli.examples.greetings.client.ActionsFluentClient) BatchFinderCriteriaResult(com.linkedin.restli.common.BatchFinderCriteriaResult) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) GreetingCriteria(com.linkedin.restli.examples.greetings.api.GreetingCriteria) CompoundKey(com.linkedin.restli.common.CompoundKey) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CreateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.CreateGreetingFluentClient) ParSeqRestliClient(com.linkedin.restli.client.ParSeqRestliClient) BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) HttpStatus(com.linkedin.restli.common.HttpStatus) AssociationAltKeyFluentClient(com.linkedin.restli.examples.greetings.client.AssociationAltKeyFluentClient) Message(com.linkedin.restli.examples.greetings.api.Message) Batchfinders(com.linkedin.restli.examples.greetings.client.Batchfinders) Sets(org.testng.collections.Sets) CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) Test(org.testng.annotations.Test)

Example 12 with GreetingsFluentClient

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);
    }
}
Also used : Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 13 with GreetingsFluentClient

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);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ArrayList(java.util.ArrayList) ExecutionGroup(com.linkedin.restli.client.ExecutionGroup) TimeoutException(java.util.concurrent.TimeoutException) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) ParSeqBasedCompletionStage(com.linkedin.restli.client.ParSeqBasedCompletionStage) CompletionStage(java.util.concurrent.CompletionStage) ParSeqBasedCompletionStage(com.linkedin.restli.client.ParSeqBasedCompletionStage) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 14 with GreetingsFluentClient

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);
    }
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) 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)

Example 15 with GreetingsFluentClient

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());
}
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) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) Test(org.testng.annotations.Test)

Aggregations

GreetingsFluentClient (com.linkedin.restli.examples.greetings.client.GreetingsFluentClient)26 Test (org.testng.annotations.Test)26 Greetings (com.linkedin.restli.examples.greetings.client.Greetings)25 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)16 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)15 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)15 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)15 HashMap (java.util.HashMap)12 Map (java.util.Map)12 List (java.util.List)11 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)10 ExecutionException (java.util.concurrent.ExecutionException)10 StringMap (com.linkedin.data.template.StringMap)9 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)9 ArrayList (java.util.ArrayList)9 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)8 CollectionResponse (com.linkedin.restli.common.CollectionResponse)8 EntityResponse (com.linkedin.restli.common.EntityResponse)8 IdEntityResponse (com.linkedin.restli.common.IdEntityResponse)8 UpdateStatus (com.linkedin.restli.common.UpdateStatus)7