From: David ‘Bombe’ Roden Date: Sun, 26 Jun 2016 17:43:55 +0000 (+0200) Subject: Fix post duplication but in “new posts & replies” page X-Git-Tag: 0.9.5^2~16 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ed4690d3d7ee4bda179aacadf9db461a06d60e9c;p=Sone.git Fix post duplication but in “new posts & replies” page --- diff --git a/src/main/java/net/pterodactylus/sone/web/NewPage.java b/src/main/java/net/pterodactylus/sone/web/NewPage.java index 6aa07d9..af0843a 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -21,7 +21,9 @@ import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; @@ -64,16 +66,17 @@ public class NewPage extends SoneTemplatePage { super.processTemplate(request, templateContext); /* collect new elements from notifications. */ - List posts = new ArrayList(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false))); + Set posts = new HashSet(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false))); for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) { posts.add(reply.getPost().get()); } /* filter and sort them. */ - Collections.sort(posts, Post.TIME_COMPARATOR); + List sortedPosts = new ArrayList(posts); + Collections.sort(sortedPosts, Post.TIME_COMPARATOR); /* paginate them. */ - Pagination pagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("page"), 0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", pagination); templateContext.set("posts", pagination.getItems()); } diff --git a/src/test/java/net/pterodactylus/sone/web/NewPageTest.java b/src/test/java/net/pterodactylus/sone/web/NewPageTest.java new file mode 100644 index 0000000..b527e58 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/NewPageTest.java @@ -0,0 +1,74 @@ +package net.pterodactylus.sone.web; + +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.notify.Notification; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; + +import freenet.clients.http.ToadletContext; + +import com.google.common.base.Optional; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link NewPage}. + * + * @author David ‘Bombe’ Roden + */ +public class NewPageTest { + + private final Template template = mock(Template.class); + private final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS); + private final NewPage newPage = new NewPage(template, webInterface); + private final Sone currentSone = mock(Sone.class); + private final TemplateContext templateContext = new TemplateContext(); + private final FreenetRequest freenetRequest = mock(FreenetRequest.class, RETURNS_DEEP_STUBS); + + @Before + public void setupFreenetRequest() { + when(freenetRequest.getToadletContext()).thenReturn(mock(ToadletContext.class)); + } + + @Before + public void setupWebInterface() { + when(webInterface.getCore().getPreferences().getPostsPerPage()).thenReturn(5); + when(webInterface.getCurrentSone(any(ToadletContext.class), anyBoolean())).thenReturn(currentSone); + when(webInterface.getNotifications(any(Sone.class))).thenReturn(Collections.emptyList()); + } + + @Test + public void postsAreNotDuplicatedWhenTheyComeFromBothNewPostsAndNewRepliesNotifications() throws Exception { + // given + Post extraPost = mock(Post.class); + List posts = asList(mock(Post.class), mock(Post.class)); + List postReplies = asList(mock(PostReply.class), mock(PostReply.class)); + when(postReplies.get(0).getPost()).thenReturn(Optional.of(posts.get(0))); + when(postReplies.get(1).getPost()).thenReturn(Optional.of(extraPost)); + when(webInterface.getNewPosts(currentSone)).thenReturn(posts); + when(webInterface.getNewReplies(currentSone)).thenReturn(postReplies); + + // when + newPage.processTemplate(freenetRequest, templateContext); + + // then + List renderedPosts = templateContext.get("posts", List.class); + assertThat(renderedPosts, containsInAnyOrder(posts.get(0), posts.get(1), extraPost)); + } + +}