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