Add unit test for login page
[Sone.git] / src / test / java / net / pterodactylus / sone / template / PostAccessorTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.List;
12 import java.util.Set;
13
14 import net.pterodactylus.sone.core.Core;
15 import net.pterodactylus.sone.data.Post;
16 import net.pterodactylus.sone.data.PostReply;
17 import net.pterodactylus.sone.data.Sone;
18 import net.pterodactylus.util.template.TemplateContext;
19
20 import org.junit.Before;
21 import org.junit.Test;
22
23 /**
24  * Unit test for {@link PostAccessor}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class PostAccessorTest {
29
30         private final Core core = mock(Core.class);
31         private final PostAccessor accessor = new PostAccessor(core);
32         private final Post post = mock(Post.class);
33
34         private final long now = System.currentTimeMillis();
35
36         @Before
37         public void setupPost() {
38                 when(post.getId()).thenReturn("post-id");
39         }
40
41         @Test
42         @SuppressWarnings("unchecked")
43         public void accessorReturnsTheCorrectReplies() {
44                 List<PostReply> replies = new ArrayList<>();
45                 replies.add(createPostReply(2000));
46                 replies.add(createPostReply(-1000));
47                 replies.add(createPostReply(-2000));
48                 replies.add(createPostReply(-3000));
49                 replies.add(createPostReply(-4000));
50                 when(core.getReplies("post-id")).thenReturn(replies);
51                 Collection<PostReply> repliesForPost = (Collection<PostReply>) accessor.get(null, post, "replies");
52                 assertThat(repliesForPost, contains(
53                                 replies.get(1),
54                                 replies.get(2),
55                                 replies.get(3),
56                                 replies.get(4)
57                 ));
58         }
59
60         private PostReply createPostReply(long timeOffset) {
61                 PostReply postReply = mock(PostReply.class);
62                 when(postReply.getTime()).thenReturn(now + timeOffset);
63                 return postReply;
64         }
65
66         @Test
67         @SuppressWarnings("unchecked")
68         public void accessorReturnsTheLikingSones() {
69                 Set<Sone> sones = mock(Set.class);
70                 when(core.getLikes(post)).thenReturn(sones);
71                 Set<Sone> likingSones = (Set<Sone>) accessor.get(null, post, "likes");
72                 assertThat(likingSones, is(sones));
73         }
74
75         @Test
76         public void accessorReturnsWhetherTheCurrentSoneLikedAPost() {
77                 Sone sone = mock(Sone.class);
78                 when(sone.isLikedPostId("post-id")).thenReturn(true);
79                 TemplateContext templateContext = new TemplateContext();
80                 templateContext.set("currentSone", sone);
81                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) true));
82         }
83
84         @Test
85         public void accessorReturnsFalseIfPostIsNotLiked() {
86                 Sone sone = mock(Sone.class);
87                 TemplateContext templateContext = new TemplateContext();
88                 templateContext.set("currentSone", sone);
89                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
90         }
91
92         @Test
93         public void accessorReturnsFalseIfThereIsNoCurrentSone() {
94                 TemplateContext templateContext = new TemplateContext();
95                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
96         }
97
98         @Test
99         public void accessorReturnsThatNotKnownPostIsNew() {
100                 assertThat(accessor.get(null, post, "new"), is((Object) true));
101         }
102
103         @Test
104         public void accessorReturnsThatKnownPostIsNotNew() {
105                 when(post.isKnown()).thenReturn(true);
106                 assertThat(accessor.get(null, post, "new"), is((Object) false));
107         }
108
109         @Test
110         public void accessorReturnsIfPostIsBookmarked() {
111                 when(core.isBookmarked(post)).thenReturn(true);
112                 assertThat(accessor.get(null, post, "bookmarked"), is((Object) true));
113         }
114
115         @Test
116         public void accessorReturnsOtherProperties() {
117                 assertThat(accessor.get(null, post, "hashCode"), is((Object) post.hashCode()));
118         }
119
120 }