Search in sources :

Example 1 with Tone

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

the class TestParseqBasedFluentClientApi method testAssociateResourceSpreadKeyAPI.

@Test
public void testAssociateResourceSpreadKeyAPI() throws Exception {
    // Use AssocationAltKeyResource and AltKeyDataProvider
    // Get
    AssociationAltKey client = new AssociationAltKeyFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    String msgKey = "c";
    Long longKey = 3L;
    Greeting res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(res.getTone(), Tone.FRIENDLY);
    Assert.assertEquals(res.getMessage(), msgKey);
    // Update
    String newMsg = "aa";
    res.setMessage(newMsg);
    client.update(longKey, msgKey, res).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(res.getMessage(), newMsg);
    // PartialUpdate
    Tone updatedTone = Tone.SINCERE;
    res.setTone(updatedTone);
    Greeting original = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    PatchRequest<Greeting> patch = PatchGenerator.diff(original, res);
    // Only tone differ
    client.partialUpdate(longKey, msgKey, patch).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    res = client.get(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(res.getMessage(), newMsg);
    Assert.assertEquals(res.getTone(), updatedTone);
    // Delete
    try {
        // that resource implementation does not allow deletion
        client.delete(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        Assert.assertEquals(((RestLiResponseException) e.getCause()).getStatus(), 404);
    }
    // Action
    Assert.assertEquals(client.testAction(longKey, msgKey).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS), "Hello!");
}
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) Tone(com.linkedin.restli.examples.greetings.api.Tone) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) AssociationAltKey(com.linkedin.restli.examples.greetings.client.AssociationAltKey) AssociationAltKeyFluentClient(com.linkedin.restli.examples.greetings.client.AssociationAltKeyFluentClient) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 2 with Tone

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

the class GreetingsResourceImpl method searchWithFacets.

@Finder("searchWithFacets")
public CollectionResult<Greeting, SearchMetadata> searchWithFacets(@PagingContextParam PagingContext ctx, @QueryParam("tone") @Optional Tone tone) {
    List<Greeting> greetings = search(ctx, tone);
    Map<Tone, Integer> toneCounts = new HashMap<>();
    for (Greeting g : greetings) {
        if (!toneCounts.containsKey(g.getTone())) {
            toneCounts.put(g.getTone(), 0);
        }
        toneCounts.put(g.getTone(), toneCounts.get(g.getTone()) + 1);
    }
    SearchMetadata metadata = new SearchMetadata();
    metadata.setFacets(new ToneFacetArray());
    for (Map.Entry<Tone, Integer> entry : toneCounts.entrySet()) {
        ToneFacet f = new ToneFacet();
        f.setTone(entry.getKey());
        f.setCount(entry.getValue());
        metadata.getFacets().add(f);
    }
    return new CollectionResult<>(greetings, null, metadata);
}
Also used : ToneFacet(com.linkedin.restli.examples.greetings.api.ToneFacet) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResult(com.linkedin.restli.server.CollectionResult) Tone(com.linkedin.restli.examples.greetings.api.Tone) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ToneFacetArray(com.linkedin.restli.examples.greetings.api.ToneFacetArray) SearchMetadata(com.linkedin.restli.examples.greetings.api.SearchMetadata) EmptyMap(com.linkedin.restli.examples.greetings.api.EmptyMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StringMap(com.linkedin.data.template.StringMap) Finder(com.linkedin.restli.server.annotations.Finder)

Example 3 with Tone

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

the class GreetingsResourceImpl method searchWithTones.

@Finder("searchWithTones")
public List<Greeting> searchWithTones(@PagingContextParam PagingContext ctx, @QueryParam("tones") @Optional Tone[] tones) {
    Set<Tone> toneSet = new HashSet<>(Arrays.asList(tones));
    List<Greeting> greetings = new ArrayList<>();
    int idx = 0;
    int start = ctx.getStart();
    int stop = start + ctx.getCount();
    for (Greeting g : _db.values()) {
        if (idx++ >= ctx.getStart()) {
            if (tones == null || toneSet.contains(g.getTone())) {
                greetings.add(g);
            }
            if (idx == stop) {
                break;
            }
        }
    }
    return greetings;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Tone(com.linkedin.restli.examples.greetings.api.Tone) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Finder(com.linkedin.restli.server.annotations.Finder)

Example 4 with Tone

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

the class GreetingsResourceImpl method updateTone.

/**
 * a more concrete example of custom action<br>
 * resource level determines the granularity of the action<br>
 * mismatching the resource level in the request throws exception and will respond HTTP
 * 400
 *
 * @param resource
 *          Instance of the resource class. This is not part of the action method and is
 *          needed because this implementation is not an actual resource.
 */
@Action(name = "updateTone", resourceLevel = ResourceLevel.ENTITY)
public // resource class instance, and is not part of the generated REST method.
Greeting updateTone(BaseResource resource, @ActionParam("newTone") @Optional Tone newTone, @ActionParam("delOld") @Optional("false") Boolean delOld) {
    // the way to get entity key in action
    Long key = resource.getContext().getPathKeys().get(_resourceName + "Id");
    Greeting g = _db.get(key);
    if (g == null) {
        // HTTP 404
        return g;
    }
    // delete existing Greeting and assign new key
    if (delOld) {
        _db.remove(key);
        key = _idSeq.incrementAndGet();
        g.setId(key);
    }
    Tone t;
    // omitting it in request results a null value
    if (newTone == null) {
        t = DEFAULT_TONE;
    } else {
        t = newTone;
    }
    g.setTone(t);
    _db.put(key, g);
    return g;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Tone(com.linkedin.restli.examples.greetings.api.Tone) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(com.linkedin.restli.server.annotations.Action)

Example 5 with Tone

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

the class TestGreetingsClient method testSearchWithTones.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testSearchWithTones(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> req = builders.findBy("SearchWithTones").setQueryParam("tones", Arrays.asList(Tone.SINCERE, Tone.INSULTING)).build();
    ResponseFuture<CollectionResponse<Greeting>> future = getClient().sendRequest(req);
    Response<CollectionResponse<Greeting>> response = future.getResponse();
    List<Greeting> greetings = response.getEntity().getElements();
    for (Greeting greeting : greetings) {
        Assert.assertTrue(greeting.hasTone());
        Tone tone = greeting.getTone();
        Assert.assertTrue(Tone.SINCERE.equals(tone) || Tone.INSULTING.equals(tone));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Tone(com.linkedin.restli.examples.greetings.api.Tone) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Test(org.testng.annotations.Test)

Aggregations

Greeting (com.linkedin.restli.examples.greetings.api.Greeting)6 Tone (com.linkedin.restli.examples.greetings.api.Tone)6 Test (org.testng.annotations.Test)3 CollectionResponse (com.linkedin.restli.common.CollectionResponse)2 Finder (com.linkedin.restli.server.annotations.Finder)2 StringMap (com.linkedin.data.template.StringMap)1 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)1 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)1 EmptyMap (com.linkedin.restli.examples.greetings.api.EmptyMap)1 SearchMetadata (com.linkedin.restli.examples.greetings.api.SearchMetadata)1 ToneFacet (com.linkedin.restli.examples.greetings.api.ToneFacet)1 ToneFacetArray (com.linkedin.restli.examples.greetings.api.ToneFacetArray)1 AssociationAltKey (com.linkedin.restli.examples.greetings.client.AssociationAltKey)1 AssociationAltKeyFluentClient (com.linkedin.restli.examples.greetings.client.AssociationAltKeyFluentClient)1 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)1 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)1 CollectionResult (com.linkedin.restli.server.CollectionResult)1 Action (com.linkedin.restli.server.annotations.Action)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1