Search in sources :

Example 6 with Tweet

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

the class SearchReceivingMessageSourceTests method demoReceiveSearchResults.

@SuppressWarnings("unchecked")
@Test
@Ignore
public void demoReceiveSearchResults() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"), prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"), prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    SearchReceivingMessageSource tSource = new SearchReceivingMessageSource(template, "foo");
    tSource.setQuery(SEARCH_QUERY);
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<Tweet> message = (Message<Tweet>) tSource.receive();
        if (message != null) {
            Tweet tweet = message.getPayload();
            logger.info(tweet.getFromUser() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Message(org.springframework.messaging.Message) Tweet(org.springframework.social.twitter.api.Tweet) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with Tweet

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

the class SearchReceivingMessageSourceTests method testPollForTweetsThreeResults.

/**
 * Verify that a polling operation returns in fact 3 results.
 */
@Test
public void testPollForTweetsThreeResults() {
    final TwitterTemplate twitterTemplate;
    final SearchOperations so = mock(SearchOperations.class);
    final List<Tweet> tweets = new ArrayList<Tweet>();
    tweets.add(mock(Tweet.class));
    tweets.add(mock(Tweet.class));
    tweets.add(mock(Tweet.class));
    final SearchResults results = new SearchResults(tweets, new SearchMetadata(111, 111));
    twitterTemplate = mock(TwitterTemplate.class);
    when(twitterTemplate.searchOperations()).thenReturn(so);
    SearchParameters params = new SearchParameters(SEARCH_QUERY).count(20).sinceId(0);
    when(twitterTemplate.searchOperations().search(params)).thenReturn(results);
    final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(twitterTemplate, "foo");
    messageSource.setQuery(SEARCH_QUERY);
    final List<Tweet> tweetSearchResults = messageSource.pollForTweets(0);
    assertNotNull(tweetSearchResults);
    assertEquals(3, tweetSearchResults.size());
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) SearchOperations(org.springframework.social.twitter.api.SearchOperations) Test(org.junit.Test)

Example 8 with Tweet

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

the class SearchReceivingMessageSourceTests method testPollForTweetsNullResults.

/**
 * This test ensures that when polling for a list of Tweets null is never returned.
 * In case of no polling results, an empty list is returned instead.
 */
@Test
public void testPollForTweetsNullResults() {
    final TwitterTemplate twitterTemplate = mock(TwitterTemplate.class);
    final SearchOperations so = mock(SearchOperations.class);
    when(twitterTemplate.searchOperations()).thenReturn(so);
    when(twitterTemplate.searchOperations().search(SEARCH_QUERY, 20, 0, 0)).thenReturn(null);
    final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(twitterTemplate, "foo");
    messageSource.setQuery(SEARCH_QUERY);
    final String setQuery = TestUtils.getPropertyValue(messageSource, "query", String.class);
    assertEquals(SEARCH_QUERY, setQuery);
    assertEquals("twitter:search-inbound-channel-adapter", messageSource.getComponentType());
    final List<Tweet> tweets = messageSource.pollForTweets(0);
    assertNotNull(tweets);
    assertTrue(tweets.isEmpty());
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Tweet(org.springframework.social.twitter.api.Tweet) SearchOperations(org.springframework.social.twitter.api.SearchOperations) Test(org.junit.Test)

Example 9 with Tweet

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

the class TimelineReceivingMessageSourceTests method demoReceiveTimeline.

@SuppressWarnings("unchecked")
@Test
@Ignore
public void demoReceiveTimeline() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"), prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"), prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    TimelineReceivingMessageSource tSource = new TimelineReceivingMessageSource(template, "foo");
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<Tweet> message = (Message<Tweet>) tSource.receive();
        if (message != null) {
            Tweet tweet = message.getPayload();
            logger.info(tweet.getFromUser() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) Message(org.springframework.messaging.Message) Tweet(org.springframework.social.twitter.api.Tweet) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with Tweet

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

the class TwitterAnnouncer method search.

public void search(Message<?> search) {
    MessageHistory history = MessageHistory.read(search);
    Tweet tweet = (Tweet) search.getPayload();
    logger.info("A search item was received " + tweet.getCreatedAt() + " with text " + tweet.getText());
}
Also used : MessageHistory(org.springframework.integration.history.MessageHistory) Tweet(org.springframework.social.twitter.api.Tweet)

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