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