X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FPostAccessorTest.java;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FPostAccessorTest.java;h=a6bc3817b951a7a9eb2af4f997620b0ecac371b1;hb=b3355b4a40c702c501594db4fa31e24056e0cc4a;hp=0000000000000000000000000000000000000000;hpb=c541fb3cfe0070db3f8ff5f422d203be5e0e2e27;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java
new file mode 100644
index 0000000..a6bc381
--- /dev/null
+++ b/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java
@@ -0,0 +1,120 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.TemplateContext;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link PostAccessor}.
+ *
+ * @author David âBombeâ Roden
+ */
+public class PostAccessorTest {
+
+ private final Core core = mock(Core.class);
+ private final PostAccessor accessor = new PostAccessor(core);
+ private final Post post = mock(Post.class);
+
+ private final long now = System.currentTimeMillis();
+
+ @Before
+ public void setupPost() {
+ when(post.getId()).thenReturn("post-id");
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void accessorReturnsTheCorrectReplies() {
+ List replies = new ArrayList<>();
+ replies.add(createPostReply(2000));
+ replies.add(createPostReply(-1000));
+ replies.add(createPostReply(-2000));
+ replies.add(createPostReply(-3000));
+ replies.add(createPostReply(-4000));
+ when(core.getReplies("post-id")).thenReturn(replies);
+ Collection repliesForPost = (Collection) accessor.get(null, post, "replies");
+ assertThat(repliesForPost, contains(
+ replies.get(1),
+ replies.get(2),
+ replies.get(3),
+ replies.get(4)
+ ));
+ }
+
+ private PostReply createPostReply(long timeOffset) {
+ PostReply postReply = mock(PostReply.class);
+ when(postReply.getTime()).thenReturn(now + timeOffset);
+ return postReply;
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void accessorReturnsTheLikingSones() {
+ Set sones = mock(Set.class);
+ when(core.getLikes(post)).thenReturn(sones);
+ Set likingSones = (Set) accessor.get(null, post, "likes");
+ assertThat(likingSones, is(sones));
+ }
+
+ @Test
+ public void accessorReturnsWhetherTheCurrentSoneLikedAPost() {
+ Sone sone = mock(Sone.class);
+ when(sone.isLikedPostId("post-id")).thenReturn(true);
+ TemplateContext templateContext = new TemplateContext();
+ templateContext.set("currentSone", sone);
+ assertThat(accessor.get(templateContext, post, "liked"), is((Object) true));
+ }
+
+ @Test
+ public void accessorReturnsFalseIfPostIsNotLiked() {
+ Sone sone = mock(Sone.class);
+ TemplateContext templateContext = new TemplateContext();
+ templateContext.set("currentSone", sone);
+ assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
+ }
+
+ @Test
+ public void accessorReturnsFalseIfThereIsNoCurrentSone() {
+ TemplateContext templateContext = new TemplateContext();
+ assertThat(accessor.get(templateContext, post, "liked"), is((Object) false));
+ }
+
+ @Test
+ public void accessorReturnsThatNotKnownPostIsNew() {
+ assertThat(accessor.get(null, post, "new"), is((Object) true));
+ }
+
+ @Test
+ public void accessorReturnsThatKnownPostIsNotNew() {
+ when(post.isKnown()).thenReturn(true);
+ assertThat(accessor.get(null, post, "new"), is((Object) false));
+ }
+
+ @Test
+ public void accessorReturnsIfPostIsBookmarked() {
+ when(core.isBookmarked(post)).thenReturn(true);
+ assertThat(accessor.get(null, post, "bookmarked"), is((Object) true));
+ }
+
+ @Test
+ public void accessorReturnsOtherProperties() {
+ assertThat(accessor.get(null, post, "hashCode"), is((Object) post.hashCode()));
+ }
+
+}