PostFeedToList implements Func1<PostListFeed, List<Post>> { @Override public List<Post> call(PostListFeed feed) { if(feed == null || feed.posts == null) { return new ArrayList<>(); } List<Post> posts = feed.posts; Map<String, User> users = feed.users; for(Post post: posts) { post.setUser(users.get(post.getUserId())); } return posts; }  @Override List<Post> call(PostListFeed feed);  }
@Test public void nullFeedReturnsEmptyFeed() { List<Post> posts = new PostFeedToList().call(null); assertThat(posts, notNullValue()); }
@Test public void setsUsersToPosts() { String userId = "userId"; Post post = mock(Post.class); when(post.getUserId()).thenReturn(userId); User user = mock(User.class); List<Post> posts = Arrays.asList(post); Map<String, User> userMap = new HashMap<>(); userMap.put(userId, user); PostListFeed feed = new PostListFeed(); feed.posts = posts; feed.users = userMap; PostFeedToList converter = new PostFeedToList(); converter.call(feed); verify(post).setUser(user); }
@Test public void returnsPostsFromFeed() { String userId = "userId"; Post post = mock(Post.class); when(post.getUserId()).thenReturn(userId); User user = mock(User.class); List<Post> posts = Arrays.asList(post); Map<String, User> userMap = new HashMap<>(); userMap.put(userId, user); PostListFeed feed = new PostListFeed(); feed.posts = posts; feed.users = userMap; PostFeedToList converter = new PostFeedToList(); List<Post> result = converter.call(feed); assertThat(result, equalTo(posts)); }