Search in sources :

Example 1 with ToneFacet

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

the class TestAlwaysProjectedFieldsOnServer method testMetadataWithProjection.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testMetadataWithProjection(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting.Fields fields = Greeting.fields();
    Request<CollectionResponse<Greeting>> findRequest = builders.findBy("searchWithFacets").setQueryParam("tone", Tone.FRIENDLY).fields(// 'id' and 'tone' are included by server
    fields.message()).metadataFields(SearchMetadata.fields().facets().items().count()).pagingFields(// 'total' is included by server
    CollectionMetadata.fields().count()).build();
    CollectionResponse<Greeting> greetingsResponse = getClient().sendRequest(findRequest).getResponse().getEntity();
    List<Greeting> greetings = greetingsResponse.getElements();
    for (Greeting g : greetings) {
        Assert.assertEquals(g.getTone(), Tone.FRIENDLY);
        Assert.assertNotNull(g.getId());
        Assert.assertNotNull(g.getMessage());
    }
    SearchMetadata metadata = new SearchMetadata(greetingsResponse.getMetadataRaw());
    Assert.assertEquals(1, metadata.getFacets().size());
    for (ToneFacet facet : metadata.getFacets()) {
        Assert.assertNotNull(facet.getCount());
        Assert.assertNotNull(facet.getTone());
    }
    CollectionMetadata pagingData = greetingsResponse.getPaging();
    Assert.assertNotNull(pagingData.getCount());
    Assert.assertNotNull(pagingData.getTotal());
    Assert.assertFalse(pagingData.hasLinks());
    Assert.assertFalse(pagingData.hasStart());
}
Also used : ToneFacet(com.linkedin.restli.examples.greetings.api.ToneFacet) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) CollectionResponse(com.linkedin.restli.common.CollectionResponse) SearchMetadata(com.linkedin.restli.examples.greetings.api.SearchMetadata) Test(org.testng.annotations.Test)

Example 2 with ToneFacet

use of com.linkedin.restli.examples.greetings.api.ToneFacet 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)

Aggregations

Greeting (com.linkedin.restli.examples.greetings.api.Greeting)2 SearchMetadata (com.linkedin.restli.examples.greetings.api.SearchMetadata)2 ToneFacet (com.linkedin.restli.examples.greetings.api.ToneFacet)2 StringMap (com.linkedin.data.template.StringMap)1 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)1 CollectionResponse (com.linkedin.restli.common.CollectionResponse)1 EmptyMap (com.linkedin.restli.examples.greetings.api.EmptyMap)1 Tone (com.linkedin.restli.examples.greetings.api.Tone)1 ToneFacetArray (com.linkedin.restli.examples.greetings.api.ToneFacetArray)1 CollectionResult (com.linkedin.restli.server.CollectionResult)1 Finder (com.linkedin.restli.server.annotations.Finder)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1