Remove @author tags
[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 public class PostAccessorTest {
27
28         private final Core core = mock(Core.class);
29         private final PostAccessor accessor = new PostAccessor(core);
30         private final Post post = mock(Post.class);
31
32         private final long now = System.currentTimeMillis();
33
34         @Before
35         public void setupPost() {
36                 when(post.getId()).thenReturn("post-id");
37         }
38
39         @Test
40         @SuppressWarnings("unchecked")
41         public void accessorReturnsTheCorrectReplies() {
42                 List<PostReply> replies = new ArrayList<>();
43                 replies.add(createPostReply(2000));
44                 replies.add(createPostReply(-1000));
45                 replies.add(createPostReply(-2000));
46                 replies.add(createPostReply(-3000));
47                 replies.add(createPostReply(-4000));
48                 when(core.getReplies("post-id")).thenReturn(replies);
49                 Collection<PostReply> repliesForPost = (Collection<PostReply>) accessor.get(null, post, "replies");
50                 assertThat(repliesForPost, contains(
51                                 replies.get(1),
52                                 replies.get(2),
53                                 replies.get(3),
54                                 replies.get(4)
55                 ));
56         }
57
58         private PostReply createPostReply(long timeOffset) {
59                 PostReply postReply = mock(PostReply.class);
60                 when(postReply.getTime()).thenReturn(now + timeOffset);
61                 return postReply;
62         }
63
64         @Test
65         @SuppressWarnings("unchecked")
66         public void accessorReturnsTheLikingSones() {
67                 Set<Sone> sones = mock(Set.class);
68                 when(core.getLikes(post)).thenReturn(sones);
69                 Set<Sone> likingSones = (Set<Sone>) accessor.get(null, post, "likes");
70                 assertThat(likingSones, is(sones));
71         }
72
73         @Test
74         public void accessorReturnsWhetherTheCurrentSoneLikedAPost() {
75                 Sone sone = mock(Sone.class);
76                 when(sone.isLikedPostId("post-id")).thenReturn(true);
77                 TemplateContext templateContext = new TemplateContext();
78                 templateContext.set("currentSone", sone);
79                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) true));
80         }
81
82         @Test
83         public void accessorReturnsFalseIfPostIsNotLiked() {
84                 Sone sone = mock(Sone.class);
85                 TemplateContext templateContext = new TemplateContext();
86                 templateContext.set("currentSone", sone);
87                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
88         }
89
90         @Test
91         public void accessorReturnsFalseIfThereIsNoCurrentSone() {
92                 TemplateContext templateContext = new TemplateContext();
93                 assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
94         }
95
96         @Test
97         public void accessorReturnsThatNotKnownPostIsNew() {
98                 assertThat(accessor.get(null, post, "new"), is((Object) true));
99         }
100
101         @Test
102         public void accessorReturnsThatKnownPostIsNotNew() {
103                 when(post.isKnown()).thenReturn(true);
104                 assertThat(accessor.get(null, post, "new"), is((Object) false));
105         }
106
107         @Test
108         public void accessorReturnsIfPostIsBookmarked() {
109                 when(core.isBookmarked(post)).thenReturn(true);
110                 assertThat(accessor.get(null, post, "bookmarked"), is((Object) true));
111         }
112
113         @Test
114         public void accessorReturnsOtherProperties() {
115                 assertThat(accessor.get(null, post, "hashCode"), is((Object) post.hashCode()));
116         }
117
118 }