From 64ed0712bbfba30577c8df20bfac6bb25e6606b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 8 Jun 2016 21:39:39 +0200 Subject: [PATCH] Rename list notification filters class --- .../sone/notify/ListNotificationFilter.java | 170 +++++++++++++ .../sone/notify/ListNotificationFilters.java | 170 ------------- .../net/pterodactylus/sone/web/WebInterface.java | 10 +- .../sone/notify/ListNotificationFilterTest.java | 281 +++++++++++++++++++++ .../sone/notify/ListNotificationFiltersTest.java | 281 --------------------- 5 files changed, 456 insertions(+), 456 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java delete mode 100644 src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java create mode 100644 src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java delete mode 100644 src/test/java/net/pterodactylus/sone/notify/ListNotificationFiltersTest.java diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java new file mode 100644 index 0000000..77dc2ea --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java @@ -0,0 +1,170 @@ +/* + * Sone - ListNotificationFilters.java - Copyright © 2010–2015 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.notify; + +import static com.google.common.collect.FluentIterable.from; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.util.notify.Notification; + +import com.google.common.base.Optional; + +/** + * Filter for {@link ListNotification}s. + * + * @author David ‘Bombe’ Roden + */ +@Singleton +public class ListNotificationFilter { + + private final PostVisibilityFilter postVisibilityFilter; + private final ReplyVisibilityFilter replyVisibilityFilter; + + @Inject + public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) { + this.postVisibilityFilter = postVisibilityFilter; + this.replyVisibilityFilter = replyVisibilityFilter; + } + + /** + * Filters new-post and new-reply notifications in the given list of + * notifications. If {@code currentSone} is null, new-post and + * new-reply notifications are removed completely. If {@code currentSone} is + * not {@code null}, only posts that are posted by a friend Sone or the Sone + * itself, and replies that are replies to posts of friend Sones or the Sone + * itself will be retained in the notifications. + * + * @param notifications + * The notifications to filter + * @param currentSone + * The current Sone, or {@code null} if not logged in + * @return The filtered notifications + */ + @SuppressWarnings("unchecked") + public List filterNotifications(Collection notifications, Sone currentSone) { + List filteredNotifications = new ArrayList(); + for (Notification notification : notifications) { + if (notification.getId().equals("new-sone-notification")) { + if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) { + continue; + } + filteredNotifications.add(notification); + } else if (notification.getId().equals("new-post-notification")) { + if (currentSone == null) { + continue; + } + if (!currentSone.getOptions().isShowNewPostNotifications()) { + continue; + } + Optional> filteredNotification = filterNewPostNotification((ListNotification) notification, currentSone); + if (filteredNotification.isPresent()) { + filteredNotifications.add(filteredNotification.get()); + } + } else if (notification.getId().equals("new-reply-notification")) { + if (currentSone == null) { + continue; + } + if (!currentSone.getOptions().isShowNewReplyNotifications()) { + continue; + } + Optional> filteredNotification = + filterNewReplyNotification((ListNotification) notification, currentSone); + if (filteredNotification.isPresent()) { + filteredNotifications.add(filteredNotification.get()); + } + } else if (notification.getId().equals("mention-notification")) { + Optional> filteredNotification = filterNewPostNotification((ListNotification) notification, null); + if (filteredNotification.isPresent()) { + filteredNotifications.add(filteredNotification.get()); + } + } else { + filteredNotifications.add(notification); + } + } + return filteredNotifications; + } + + /** + * Filters the new posts of the given notification. If {@code currentSone} + * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is + * returned and the notification is subsequently removed. Otherwise only + * posts that are posted by friend Sones of the given Sone are retained; all + * other posts are removed. + * + * @param newPostNotification + * The new-post notification + * @param currentSone + * The current Sone, or {@code null} if not logged in + * @return The filtered new-post notification, or {@code null} if the + * notification should be removed + */ + @Nonnull + private Optional> filterNewPostNotification(@Nonnull ListNotification newPostNotification, + @Nonnull Sone currentSone) { + List newPosts = from(newPostNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList(); + if (newPosts.isEmpty()) { + return Optional.absent(); + } + if (newPosts.size() == newPostNotification.getElements().size()) { + return Optional.of(newPostNotification); + } + ListNotification filteredNotification = new ListNotification(newPostNotification); + filteredNotification.setElements(newPosts); + filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime()); + return Optional.of(filteredNotification); + } + + /** + * Filters the new replies of the given notification. If {@code currentSone} + * is {@code null}, {@code null} is returned and the notification is + * subsequently removed. Otherwise only replies that are replies to posts + * that are posted by friend Sones of the given Sone are retained; all other + * replies are removed. + * + * @param newReplyNotification + * The new-reply notification + * @param currentSone + * The current Sone, or {@code null} if not logged in + * @return The filtered new-reply notification, or {@code null} if the + * notification should be removed + */ + private Optional> filterNewReplyNotification(ListNotification newReplyNotification, + @Nonnull Sone currentSone) { + List newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList(); + if (newReplies.isEmpty()) { + return Optional.absent(); + } + if (newReplies.size() == newReplyNotification.getElements().size()) { + return Optional.of(newReplyNotification); + } + ListNotification filteredNotification = new ListNotification(newReplyNotification); + filteredNotification.setElements(newReplies); + filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime()); + return Optional.of(filteredNotification); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java deleted file mode 100644 index 0a2a518..0000000 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Sone - ListNotificationFilters.java - Copyright © 2010–2015 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.notify; - -import static com.google.common.collect.FluentIterable.from; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import javax.annotation.Nonnull; -import javax.inject.Inject; -import javax.inject.Singleton; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.util.notify.Notification; - -import com.google.common.base.Optional; - -/** - * Filter for {@link ListNotification}s. - * - * @author David ‘Bombe’ Roden - */ -@Singleton -public class ListNotificationFilters { - - private final PostVisibilityFilter postVisibilityFilter; - private final ReplyVisibilityFilter replyVisibilityFilter; - - @Inject - public ListNotificationFilters(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) { - this.postVisibilityFilter = postVisibilityFilter; - this.replyVisibilityFilter = replyVisibilityFilter; - } - - /** - * Filters new-post and new-reply notifications in the given list of - * notifications. If {@code currentSone} is null, new-post and - * new-reply notifications are removed completely. If {@code currentSone} is - * not {@code null}, only posts that are posted by a friend Sone or the Sone - * itself, and replies that are replies to posts of friend Sones or the Sone - * itself will be retained in the notifications. - * - * @param notifications - * The notifications to filter - * @param currentSone - * The current Sone, or {@code null} if not logged in - * @return The filtered notifications - */ - @SuppressWarnings("unchecked") - public List filterNotifications(Collection notifications, Sone currentSone) { - List filteredNotifications = new ArrayList(); - for (Notification notification : notifications) { - if (notification.getId().equals("new-sone-notification")) { - if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) { - continue; - } - filteredNotifications.add(notification); - } else if (notification.getId().equals("new-post-notification")) { - if (currentSone == null) { - continue; - } - if (!currentSone.getOptions().isShowNewPostNotifications()) { - continue; - } - Optional> filteredNotification = filterNewPostNotification((ListNotification) notification, currentSone); - if (filteredNotification.isPresent()) { - filteredNotifications.add(filteredNotification.get()); - } - } else if (notification.getId().equals("new-reply-notification")) { - if (currentSone == null) { - continue; - } - if (!currentSone.getOptions().isShowNewReplyNotifications()) { - continue; - } - Optional> filteredNotification = - filterNewReplyNotification((ListNotification) notification, currentSone); - if (filteredNotification.isPresent()) { - filteredNotifications.add(filteredNotification.get()); - } - } else if (notification.getId().equals("mention-notification")) { - Optional> filteredNotification = filterNewPostNotification((ListNotification) notification, null); - if (filteredNotification.isPresent()) { - filteredNotifications.add(filteredNotification.get()); - } - } else { - filteredNotifications.add(notification); - } - } - return filteredNotifications; - } - - /** - * Filters the new posts of the given notification. If {@code currentSone} - * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is - * returned and the notification is subsequently removed. Otherwise only - * posts that are posted by friend Sones of the given Sone are retained; all - * other posts are removed. - * - * @param newPostNotification - * The new-post notification - * @param currentSone - * The current Sone, or {@code null} if not logged in - * @return The filtered new-post notification, or {@code null} if the - * notification should be removed - */ - @Nonnull - private Optional> filterNewPostNotification(@Nonnull ListNotification newPostNotification, - @Nonnull Sone currentSone) { - List newPosts = from(newPostNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList(); - if (newPosts.isEmpty()) { - return Optional.absent(); - } - if (newPosts.size() == newPostNotification.getElements().size()) { - return Optional.of(newPostNotification); - } - ListNotification filteredNotification = new ListNotification(newPostNotification); - filteredNotification.setElements(newPosts); - filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime()); - return Optional.of(filteredNotification); - } - - /** - * Filters the new replies of the given notification. If {@code currentSone} - * is {@code null}, {@code null} is returned and the notification is - * subsequently removed. Otherwise only replies that are replies to posts - * that are posted by friend Sones of the given Sone are retained; all other - * replies are removed. - * - * @param newReplyNotification - * The new-reply notification - * @param currentSone - * The current Sone, or {@code null} if not logged in - * @return The filtered new-reply notification, or {@code null} if the - * notification should be removed - */ - private Optional> filterNewReplyNotification(ListNotification newReplyNotification, - @Nonnull Sone currentSone) { - List newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList(); - if (newReplies.isEmpty()) { - return Optional.absent(); - } - if (newReplies.size() == newReplyNotification.getElements().size()) { - return Optional.of(newReplyNotification); - } - ListNotification filteredNotification = new ListNotification(newReplyNotification); - filteredNotification.setElements(newReplies); - filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime()); - return Optional.of(filteredNotification); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 4e4f6fa..4185c93 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -75,7 +75,7 @@ import net.pterodactylus.sone.main.Loaders; import net.pterodactylus.sone.main.ReparseFilter; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotification; -import net.pterodactylus.sone.notify.ListNotificationFilters; +import net.pterodactylus.sone.notify.ListNotificationFilter; import net.pterodactylus.sone.notify.PostVisibilityFilter; import net.pterodactylus.sone.notify.ReplyVisibilityFilter; import net.pterodactylus.sone.template.AlbumAccessor; @@ -201,7 +201,7 @@ public class WebInterface { /** The parser filter. */ private final ParserFilter parserFilter; - private final ListNotificationFilters listNotificationFilters; + private final ListNotificationFilter listNotificationFilter; private final PostVisibilityFilter postVisibilityFilter; private final ReplyVisibilityFilter replyVisibilityFilter; @@ -254,10 +254,10 @@ public class WebInterface { * The Sone plugin */ @Inject - public WebInterface(SonePlugin sonePlugin, Loaders loaders, ListNotificationFilters listNotificationFilters, PostVisibilityFilter postVisibilityFilter, ReplyVisibilityFilter replyVisibilityFilter) { + public WebInterface(SonePlugin sonePlugin, Loaders loaders, ListNotificationFilter listNotificationFilter, PostVisibilityFilter postVisibilityFilter, ReplyVisibilityFilter replyVisibilityFilter) { this.sonePlugin = sonePlugin; this.loaders = loaders; - this.listNotificationFilters = listNotificationFilters; + this.listNotificationFilter = listNotificationFilter; this.postVisibilityFilter = postVisibilityFilter; this.replyVisibilityFilter = replyVisibilityFilter; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); @@ -475,7 +475,7 @@ public class WebInterface { @Nonnull public Collection getNotifications(@Nullable Sone currentSone) { - return listNotificationFilters.filterNotifications(notificationManager.getNotifications(), currentSone); + return listNotificationFilter.filterNotifications(notificationManager.getNotifications(), currentSone); } /** diff --git a/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java b/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java new file mode 100644 index 0000000..b29b2c1 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/notify/ListNotificationFilterTest.java @@ -0,0 +1,281 @@ +package net.pterodactylus.sone.notify; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.emptyIterable; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.annotation.Nullable; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.SoneOptions; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.util.notify.Notification; + +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit test for {@link ListNotificationFilterTest}. + * + * @author David ‘Bombe’ Roden + */ +public class ListNotificationFilterTest { + + private static final String LOCAL_ID = "local-id"; + + private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class); + private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class); + private final ListNotificationFilter listNotificationFilter = new ListNotificationFilter(postVisibilityFilter, replyVisibilityFilter); + + private final Sone localSone = mock(Sone.class); + private final SoneOptions soneOptions = mock(SoneOptions.class); + private final OwnIdentity localIdentity = mock(OwnIdentity.class); + private final List> newPostNotifications = Arrays.asList(createNewPostNotification()); + private final List> newReplyNotifications = Arrays.asList(createNewReplyNotification()); + private final List> mentionNotifications = Arrays.asList(createMentionNotification()); + + public ListNotificationFilterTest() { + when(localSone.getId()).thenReturn(LOCAL_ID); + when(localSone.isLocal()).thenReturn(true); + when(localSone.getIdentity()).thenReturn(localIdentity); + when(localIdentity.getId()).thenReturn(LOCAL_ID); + when(localSone.getOptions()).thenReturn(soneOptions); + } + + @Test + public void filterIsOnlyCreatedOnce() { + Injector injector = Guice.createInjector(); + ListNotificationFilter firstFilter = injector.getInstance(ListNotificationFilter.class); + ListNotificationFilter secondFilter = injector.getInstance(ListNotificationFilter.class); + assertThat(firstFilter, sameInstance(secondFilter)); + } + + @Test + public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() { + List notifications = Arrays.asList(createNewSoneNotification()); + List filteredNotifications = listNotificationFilter.filterNotifications(notifications, null); + assertThat(filteredNotifications, contains(notifications.get(0))); + } + + private Notification createNewSoneNotification() { + ListNotification newSoneNotification = mock(ListNotification.class); + when(newSoneNotification.getId()).thenReturn("new-sone-notification"); + return newSoneNotification; + } + + @Test + public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() { + List notifications = Arrays.asList(createNewSoneNotification()); + List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); + assertThat(filteredNotifications, emptyIterable()); + } + + @Test + public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() { + List notifications = Arrays.asList(createNewSoneNotification()); + when(soneOptions.isShowNewSoneNotifications()).thenReturn(true); + List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); + assertThat(filteredNotifications, contains(notifications.get(0))); + } + + private ListNotification createNewPostNotification() { + ListNotification newSoneNotification = mock(ListNotification.class); + when(newSoneNotification.getElements()).thenReturn(new ArrayList()); + when(newSoneNotification.getId()).thenReturn("new-post-notification"); + return newSoneNotification; + } + + @Test + public void newPostNotificationIsNotShownIfOptionsSetAccordingly() { + List> notifications = Arrays.asList(createNewPostNotification()); + List filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone); + assertThat(filteredNotifications, hasSize(0)); + } + + private void activateNewPostNotifications() { + when(soneOptions.isShowNewPostNotifications()).thenReturn(true); + } + + private boolean addPostToPostNotification(List> notifications) { + return notifications.get(0).getElements().add(mock(Post.class)); + } + + private void setPostVisibilityPredicate(Predicate value) { + when(postVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value); + } + + @Test + public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() { + activateNewPostNotifications(); + addPostToPostNotification(newPostNotifications); + setPostVisibilityPredicate(Predicates.alwaysFalse()); + List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); + assertThat(filteredNotifications, hasSize(0)); + } + + @Test + public void newPostNotificationIsShownIfNewPostsAreVisible() { + activateNewPostNotifications(); + addPostToPostNotification(newPostNotifications); + setPostVisibilityPredicate(Predicates.alwaysTrue()); + List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); + assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0))); + } + + @Test + public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() { + activateNewPostNotifications(); + addPostToPostNotification(newPostNotifications); + setPostVisibilityPredicate(Predicates.alwaysTrue()); + List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null); + assertThat(filteredNotifications, Matchers.emptyIterable()); + } + + @Test + public void newPostNotificationContainsOnlyVisiblePosts() { + activateNewPostNotifications(); + addPostToPostNotification(newPostNotifications); + addPostToPostNotification(newPostNotifications); + setPostVisibilityPredicate(new Predicate() { + @Override + public boolean apply(@Nullable Post post) { + return post.equals(newPostNotifications.get(0).getElements().get(1)); + } + }); + List filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone); + assertThat(filteredNotifications, hasSize(1)); + assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1))); + } + + private ListNotification createNewReplyNotification() { + ListNotification newReplyNotifications = mock(ListNotification.class); + when(newReplyNotifications.getElements()).thenReturn(new ArrayList()); + when(newReplyNotifications.getId()).thenReturn("new-reply-notification"); + return newReplyNotifications; + } + + private void activateNewReplyNotifications() { + when(soneOptions.isShowNewReplyNotifications()).thenReturn(true); + } + + private void addReplyToNewReplyNotification(List> notifications) { + notifications.get(0).getElements().add(mock(PostReply.class)); + } + + private void setReplyVisibilityPredicate(Predicate value) { + when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value); + } + + @Test + public void newReplyNotificationContainsOnlyVisibleReplies() { + activateNewReplyNotifications(); + addReplyToNewReplyNotification(newReplyNotifications); + addReplyToNewReplyNotification(newReplyNotifications); + setReplyVisibilityPredicate(new Predicate() { + @Override + public boolean apply(@Nullable PostReply postReply) { + return postReply.equals(newReplyNotifications.get(0).getElements().get(1)); + } + }); + List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); + assertThat(filteredNotifications, hasSize(1)); + assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1))); + } + + @Test + public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() { + activateNewReplyNotifications(); + addReplyToNewReplyNotification(newReplyNotifications); + addReplyToNewReplyNotification(newReplyNotifications); + setReplyVisibilityPredicate(Predicates.alwaysTrue()); + List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); + assertThat(filteredNotifications, hasSize(1)); + assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0))); + assertThat(((ListNotification) filteredNotifications.get(0)).getElements(), hasSize(2)); + } + + @Test + public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() { + activateNewReplyNotifications(); + addReplyToNewReplyNotification(newReplyNotifications); + addReplyToNewReplyNotification(newReplyNotifications); + setReplyVisibilityPredicate(Predicates.alwaysFalse()); + List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); + assertThat(filteredNotifications, hasSize(0)); + } + + @Test + public void newReplyNotificationIsNotShownIfDeactivatedInOptions() { + addReplyToNewReplyNotification(newReplyNotifications); + addReplyToNewReplyNotification(newReplyNotifications); + setReplyVisibilityPredicate(Predicates.alwaysTrue()); + List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone); + assertThat(filteredNotifications, hasSize(0)); + } + + @Test + public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() { + addReplyToNewReplyNotification(newReplyNotifications); + addReplyToNewReplyNotification(newReplyNotifications); + setReplyVisibilityPredicate(Predicates.alwaysTrue()); + List filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null); + assertThat(filteredNotifications, hasSize(0)); + } + + private ListNotification createMentionNotification() { + ListNotification newSoneNotification = mock(ListNotification.class); + when(newSoneNotification.getElements()).thenReturn(new ArrayList()); + when(newSoneNotification.getId()).thenReturn("mention-notification"); + return newSoneNotification; + } + + @Test + public void mentionNotificationContainsOnlyVisiblePosts() { + addPostToPostNotification(mentionNotifications); + addPostToPostNotification(mentionNotifications); + setPostVisibilityPredicate(new Predicate() { + @Override + public boolean apply(@Nullable Post post) { + return post.equals(mentionNotifications.get(0).getElements().get(1)); + } + }); + List filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone); + assertThat(filteredNotifications, hasSize(1)); + assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1))); + } + + @Test + public void mentionNotificationIsNotShownIfNoPostsAreVisible() { + addPostToPostNotification(mentionNotifications); + addPostToPostNotification(mentionNotifications); + setPostVisibilityPredicate(Predicates.alwaysFalse()); + List filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone); + assertThat(filteredNotifications, hasSize(0)); + } + + @Test + public void unfilterableNotificationIsNotFiltered() { + Notification notification = mock(Notification.class); + when(notification.getId()).thenReturn("random-notification"); + List notifications = Arrays.asList(notification); + List filteredNotifications = listNotificationFilter.filterNotifications(notifications, null); + assertThat(filteredNotifications, contains(notification)); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/notify/ListNotificationFiltersTest.java b/src/test/java/net/pterodactylus/sone/notify/ListNotificationFiltersTest.java deleted file mode 100644 index f6b7da5..0000000 --- a/src/test/java/net/pterodactylus/sone/notify/ListNotificationFiltersTest.java +++ /dev/null @@ -1,281 +0,0 @@ -package net.pterodactylus.sone.notify; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.emptyIterable; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.annotation.Nullable; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.data.SoneOptions; -import net.pterodactylus.sone.freenet.wot.OwnIdentity; -import net.pterodactylus.util.notify.Notification; - -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.inject.Guice; -import com.google.inject.Injector; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Unit test for {@link ListNotificationFiltersTest}. - * - * @author David ‘Bombe’ Roden - */ -public class ListNotificationFiltersTest { - - private static final String LOCAL_ID = "local-id"; - - private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class); - private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class); - private final ListNotificationFilters listNotificationFilters = new ListNotificationFilters(postVisibilityFilter, replyVisibilityFilter); - - private final Sone localSone = mock(Sone.class); - private final SoneOptions soneOptions = mock(SoneOptions.class); - private final OwnIdentity localIdentity = mock(OwnIdentity.class); - private final List> newPostNotifications = Arrays.asList(createNewPostNotification()); - private final List> newReplyNotifications = Arrays.asList(createNewReplyNotification()); - private final List> mentionNotifications = Arrays.asList(createMentionNotification()); - - public ListNotificationFiltersTest() { - when(localSone.getId()).thenReturn(LOCAL_ID); - when(localSone.isLocal()).thenReturn(true); - when(localSone.getIdentity()).thenReturn(localIdentity); - when(localIdentity.getId()).thenReturn(LOCAL_ID); - when(localSone.getOptions()).thenReturn(soneOptions); - } - - @Test - public void filterIsOnlyCreatedOnce() { - Injector injector = Guice.createInjector(); - ListNotificationFilters firstFilter = injector.getInstance(ListNotificationFilters.class); - ListNotificationFilters secondFilter = injector.getInstance(ListNotificationFilters.class); - assertThat(firstFilter, sameInstance(secondFilter)); - } - - @Test - public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() { - List notifications = Arrays.asList(createNewSoneNotification()); - List filteredNotifications = listNotificationFilters.filterNotifications(notifications, null); - assertThat(filteredNotifications, contains(notifications.get(0))); - } - - private Notification createNewSoneNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getId()).thenReturn("new-sone-notification"); - return newSoneNotification; - } - - @Test - public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() { - List notifications = Arrays.asList(createNewSoneNotification()); - List filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, emptyIterable()); - } - - @Test - public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() { - List notifications = Arrays.asList(createNewSoneNotification()); - when(soneOptions.isShowNewSoneNotifications()).thenReturn(true); - List filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, contains(notifications.get(0))); - } - - private ListNotification createNewPostNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getElements()).thenReturn(new ArrayList()); - when(newSoneNotification.getId()).thenReturn("new-post-notification"); - return newSoneNotification; - } - - @Test - public void newPostNotificationIsNotShownIfOptionsSetAccordingly() { - List> notifications = Arrays.asList(createNewPostNotification()); - List filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - private void activateNewPostNotifications() { - when(soneOptions.isShowNewPostNotifications()).thenReturn(true); - } - - private boolean addPostToPostNotification(List> notifications) { - return notifications.get(0).getElements().add(mock(Post.class)); - } - - private void setPostVisibilityPredicate(Predicate value) { - when(postVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value); - } - - @Test - public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newPostNotificationIsShownIfNewPostsAreVisible() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0))); - } - - @Test - public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, null); - assertThat(filteredNotifications, Matchers.emptyIterable()); - } - - @Test - public void newPostNotificationContainsOnlyVisiblePosts() { - activateNewPostNotifications(); - addPostToPostNotification(newPostNotifications); - addPostToPostNotification(newPostNotifications); - setPostVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable Post post) { - return post.equals(newPostNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1))); - } - - private ListNotification createNewReplyNotification() { - ListNotification newReplyNotifications = mock(ListNotification.class); - when(newReplyNotifications.getElements()).thenReturn(new ArrayList()); - when(newReplyNotifications.getId()).thenReturn("new-reply-notification"); - return newReplyNotifications; - } - - private void activateNewReplyNotifications() { - when(soneOptions.isShowNewReplyNotifications()).thenReturn(true); - } - - private void addReplyToNewReplyNotification(List> notifications) { - notifications.get(0).getElements().add(mock(PostReply.class)); - } - - private void setReplyVisibilityPredicate(Predicate value) { - when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value); - } - - @Test - public void newReplyNotificationContainsOnlyVisibleReplies() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable PostReply postReply) { - return postReply.equals(newReplyNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1))); - } - - @Test - public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0))); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements(), hasSize(2)); - } - - @Test - public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() { - activateNewReplyNotifications(); - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newReplyNotificationIsNotShownIfDeactivatedInOptions() { - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() { - addReplyToNewReplyNotification(newReplyNotifications); - addReplyToNewReplyNotification(newReplyNotifications); - setReplyVisibilityPredicate(Predicates.alwaysTrue()); - List filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, null); - assertThat(filteredNotifications, hasSize(0)); - } - - private ListNotification createMentionNotification() { - ListNotification newSoneNotification = mock(ListNotification.class); - when(newSoneNotification.getElements()).thenReturn(new ArrayList()); - when(newSoneNotification.getId()).thenReturn("mention-notification"); - return newSoneNotification; - } - - @Test - public void mentionNotificationContainsOnlyVisiblePosts() { - addPostToPostNotification(mentionNotifications); - addPostToPostNotification(mentionNotifications); - setPostVisibilityPredicate(new Predicate() { - @Override - public boolean apply(@Nullable Post post) { - return post.equals(mentionNotifications.get(0).getElements().get(1)); - } - }); - List filteredNotifications = listNotificationFilters.filterNotifications(mentionNotifications, localSone); - assertThat(filteredNotifications, hasSize(1)); - assertThat(((ListNotification) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1))); - } - - @Test - public void mentionNotificationIsNotShownIfNoPostsAreVisible() { - addPostToPostNotification(mentionNotifications); - addPostToPostNotification(mentionNotifications); - setPostVisibilityPredicate(Predicates.alwaysFalse()); - List filteredNotifications = listNotificationFilters.filterNotifications(mentionNotifications, localSone); - assertThat(filteredNotifications, hasSize(0)); - } - - @Test - public void unfilterableNotificationIsNotFiltered() { - Notification notification = mock(Notification.class); - when(notification.getId()).thenReturn("random-notification"); - List notifications = Arrays.asList(notification); - List filteredNotifications = listNotificationFilters.filterNotifications(notifications, null); - assertThat(filteredNotifications, contains(notification)); - } - -} -- 2.7.4