--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@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 <code>null</code>, 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<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
+ List<Notification> filteredNotifications = new ArrayList<Notification>();
+ 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<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) 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<ListNotification<PostReply>> filteredNotification =
+ filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
+ if (filteredNotification.isPresent()) {
+ filteredNotifications.add(filteredNotification.get());
+ }
+ } else if (notification.getId().equals("mention-notification")) {
+ Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) 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<ListNotification<Post>> filterNewPostNotification(@Nonnull ListNotification<Post> newPostNotification,
+ @Nonnull Sone currentSone) {
+ List<Post> 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<Post> filteredNotification = new ListNotification<Post>(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<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
+ @Nonnull Sone currentSone) {
+ List<PostReply> 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<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
+ filteredNotification.setElements(newReplies);
+ filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
+ return Optional.of(filteredNotification);
+ }
+
+}
+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-@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 <code>null</code>, 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<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
- List<Notification> filteredNotifications = new ArrayList<Notification>();
- 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<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) 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<ListNotification<PostReply>> filteredNotification =
- filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
- if (filteredNotification.isPresent()) {
- filteredNotifications.add(filteredNotification.get());
- }
- } else if (notification.getId().equals("mention-notification")) {
- Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) 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<ListNotification<Post>> filterNewPostNotification(@Nonnull ListNotification<Post> newPostNotification,
- @Nonnull Sone currentSone) {
- List<Post> 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<Post> filteredNotification = new ListNotification<Post>(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<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
- @Nonnull Sone currentSone) {
- List<PostReply> 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<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
- filteredNotification.setElements(newReplies);
- filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
- return Optional.of(filteredNotification);
- }
-
-}
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;
/** The parser filter. */
private final ParserFilter parserFilter;
- private final ListNotificationFilters listNotificationFilters;
+ private final ListNotificationFilter listNotificationFilter;
private final PostVisibilityFilter postVisibilityFilter;
private final ReplyVisibilityFilter replyVisibilityFilter;
* 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();
@Nonnull
public Collection<Notification> getNotifications(@Nullable Sone currentSone) {
- return listNotificationFilters.filterNotifications(notificationManager.getNotifications(), currentSone);
+ return listNotificationFilter.filterNotifications(notificationManager.getNotifications(), currentSone);
}
/**
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<ListNotification<Post>> newPostNotifications = Arrays.asList(createNewPostNotification());
+ private final List<ListNotification<PostReply>> newReplyNotifications = Arrays.asList(createNewReplyNotification());
+ private final List<ListNotification<Post>> 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<Notification> notifications = Arrays.asList(createNewSoneNotification());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
+ assertThat(filteredNotifications, contains(notifications.get(0)));
+ }
+
+ private Notification createNewSoneNotification() {
+ ListNotification<Sone> newSoneNotification = mock(ListNotification.class);
+ when(newSoneNotification.getId()).thenReturn("new-sone-notification");
+ return newSoneNotification;
+ }
+
+ @Test
+ public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() {
+ List<Notification> notifications = Arrays.asList(createNewSoneNotification());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
+ assertThat(filteredNotifications, emptyIterable());
+ }
+
+ @Test
+ public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() {
+ List<Notification> notifications = Arrays.asList(createNewSoneNotification());
+ when(soneOptions.isShowNewSoneNotifications()).thenReturn(true);
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
+ assertThat(filteredNotifications, contains(notifications.get(0)));
+ }
+
+ private ListNotification<Post> createNewPostNotification() {
+ ListNotification<Post> newSoneNotification = mock(ListNotification.class);
+ when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
+ when(newSoneNotification.getId()).thenReturn("new-post-notification");
+ return newSoneNotification;
+ }
+
+ @Test
+ public void newPostNotificationIsNotShownIfOptionsSetAccordingly() {
+ List<ListNotification<Post>> notifications = Arrays.asList(createNewPostNotification());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
+ assertThat(filteredNotifications, hasSize(0));
+ }
+
+ private void activateNewPostNotifications() {
+ when(soneOptions.isShowNewPostNotifications()).thenReturn(true);
+ }
+
+ private boolean addPostToPostNotification(List<ListNotification<Post>> notifications) {
+ return notifications.get(0).getElements().add(mock(Post.class));
+ }
+
+ private void setPostVisibilityPredicate(Predicate<Post> value) {
+ when(postVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
+ }
+
+ @Test
+ public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() {
+ activateNewPostNotifications();
+ addPostToPostNotification(newPostNotifications);
+ setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(0));
+ }
+
+ @Test
+ public void newPostNotificationIsShownIfNewPostsAreVisible() {
+ activateNewPostNotifications();
+ addPostToPostNotification(newPostNotifications);
+ setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
+ assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0)));
+ }
+
+ @Test
+ public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() {
+ activateNewPostNotifications();
+ addPostToPostNotification(newPostNotifications);
+ setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null);
+ assertThat(filteredNotifications, Matchers.<Notification>emptyIterable());
+ }
+
+ @Test
+ public void newPostNotificationContainsOnlyVisiblePosts() {
+ activateNewPostNotifications();
+ addPostToPostNotification(newPostNotifications);
+ addPostToPostNotification(newPostNotifications);
+ setPostVisibilityPredicate(new Predicate<Post>() {
+ @Override
+ public boolean apply(@Nullable Post post) {
+ return post.equals(newPostNotifications.get(0).getElements().get(1));
+ }
+ });
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(1));
+ assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1)));
+ }
+
+ private ListNotification<PostReply> createNewReplyNotification() {
+ ListNotification<PostReply> newReplyNotifications = mock(ListNotification.class);
+ when(newReplyNotifications.getElements()).thenReturn(new ArrayList<PostReply>());
+ when(newReplyNotifications.getId()).thenReturn("new-reply-notification");
+ return newReplyNotifications;
+ }
+
+ private void activateNewReplyNotifications() {
+ when(soneOptions.isShowNewReplyNotifications()).thenReturn(true);
+ }
+
+ private void addReplyToNewReplyNotification(List<ListNotification<PostReply>> notifications) {
+ notifications.get(0).getElements().add(mock(PostReply.class));
+ }
+
+ private void setReplyVisibilityPredicate(Predicate<PostReply> value) {
+ when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
+ }
+
+ @Test
+ public void newReplyNotificationContainsOnlyVisibleReplies() {
+ activateNewReplyNotifications();
+ addReplyToNewReplyNotification(newReplyNotifications);
+ addReplyToNewReplyNotification(newReplyNotifications);
+ setReplyVisibilityPredicate(new Predicate<PostReply>() {
+ @Override
+ public boolean apply(@Nullable PostReply postReply) {
+ return postReply.equals(newReplyNotifications.get(0).getElements().get(1));
+ }
+ });
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(1));
+ assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1)));
+ }
+
+ @Test
+ public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() {
+ activateNewReplyNotifications();
+ addReplyToNewReplyNotification(newReplyNotifications);
+ addReplyToNewReplyNotification(newReplyNotifications);
+ setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(1));
+ assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0)));
+ assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements(), hasSize(2));
+ }
+
+ @Test
+ public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() {
+ activateNewReplyNotifications();
+ addReplyToNewReplyNotification(newReplyNotifications);
+ addReplyToNewReplyNotification(newReplyNotifications);
+ setReplyVisibilityPredicate(Predicates.<PostReply>alwaysFalse());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(0));
+ }
+
+ @Test
+ public void newReplyNotificationIsNotShownIfDeactivatedInOptions() {
+ addReplyToNewReplyNotification(newReplyNotifications);
+ addReplyToNewReplyNotification(newReplyNotifications);
+ setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(0));
+ }
+
+ @Test
+ public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() {
+ addReplyToNewReplyNotification(newReplyNotifications);
+ addReplyToNewReplyNotification(newReplyNotifications);
+ setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null);
+ assertThat(filteredNotifications, hasSize(0));
+ }
+
+ private ListNotification<Post> createMentionNotification() {
+ ListNotification<Post> newSoneNotification = mock(ListNotification.class);
+ when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
+ when(newSoneNotification.getId()).thenReturn("mention-notification");
+ return newSoneNotification;
+ }
+
+ @Test
+ public void mentionNotificationContainsOnlyVisiblePosts() {
+ addPostToPostNotification(mentionNotifications);
+ addPostToPostNotification(mentionNotifications);
+ setPostVisibilityPredicate(new Predicate<Post>() {
+ @Override
+ public boolean apply(@Nullable Post post) {
+ return post.equals(mentionNotifications.get(0).getElements().get(1));
+ }
+ });
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
+ assertThat(filteredNotifications, hasSize(1));
+ assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1)));
+ }
+
+ @Test
+ public void mentionNotificationIsNotShownIfNoPostsAreVisible() {
+ addPostToPostNotification(mentionNotifications);
+ addPostToPostNotification(mentionNotifications);
+ setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
+ List<Notification> 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<Notification> notifications = Arrays.asList(notification);
+ List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
+ assertThat(filteredNotifications, contains(notification));
+ }
+
+}
+++ /dev/null
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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<ListNotification<Post>> newPostNotifications = Arrays.asList(createNewPostNotification());
- private final List<ListNotification<PostReply>> newReplyNotifications = Arrays.asList(createNewReplyNotification());
- private final List<ListNotification<Post>> 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<Notification> notifications = Arrays.asList(createNewSoneNotification());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(notifications, null);
- assertThat(filteredNotifications, contains(notifications.get(0)));
- }
-
- private Notification createNewSoneNotification() {
- ListNotification<Sone> newSoneNotification = mock(ListNotification.class);
- when(newSoneNotification.getId()).thenReturn("new-sone-notification");
- return newSoneNotification;
- }
-
- @Test
- public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() {
- List<Notification> notifications = Arrays.asList(createNewSoneNotification());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone);
- assertThat(filteredNotifications, emptyIterable());
- }
-
- @Test
- public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() {
- List<Notification> notifications = Arrays.asList(createNewSoneNotification());
- when(soneOptions.isShowNewSoneNotifications()).thenReturn(true);
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone);
- assertThat(filteredNotifications, contains(notifications.get(0)));
- }
-
- private ListNotification<Post> createNewPostNotification() {
- ListNotification<Post> newSoneNotification = mock(ListNotification.class);
- when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
- when(newSoneNotification.getId()).thenReturn("new-post-notification");
- return newSoneNotification;
- }
-
- @Test
- public void newPostNotificationIsNotShownIfOptionsSetAccordingly() {
- List<ListNotification<Post>> notifications = Arrays.asList(createNewPostNotification());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(notifications, localSone);
- assertThat(filteredNotifications, hasSize(0));
- }
-
- private void activateNewPostNotifications() {
- when(soneOptions.isShowNewPostNotifications()).thenReturn(true);
- }
-
- private boolean addPostToPostNotification(List<ListNotification<Post>> notifications) {
- return notifications.get(0).getElements().add(mock(Post.class));
- }
-
- private void setPostVisibilityPredicate(Predicate<Post> value) {
- when(postVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
- }
-
- @Test
- public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() {
- activateNewPostNotifications();
- addPostToPostNotification(newPostNotifications);
- setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone);
- assertThat(filteredNotifications, hasSize(0));
- }
-
- @Test
- public void newPostNotificationIsShownIfNewPostsAreVisible() {
- activateNewPostNotifications();
- addPostToPostNotification(newPostNotifications);
- setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone);
- assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0)));
- }
-
- @Test
- public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() {
- activateNewPostNotifications();
- addPostToPostNotification(newPostNotifications);
- setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, null);
- assertThat(filteredNotifications, Matchers.<Notification>emptyIterable());
- }
-
- @Test
- public void newPostNotificationContainsOnlyVisiblePosts() {
- activateNewPostNotifications();
- addPostToPostNotification(newPostNotifications);
- addPostToPostNotification(newPostNotifications);
- setPostVisibilityPredicate(new Predicate<Post>() {
- @Override
- public boolean apply(@Nullable Post post) {
- return post.equals(newPostNotifications.get(0).getElements().get(1));
- }
- });
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newPostNotifications, localSone);
- assertThat(filteredNotifications, hasSize(1));
- assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1)));
- }
-
- private ListNotification<PostReply> createNewReplyNotification() {
- ListNotification<PostReply> newReplyNotifications = mock(ListNotification.class);
- when(newReplyNotifications.getElements()).thenReturn(new ArrayList<PostReply>());
- when(newReplyNotifications.getId()).thenReturn("new-reply-notification");
- return newReplyNotifications;
- }
-
- private void activateNewReplyNotifications() {
- when(soneOptions.isShowNewReplyNotifications()).thenReturn(true);
- }
-
- private void addReplyToNewReplyNotification(List<ListNotification<PostReply>> notifications) {
- notifications.get(0).getElements().add(mock(PostReply.class));
- }
-
- private void setReplyVisibilityPredicate(Predicate<PostReply> value) {
- when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
- }
-
- @Test
- public void newReplyNotificationContainsOnlyVisibleReplies() {
- activateNewReplyNotifications();
- addReplyToNewReplyNotification(newReplyNotifications);
- addReplyToNewReplyNotification(newReplyNotifications);
- setReplyVisibilityPredicate(new Predicate<PostReply>() {
- @Override
- public boolean apply(@Nullable PostReply postReply) {
- return postReply.equals(newReplyNotifications.get(0).getElements().get(1));
- }
- });
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone);
- assertThat(filteredNotifications, hasSize(1));
- assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1)));
- }
-
- @Test
- public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() {
- activateNewReplyNotifications();
- addReplyToNewReplyNotification(newReplyNotifications);
- addReplyToNewReplyNotification(newReplyNotifications);
- setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone);
- assertThat(filteredNotifications, hasSize(1));
- assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0)));
- assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements(), hasSize(2));
- }
-
- @Test
- public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() {
- activateNewReplyNotifications();
- addReplyToNewReplyNotification(newReplyNotifications);
- addReplyToNewReplyNotification(newReplyNotifications);
- setReplyVisibilityPredicate(Predicates.<PostReply>alwaysFalse());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone);
- assertThat(filteredNotifications, hasSize(0));
- }
-
- @Test
- public void newReplyNotificationIsNotShownIfDeactivatedInOptions() {
- addReplyToNewReplyNotification(newReplyNotifications);
- addReplyToNewReplyNotification(newReplyNotifications);
- setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, localSone);
- assertThat(filteredNotifications, hasSize(0));
- }
-
- @Test
- public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() {
- addReplyToNewReplyNotification(newReplyNotifications);
- addReplyToNewReplyNotification(newReplyNotifications);
- setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(newReplyNotifications, null);
- assertThat(filteredNotifications, hasSize(0));
- }
-
- private ListNotification<Post> createMentionNotification() {
- ListNotification<Post> newSoneNotification = mock(ListNotification.class);
- when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
- when(newSoneNotification.getId()).thenReturn("mention-notification");
- return newSoneNotification;
- }
-
- @Test
- public void mentionNotificationContainsOnlyVisiblePosts() {
- addPostToPostNotification(mentionNotifications);
- addPostToPostNotification(mentionNotifications);
- setPostVisibilityPredicate(new Predicate<Post>() {
- @Override
- public boolean apply(@Nullable Post post) {
- return post.equals(mentionNotifications.get(0).getElements().get(1));
- }
- });
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(mentionNotifications, localSone);
- assertThat(filteredNotifications, hasSize(1));
- assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1)));
- }
-
- @Test
- public void mentionNotificationIsNotShownIfNoPostsAreVisible() {
- addPostToPostNotification(mentionNotifications);
- addPostToPostNotification(mentionNotifications);
- setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
- List<Notification> 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<Notification> notifications = Arrays.asList(notification);
- List<Notification> filteredNotifications = listNotificationFilters.filterNotifications(notifications, null);
- assertThat(filteredNotifications, contains(notification));
- }
-
-}