Search in sources :

Example 1 with MessageCriteria

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

the class TestParseqBasedFluentClientApi method testAssociationBatchFinderUsingAssocKey.

@Test
public void testAssociationBatchFinderUsingAssocKey() throws Exception {
    AssociationsFluentClient associations = new AssociationsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    MessageCriteriaArray criteriaArray = new MessageCriteriaArray();
    criteriaArray.add(new MessageCriteria().setTone(Tone.FRIENDLY));
    criteriaArray.add(new MessageCriteria().setTone(Tone.INSULTING));
    BatchCollectionResponse<Message> messages = associations.findBySearchMessages(AssociationResourceHelpers.URL_COMPOUND_KEY.getPartAsString("src"), criteriaArray).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(messages.getResults().size(), 2);
    BatchFinderCriteriaResult<Message> friendly = messages.getResults().get(0);
    Assert.assertFalse(friendly.isError());
    for (Message message : friendly.getElements()) {
        Assert.assertEquals(message.getTone(), Tone.FRIENDLY);
    }
    BatchFinderCriteriaResult<Message> insulting = messages.getResults().get(1);
    Assert.assertTrue(insulting.isError());
    Assert.assertEquals((int) insulting.getError().getStatus(), 404);
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) MessageCriteria(com.linkedin.restli.examples.greetings.api.MessageCriteria) MessageCriteriaArray(com.linkedin.restli.examples.greetings.api.MessageCriteriaArray) AssociationsAssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsAssociationsFluentClient) AssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsFluentClient) Test(org.testng.annotations.Test)

Example 2 with MessageCriteria

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

the class TestAssociationsResource method testBatchFinder.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testBatchFinder(RootBuilderWrapper<CompoundKey, Message> builders) throws RemoteInvocationException {
    MessageCriteria m1 = new MessageCriteria().setMessage("hello").setTone(Tone.FRIENDLY);
    MessageCriteria m2 = new MessageCriteria().setMessage("world").setTone(Tone.SINCERE);
    Request<BatchCollectionResponse<Message>> request = builders.batchFindBy("searchMessages").assocKey("src", "KEY1").setQueryParam("criteria", Arrays.asList(m1, m2)).build();
    ResponseFuture<BatchCollectionResponse<Message>> future = getClient().sendRequest(request);
    BatchCollectionResponse<Message> response = future.getResponse().getEntity();
    List<BatchFinderCriteriaResult<Message>> batchResult = response.getResults();
    // on success
    List<Message> messages = batchResult.get(0).getElements();
    Assert.assertTrue(messages.get(0).hasTone());
    Assert.assertTrue(messages.get(0).getTone().equals(Tone.FRIENDLY));
    // on error
    Assert.assertTrue(batchResult.get(1).isError());
    ErrorResponse error = batchResult.get(1).getError();
    Assert.assertEquals(error.getMessage(), "Failed to find message!");
}
Also used : BatchFinderCriteriaResult(com.linkedin.restli.common.BatchFinderCriteriaResult) BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) Message(com.linkedin.restli.examples.greetings.api.Message) MessageCriteria(com.linkedin.restli.examples.greetings.api.MessageCriteria) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 3 with MessageCriteria

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

the class AssociationsResource method searchMessages.

@BatchFinder(value = "searchMessages", batchParam = "criteria")
public BatchFinderResult<MessageCriteria, Message, Empty> searchMessages(@AssocKeyParam("src") String src, @PagingContextParam PagingContext context, @QueryParam("criteria") MessageCriteria[] criteria) {
    BatchFinderResult<MessageCriteria, Message, Empty> batchFinderResult = new BatchFinderResult<>();
    for (MessageCriteria currentCriteria : criteria) {
        if (currentCriteria.getTone() == Tone.FRIENDLY) {
            // on success
            CollectionResult<Message, Empty> cr = new CollectionResult<>(Arrays.asList(m1, m2), 2);
            batchFinderResult.putResult(currentCriteria, cr);
        } else {
            // on error: to construct error response for test
            batchFinderResult.putError(currentCriteria, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "Failed to find message!"));
        }
    }
    return batchFinderResult;
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) Empty(com.linkedin.restli.examples.greetings.api.Empty) Message(com.linkedin.restli.examples.greetings.api.Message) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MessageCriteria(com.linkedin.restli.examples.greetings.api.MessageCriteria) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder)

Aggregations

Message (com.linkedin.restli.examples.greetings.api.Message)3 MessageCriteria (com.linkedin.restli.examples.greetings.api.MessageCriteria)3 Test (org.testng.annotations.Test)2 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)1 BatchFinderCriteriaResult (com.linkedin.restli.common.BatchFinderCriteriaResult)1 ErrorResponse (com.linkedin.restli.common.ErrorResponse)1 Empty (com.linkedin.restli.examples.greetings.api.Empty)1 MessageCriteriaArray (com.linkedin.restli.examples.greetings.api.MessageCriteriaArray)1 AssociationsAssociationsFluentClient (com.linkedin.restli.examples.greetings.client.AssociationsAssociationsFluentClient)1 AssociationsFluentClient (com.linkedin.restli.examples.greetings.client.AssociationsFluentClient)1 BatchFinderResult (com.linkedin.restli.server.BatchFinderResult)1 CollectionResult (com.linkedin.restli.server.CollectionResult)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 BatchFinder (com.linkedin.restli.server.annotations.BatchFinder)1