e11096902d817469c987ed09d5114c03bdc6ed3d
[Sone.git] / src / test / java / net / pterodactylus / sone / web / NewPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static java.util.Arrays.asList;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.containsInAnyOrder;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.util.List;
10
11 import net.pterodactylus.sone.data.Post;
12 import net.pterodactylus.sone.data.PostReply;
13
14 import com.google.common.base.Optional;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 /**
19  * Unit test for {@link NewPage}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class NewPageTest extends WebPageTest {
24
25         private final NewPage newPage = new NewPage(template, webInterface);
26
27         @Before
28         public void setupNumberOfPostsPerPage() {
29                 when(webInterface.getCore().getPreferences().getPostsPerPage()).thenReturn(5);
30         }
31
32         @Test
33         public void postsAreNotDuplicatedWhenTheyComeFromBothNewPostsAndNewRepliesNotifications() throws Exception {
34                 // given
35                 Post extraPost = mock(Post.class);
36                 List<Post> posts = asList(mock(Post.class), mock(Post.class));
37                 List<PostReply> postReplies = asList(mock(PostReply.class), mock(PostReply.class));
38                 when(postReplies.get(0).getPost()).thenReturn(Optional.of(posts.get(0)));
39                 when(postReplies.get(1).getPost()).thenReturn(Optional.of(extraPost));
40                 when(webInterface.getNewPosts(currentSone)).thenReturn(posts);
41                 when(webInterface.getNewReplies(currentSone)).thenReturn(postReplies);
42
43                 // when
44                 newPage.processTemplate(freenetRequest, templateContext);
45
46                 // then
47                 List<Post> renderedPosts = templateContext.get("posts", List.class);
48                 assertThat(renderedPosts, containsInAnyOrder(posts.get(0), posts.get(1), extraPost));
49         }
50
51 }