/** 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());
  }
 @Override
 public void run() {
   List<Tweet> userTimeline;
   try {
     log.info("Getting latest tweets from public timeline and feeding them into processing grid");
     // Return all the tweets from the Twitter API
     userTimeline = twitterTemplate.timelineOperations().getUserTimeline("BriefingcomSMU");
   } catch (ApiException e) {
     log.log(Level.SEVERE, "Error getting tweets from public timeline from twitter", e);
     return;
   }
   try {
     // according to the API we may get duplicate tweets if invoked with frequency of lower than 60
     // seconds.
     // We will filter tweets which are duplicates
     for (Tweet publicTweet : userTimeline) {
       if (previousTimeLineTweets.contains(publicTweet.getId())) {
         continue;
       }
       logTweet(publicTweet);
       gigaSpace.write(buildTweet(publicTweet));
     }
   } catch (DataAccessException e) {
     log.log(Level.SEVERE, "error feeding tweets", e);
   } finally {
     previousTimeLineTweets.clear();
     for (Tweet publicTweet : userTimeline) {
       previousTimeLineTweets.add(publicTweet.getId());
     }
   }
 }
  /**
   * 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());
  }