From b3355b4a40c702c501594db4fa31e24056e0cc4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 10 Nov 2016 07:03:16 +0100 Subject: [PATCH] Add unit test for post acccessor --- .../sone/template/PostAccessorTest.java | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java 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())); + } + +} -- 2.7.4