From: David ‘Bombe’ Roden Date: Wed, 12 Oct 2016 20:10:16 +0000 (+0200) Subject: Add test for page that shows bookmarked posts X-Git-Tag: 0.9.6^2~21 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f4cc2308ed23058a9cee4e6dcf4ffbf77659234d;p=Sone.git Add test for page that shows bookmarked posts --- diff --git a/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java b/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java new file mode 100644 index 0000000..2412414 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java @@ -0,0 +1,72 @@ +package net.pterodactylus.sone.web; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +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 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); + Post post2 = createPost(true); + Post post3 = createPost(true); + Set bookmarkedPosts = createBookmarkedPosts(post1, post2, post3); + when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts); + when(core.getPreferences().getPostsPerPage()).thenReturn(5); + page.processTemplate(freenetRequest, templateContext); + assertThat((Collection) templateContext.get("posts"), containsInAnyOrder(post1, post2, post3)); + 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); + Post post2 = createPost(true); + Post post3 = createPost(false); + Set bookmarkedPosts = createBookmarkedPosts(post1, post2, post3); + when(core.getBookmarkedPosts()).thenReturn(bookmarkedPosts); + when(core.getPreferences().getPostsPerPage()).thenReturn(5); + page.processTemplate(freenetRequest, templateContext); + assertThat((Collection) templateContext.get("posts"), containsInAnyOrder(post1, post2)); + assertThat(((Boolean) templateContext.get("postsNotLoaded")), is(true)); + } + + private Post createPost(boolean postLoaded) { + return when(mock(Post.class).isLoaded()).thenReturn(postLoaded).getMock(); + } + +}