Replace bookmarks page test with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2017 19:47:21 +0000 (20:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2017 19:47:21 +0000 (20:47 +0100)
src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/web/BookmarksPageTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java b/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java
deleted file mode 100644 (file)
index 0c85a88..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-package net.pterodactylus.sone.web;
-
-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.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
-import net.pterodactylus.util.collection.Pagination;
-
-import org.junit.Test;
-
-/**
- * Unit test for {@link BookmarksPage}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class BookmarksPageTest extends WebPageTest {
-
-       private final BookmarksPage page = new BookmarksPage(template, webInterface);
-
-       @Test
-       public void pageReturnsCorrectPath() {
-               assertThat(page.getPath(), is("bookmarks.html"));
-       }
-
-       @Test
-       @SuppressWarnings("unchecked")
-       public void pageSetsCorrectPostsInTemplateContext() throws RedirectException {
-               Post post1 = createPost(true, 3000L);
-               Post post2 = createPost(true, 1000L);
-               Post post3 = createPost(true, 2000L);
-               Set<Post> bookmarkedPosts = createBookmarkedPosts(post1, post2, post3);
-               when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts);
-               core.getPreferences().setPostsPerPage(5);
-               page.processTemplate(freenetRequest, templateContext);
-               assertThat((Collection<Post>) templateContext.get("posts"), contains(post1, post3, post2));
-               assertThat(((Pagination<Post>) templateContext.get("pagination")).getItems(), contains(post1, post3, post2));
-               assertThat(((Boolean) templateContext.get("postsNotLoaded")), is(false));
-       }
-
-       private Set<Post> createBookmarkedPosts(Post post1, Post post2, Post post3) {
-               Set<Post> bookmarkedPosts = new HashSet<>();
-               bookmarkedPosts.add(post1);
-               bookmarkedPosts.add(post2);
-               bookmarkedPosts.add(post3);
-               return bookmarkedPosts;
-       }
-
-       @Test
-       @SuppressWarnings("unchecked")
-       public void notLoadedPostsAreNotIncludedButAFlagIsSet() throws RedirectException {
-               Post post1 = createPost(true, 1000L);
-               Post post2 = createPost(true, 3000L);
-               Post post3 = createPost(false, 2000L);
-               Set<Post> bookmarkedPosts = createBookmarkedPosts(post1, post2, post3);
-               when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts);
-               core.getPreferences().setPostsPerPage(5);
-               page.processTemplate(freenetRequest, templateContext);
-               assertThat((Collection<Post>) templateContext.get("posts"), contains(post2, post1));
-               assertThat(((Pagination<Post>) templateContext.get("pagination")).getItems(), contains(post2, post1));
-               assertThat(((Boolean) templateContext.get("postsNotLoaded")), is(true));
-       }
-
-       private Post createPost(boolean postLoaded, long time) {
-               Post post = mock(Post.class);
-               when(post.isLoaded()).thenReturn(postLoaded);
-               when(post.getTime()).thenReturn(time);
-               return post;
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/BookmarksPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/BookmarksPageTest.kt
new file mode 100644 (file)
index 0000000..cfde866
--- /dev/null
@@ -0,0 +1,58 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.util.collection.Pagination
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.equalTo
+import org.junit.Before
+import org.junit.Test
+
+/**
+ * Unit test for [BookmarksPage].
+ */
+class BookmarksPageTest: WebPageTest() {
+
+       private val page = BookmarksPage(template, webInterface)
+       private val post1 = createLoadedPost(1000)
+       private val post2 = createLoadedPost(3000)
+       private val post3 = createLoadedPost(2000)
+
+       private fun createLoadedPost(time: Long) = mock<Post>().apply {
+               whenever(isLoaded).thenReturn(true)
+               whenever(this.time).thenReturn(time)
+       }
+
+       @Before
+       fun setupBookmarkedPostsAndPagination() {
+               whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
+               core.preferences.postsPerPage = 5
+       }
+
+       @Test
+       fun `page returns correct path`() {
+               assertThat(page.path, equalTo("bookmarks.html"))
+       }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `page sets correct posts in template context`() {
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
+               assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
+               assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
+       }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `page does not put unloaded posts in template context but sets a flag`() {
+               whenever(post3.isLoaded).thenReturn(false)
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
+               assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
+               assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
+       }
+
+}