Verify that posts are sorted correctly
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 20 Oct 2016 17:37:35 +0000 (19:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 20 Oct 2016 17:37:35 +0000 (19:37 +0200)
src/test/java/net/pterodactylus/sone/web/BookmarksPageTest.java

index 2412414..46b0a18 100644 (file)
@@ -1,7 +1,7 @@
 package net.pterodactylus.sone.web;
 
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.is;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -12,6 +12,7 @@ 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;
 
@@ -32,14 +33,15 @@ public class BookmarksPageTest extends WebPageTest {
        @Test
        @SuppressWarnings("unchecked")
        public void pageSetsCorrectPostsInTemplateContext() throws RedirectException {
-               Post post1 = createPost(true);
-               Post post2 = createPost(true);
-               Post post3 = createPost(true);
+               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);
                when(core.getPreferences().getPostsPerPage()).thenReturn(5);
                page.processTemplate(freenetRequest, templateContext);
-               assertThat((Collection<Post>) templateContext.get("posts"), containsInAnyOrder(post1, post2, post3));
+               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));
        }
 
@@ -54,19 +56,23 @@ public class BookmarksPageTest extends WebPageTest {
        @Test
        @SuppressWarnings("unchecked")
        public void notLoadedPostsAreNotIncludedButAFlagIsSet() throws RedirectException {
-               Post post1 = createPost(true);
-               Post post2 = createPost(true);
-               Post post3 = createPost(false);
+               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);
                when(core.getPreferences().getPostsPerPage()).thenReturn(5);
                page.processTemplate(freenetRequest, templateContext);
-               assertThat((Collection<Post>) templateContext.get("posts"), containsInAnyOrder(post1, post2));
+               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) {
-               return when(mock(Post.class).isLoaded()).thenReturn(postLoaded).getMock();
+       private Post createPost(boolean postLoaded, long time) {
+               Post post = mock(Post.class);
+               when(post.isLoaded()).thenReturn(postLoaded);
+               when(post.getTime()).thenReturn(time);
+               return post;
        }
 
 }