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;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
+import net.pterodactylus.util.collection.Pagination;
import org.junit.Test;
@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));
}
@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;
}
}