EventListFeed extends FlattenableFeed<List<Event>> { @Override public List<Event> flatten() { if(events == null) { return new ArrayList<>(); } for(Event event : events) { event.setUser(users.get(event.getUserId())); event.setPost(posts.get(event.getPostId())); } return events; }  @Override List<Event> flatten();  }
@Test public void nullFeedReturnsEmptyList() { List<Event> events = new EventListFeed().flatten(); assertThat(events, notNullValue()); }
@Test public void returnsEventsFromFeed() { Event event = mock(Event.class); List<Event> events = Arrays.asList(event); EventListFeed feed = new EventListFeed(); feed.events = events; feed.users = new HashMap<>(); feed.posts = new HashMap<>(); List<Event> result = feed.flatten(); assertThat(result, equalTo(events)); }
@Test public void populatesUsersToEvents() { String id = "userId"; Event event = mock(Event.class); List<Event> events = Arrays.asList(event); when(event.getUserId()).thenReturn(id); Map<String, User> users = new HashMap<>(); User user = mock(User.class); users.put(id, user); EventListFeed feed = new EventListFeed(); feed.events = events; feed.users = users; feed.posts = new HashMap<>(); feed.flatten(); verify(event).setUser(user); }
@Test public void populatesPostsToEvents() { String postId = "postId"; Post post = mock(Post.class); Event event = mock(Event.class); when(event.getPostId()).thenReturn(postId); EventListFeed feed = new EventListFeed(); feed.events = Arrays.asList(event); Map<String, Post> postMap = new HashMap<String, Post>(); postMap.put(postId, post); feed.posts = postMap; feed.users = new HashMap<>(); feed.flatten(); verify(event).setPost(post); }