From: David ‘Bombe’ Roden Date: Wed, 8 Feb 2017 19:47:21 +0000 (+0100) Subject: Replace bookmarks page test with Kotlin version X-Git-Tag: 0.9.7^2~302 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4ae1e432c3ce10164ad02dfad608d69b4d39228c;p=Sone.git Replace bookmarks page test with Kotlin version --- 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 index 0c85a88..0000000 --- a/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java +++ /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 David ‘Bombe’ Roden - */ -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 bookmarkedPosts = createBookmarkedPosts(post1, post2, post3); - when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts); - core.getPreferences().setPostsPerPage(5); - page.processTemplate(freenetRequest, templateContext); - assertThat((Collection) templateContext.get("posts"), contains(post1, post3, post2)); - assertThat(((Pagination) templateContext.get("pagination")).getItems(), contains(post1, post3, post2)); - assertThat(((Boolean) templateContext.get("postsNotLoaded")), is(false)); - } - - private Set createBookmarkedPosts(Post post1, Post post2, Post post3) { - Set 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 bookmarkedPosts = createBookmarkedPosts(post1, post2, post3); - when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts); - core.getPreferences().setPostsPerPage(5); - page.processTemplate(freenetRequest, templateContext); - assertThat((Collection) templateContext.get("posts"), contains(post2, post1)); - assertThat(((Pagination) 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 index 0000000..cfde866 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/BookmarksPageTest.kt @@ -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().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, contains(post2, post3, post1)) + assertThat((templateContext["pagination"] as Pagination).items, contains(post2, post3, post1)) + assertThat(templateContext["postsNotLoaded"], equalTo(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, contains(post2, post1)) + assertThat((templateContext["pagination"] as Pagination).items, contains(post2, post1)) + assertThat(templateContext["postsNotLoaded"], equalTo(true)) + } + +}