CollectionTimeline extends BaseTimeline implements Timeline<Tweet> { static List<Tweet> getOrderedTweets(TwitterCollection collection) { if (collection == null || collection.contents == null || collection.contents.tweetMap == null || collection.contents.userMap == null || collection.contents.tweetMap.isEmpty() || collection.contents.userMap.isEmpty() || collection.metadata == null || collection.metadata.timelineItems == null || collection.metadata.position == null) { return Collections.emptyList(); } final List<Tweet> tweets = new ArrayList<>(); for (TwitterCollection.TimelineItem item: collection.metadata.timelineItems) { final Tweet trimmedTweet = collection.contents.tweetMap.get(item.tweetItem.id); final Tweet tweet = mapTweetToUsers(trimmedTweet, collection.contents.userMap); tweets.add(tweet); } return tweets; } CollectionTimeline(TwitterCore twitterCore, Long collectionId, Integer maxItemsPerRequest); @Override void next(Long minPosition, Callback<TimelineResult<Tweet>> cb); @Override void previous(Long maxPosition, Callback<TimelineResult<Tweet>> cb);  }
@Test public void testGetOrderedTweets() { final TwitterCollection.Content contents = new TwitterCollection.Content(testTweetMap, testUserMap); final TwitterCollection.Metadata metadata = new TwitterCollection.Metadata("", TEST_POSITION, testItems); final List<Tweet> tweets = CollectionTimeline.getOrderedTweets( new TwitterCollection(contents, metadata)); assertEquals(3, tweets.size()); assertEquals(TEST_TWEET_1, tweets.get(0)); assertEquals(TEST_TWEET_2, tweets.get(1)); assertEquals(TEST_TWEET_QUOTE, tweets.get(2)); }
@Test public void testGetOrderedTweets_respectsTimelineItemsOrder() { final TwitterCollection.Content contents = new TwitterCollection.Content(testTweetMap, testUserMap); final TwitterCollection.Metadata metadata = new TwitterCollection.Metadata("", TEST_POSITION, testItemsRev); final List<Tweet> tweets = CollectionTimeline.getOrderedTweets( new TwitterCollection(contents, metadata)); assertEquals(3, tweets.size()); assertEquals(TEST_TWEET_QUOTE, tweets.get(0)); assertEquals(TEST_TWEET_2, tweets.get(1)); assertEquals(TEST_TWEET_1, tweets.get(2)); }
@Test public void testGetOrderedTweets_handlesNull() { TwitterCollection collection = new TwitterCollection( new TwitterCollection.Content(null, testUserMap), new TwitterCollection.Metadata("", TEST_POSITION, testItems)); List<Tweet> tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(new TwitterCollection.Content(testTweetMap, null), new TwitterCollection.Metadata("", TEST_POSITION, testItems)); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(new TwitterCollection.Content(testTweetMap, testUserMap), new TwitterCollection.Metadata("", null, testItems)); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(new TwitterCollection.Content(testTweetMap, testUserMap), new TwitterCollection.Metadata("", TEST_POSITION, null)); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(new TwitterCollection.Content(testTweetMap, testUserMap), null); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(null, new TwitterCollection.Metadata("", TEST_POSITION, testItems)); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); collection = new TwitterCollection(null, null); tweets = CollectionTimeline.getOrderedTweets(collection); assertTrue(tweets.isEmpty()); tweets = CollectionTimeline.getOrderedTweets(null); assertTrue(tweets.isEmpty()); }