🎨 Replace post accessor test with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Jan 2020 19:31:55 +0000 (20:31 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Jan 2020 19:32:45 +0000 (20:32 +0100)
src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java
deleted file mode 100644 (file)
index e60dce9..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-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}.
- */
-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<PostReply> 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<PostReply> repliesForPost = (Collection<PostReply>) 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<Sone> sones = mock(Set.class);
-               when(core.getLikes(post)).thenReturn(sones);
-               Set<Sone> likingSones = (Set<Sone>) 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()));
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt
new file mode 100644 (file)
index 0000000..000e50c
--- /dev/null
@@ -0,0 +1,102 @@
+package net.pterodactylus.sone.template
+
+import net.pterodactylus.sone.core.*
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.test.*
+import net.pterodactylus.util.template.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+
+/**
+ * Unit test for [PostAccessor].
+ */
+class PostAccessorTest {
+
+       private val core = mock<Core>()
+       private val accessor = PostAccessor(core)
+       private val post = mock<Post>()
+       private val now = System.currentTimeMillis()
+
+       @Before
+       fun setupPost() {
+               whenever(post.id).thenReturn("post-id")
+       }
+
+       @Test
+       fun `accessor returns the correct replies`() {
+               val replies = listOf(
+                               createPostReply(2000),
+                               createPostReply(-1000),
+                               createPostReply(-2000),
+                               createPostReply(-3000),
+                               createPostReply(-4000)
+               )
+               whenever(core.getReplies("post-id")).thenReturn(replies)
+               val repliesForPost = accessor[null, post, "replies"] as Collection<PostReply>
+               assertThat(repliesForPost, contains(
+                               replies[1],
+                               replies[2],
+                               replies[3],
+                               replies[4]
+               ))
+       }
+
+       private fun createPostReply(timeOffset: Long) = mock<PostReply>().apply {
+               whenever(time).thenReturn(now + timeOffset)
+       }
+
+       @Test
+       fun `accessor returns the liking sones`() {
+               val sones = setOf<Sone>()
+               whenever(core.getLikes(post)).thenReturn(sones)
+               val likingSones = accessor[null, post, "likes"] as Set<Sone>
+               assertThat(likingSones, equalTo(sones))
+       }
+
+       @Test
+       fun `accessor returns whether the current sone liked a post`() {
+               val sone = mock<Sone>()
+               whenever(sone.isLikedPostId("post-id")).thenReturn(true)
+               val templateContext = TemplateContext()
+               templateContext["currentSone"] = sone
+               assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(true))
+       }
+
+       @Test
+       fun `accessor returns false if post is not liked`() {
+               val sone = mock<Sone>()
+               val templateContext = TemplateContext()
+               templateContext["currentSone"] = sone
+               assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
+       }
+
+       @Test
+       fun `accessor returns false if there is no current sone`() {
+               val templateContext = TemplateContext()
+               assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
+       }
+
+       @Test
+       fun `accessor returns that not known post is new`() {
+               assertThat(accessor[null, post, "new"], equalTo<Any>(true))
+       }
+
+       @Test
+       fun `accessor returns that known post is not new`() {
+               whenever(post.isKnown).thenReturn(true)
+               assertThat(accessor[null, post, "new"], equalTo<Any>(false))
+       }
+
+       @Test
+       fun `accessor returns if post is bookmarked`() {
+               whenever(core.isBookmarked(post)).thenReturn(true)
+               assertThat(accessor[null, post, "bookmarked"], equalTo<Any>(true))
+       }
+
+       @Test
+       fun `accessor returns other properties`() {
+               assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))
+       }
+
+}