1 package net.pterodactylus.sone.template;
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;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.List;
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;
20 import org.junit.Before;
21 import org.junit.Test;
24 * Unit test for {@link PostAccessor}.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class PostAccessorTest {
30 private final Core core = mock(Core.class);
31 private final PostAccessor accessor = new PostAccessor(core);
32 private final Post post = mock(Post.class);
34 private final long now = System.currentTimeMillis();
37 public void setupPost() {
38 when(post.getId()).thenReturn("post-id");
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(
60 private PostReply createPostReply(long timeOffset) {
61 PostReply postReply = mock(PostReply.class);
62 when(postReply.getTime()).thenReturn(now + timeOffset);
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));
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));
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));
93 public void accessorReturnsFalseIfThereIsNoCurrentSone() {
94 TemplateContext templateContext = new TemplateContext();
95 assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
99 public void accessorReturnsThatNotKnownPostIsNew() {
100 assertThat(accessor.get(null, post, "new"), is((Object) true));
104 public void accessorReturnsThatKnownPostIsNotNew() {
105 when(post.isKnown()).thenReturn(true);
106 assertThat(accessor.get(null, post, "new"), is((Object) false));
110 public void accessorReturnsIfPostIsBookmarked() {
111 when(core.isBookmarked(post)).thenReturn(true);
112 assertThat(accessor.get(null, post, "bookmarked"), is((Object) true));
116 public void accessorReturnsOtherProperties() {
117 assertThat(accessor.get(null, post, "hashCode"), is((Object) post.hashCode()));