Utils { static List<Tweet> orderTweets(List<Long> tweetIds, List<Tweet> tweets) { final HashMap<Long, Tweet> idToTweet = new HashMap<>(); final ArrayList<Tweet> ordered = new ArrayList<>(); for (Tweet tweet: tweets) { idToTweet.put(tweet.id, tweet); } for (Long id: tweetIds) { if (idToTweet.containsKey(id)) { ordered.add(idToTweet.get(id)); } } return ordered; } private  Utils();   }
@Test public void testSortTweets() { final List<Long> requestedIds = TestFixtures.TWEET_IDS; final List<Tweet> tweets = new ArrayList<>(TestFixtures.UNORDERED_TWEETS); final List<Tweet> ordered = Utils.orderTweets(requestedIds, tweets); assertEquals(TestFixtures.ORDERED_TWEETS, ordered); }
@Test public void testSortTweets_duplicateRequestedIds() { final List<Long> requestedIds = TestFixtures.DUPLICATE_TWEET_IDS; final List<Tweet> tweets = new ArrayList<>(TestFixtures.UNORDERED_TWEETS); final List<Tweet> ordered = Utils.orderTweets(requestedIds, tweets); assertEquals(TestFixtures.ORDERED_DUPLICATE_TWEETS, ordered); }
@Test public void testSortTweets_duplicateTweets() { final List<Long> requestedIds = TestFixtures.TWEET_IDS; final List<Tweet> tweets = new ArrayList<>(TestFixtures.UNORDERED_DUPLICATE_TWEETS); final List<Tweet> ordered = Utils.orderTweets(requestedIds, tweets); assertEquals(TestFixtures.ORDERED_TWEETS, ordered); }
@Test public void testSortTweets_missingTweets() { final List<Long> requestedIds = TestFixtures.TWEET_IDS; final List<Tweet> tweets = new ArrayList<>(TestFixtures.UNORDERED_MISSING_TWEETS); final List<Tweet> ordered = Utils.orderTweets(requestedIds, tweets); assertEquals(TestFixtures.ORDERED_MISSING_TWEETS, ordered); }
@Test public void testSortTweets_extraTweetsFirst() { final List<Long> requestedIds = TestFixtures.TWEET_IDS; final List<Tweet> tweets = new ArrayList<>(TestFixtures.UNORDERED_TWEETS); tweets.add(TestFixtures.TEST_TWEET); final List<Tweet> ordered = Utils.orderTweets(requestedIds, tweets); assertEquals(TestFixtures.ORDERED_TWEETS, ordered); }