Search in sources :

Example 11 with Tweet

use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.

the class TwitterSearchOutboundGatewayTests method testStringQuery.

@Test
public void testStringQuery() {
    Tweet tweet = mock(Tweet.class);
    SearchMetadata searchMetadata = mock(SearchMetadata.class);
    final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
    doAnswer(invocation -> {
        SearchParameters searchParameters = invocation.getArgument(0);
        assertEquals("foo", searchParameters.getQuery());
        assertEquals(Integer.valueOf(20), searchParameters.getCount());
        return searchResults;
    }).when(this.searchOps).search(any(SearchParameters.class));
    this.gateway.handleMessage(new GenericMessage<String>("foo"));
    Message<?> reply = this.outputChannel.receive(0);
    assertNotNull(reply);
    @SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
    assertEquals(1, tweets.size());
    assertSame(tweet, tweets.get(0));
    assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Example 12 with Tweet

use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.

the class StatusUpdatingMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    Object value;
    if (this.tweetDataExpression != null) {
        value = this.tweetDataExpression.getValue(this.evaluationContext, message);
    } else {
        value = message.getPayload();
    }
    Assert.notNull(value, "The tweetData cannot evaluate to 'null'.");
    TweetData tweetData = null;
    if (value instanceof TweetData) {
        tweetData = (TweetData) value;
    } else if (value instanceof Tweet) {
        tweetData = new TweetData(((Tweet) value).getText());
    } else if (value instanceof String) {
        tweetData = new TweetData((String) value);
    } else {
        throw new MessageHandlingException(message, "Unsupported tweetData: " + value);
    }
    this.twitter.timelineOperations().updateStatus(tweetData);
}
Also used : Tweet(org.springframework.social.twitter.api.Tweet) TweetData(org.springframework.social.twitter.api.TweetData) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 13 with Tweet

use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.

the class TwitterSearchOutboundGatewayTests method testSearchParamsQuery.

@Test
public void testSearchParamsQuery() {
    Tweet tweet = mock(Tweet.class);
    SearchMetadata searchMetadata = mock(SearchMetadata.class);
    final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
    final SearchParameters parameters = new SearchParameters("bar");
    doAnswer(invocation -> {
        SearchParameters searchParameters = invocation.getArgument(0);
        assertSame(parameters, searchParameters);
        return searchResults;
    }).when(this.searchOps).search(any(SearchParameters.class));
    this.gateway.handleMessage(new GenericMessage<SearchParameters>(parameters));
    Message<?> reply = this.outputChannel.receive(0);
    assertNotNull(reply);
    @SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
    assertEquals(1, tweets.size());
    assertSame(tweet, tweets.get(0));
    assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Example 14 with Tweet

use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.

the class TwitterSearchOutboundGatewayTests method testEmptyResult.

@Test
public void testEmptyResult() {
    SearchMetadata searchMetadata = mock(SearchMetadata.class);
    List<Tweet> empty = new ArrayList<Tweet>(0);
    final SearchResults searchResults = new SearchResults(empty, searchMetadata);
    doAnswer(invocation -> {
        SearchParameters searchParameters = invocation.getArgument(0);
        assertEquals("foo", searchParameters.getQuery());
        assertEquals(Integer.valueOf(20), searchParameters.getCount());
        return searchResults;
    }).when(this.searchOps).search(any(SearchParameters.class));
    this.gateway.handleMessage(new GenericMessage<String>("foo"));
    Message<?> reply = this.outputChannel.receive(0);
    assertNotNull(reply);
    @SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
    assertEquals(0, tweets.size());
    assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Example 15 with Tweet

use of org.springframework.social.twitter.api.Tweet in project spring-integration by spring-projects.

the class TwitterSearchOutboundGatewayTests method testStringQueryCustomLimit.

@Test
public void testStringQueryCustomLimit() {
    this.gateway.setSearchArgsExpression(new SpelExpressionParser().parseExpression("{payload, 30}"));
    Tweet tweet = mock(Tweet.class);
    SearchMetadata searchMetadata = mock(SearchMetadata.class);
    final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
    doAnswer(invocation -> {
        SearchParameters searchParameters = invocation.getArgument(0);
        assertEquals("foo", searchParameters.getQuery());
        assertEquals(Integer.valueOf(30), searchParameters.getCount());
        return searchResults;
    }).when(this.searchOps).search(any(SearchParameters.class));
    this.gateway.handleMessage(new GenericMessage<String>("foo"));
    Message<?> reply = this.outputChannel.receive(0);
    assertNotNull(reply);
    @SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
    assertEquals(1, tweets.size());
    assertSame(tweet, tweets.get(0));
    assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Aggregations

Tweet (org.springframework.social.twitter.api.Tweet)16 Test (org.junit.Test)11 SearchParameters (org.springframework.social.twitter.api.SearchParameters)8 SearchResults (org.springframework.social.twitter.api.SearchResults)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SearchMetadata (org.springframework.social.twitter.api.SearchMetadata)7 TwitterTemplate (org.springframework.social.twitter.api.impl.TwitterTemplate)6 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)3 Properties (java.util.Properties)2 Ignore (org.junit.Ignore)2 PropertiesFactoryBean (org.springframework.beans.factory.config.PropertiesFactoryBean)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Message (org.springframework.messaging.Message)2 Secured (org.springframework.security.access.annotation.Secured)2 SearchOperations (org.springframework.social.twitter.api.SearchOperations)2 StatusDetails (org.springframework.social.twitter.api.StatusDetails)2 Twitter (org.springframework.social.twitter.api.Twitter)2 MessageHistory (org.springframework.integration.history.MessageHistory)1 MetadataStore (org.springframework.integration.metadata.MetadataStore)1