Remove @author tags
[Sone.git] / src / test / java / net / pterodactylus / sone / notify / ListNotificationFilterTest.java
1 package net.pterodactylus.sone.notify;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5 import static org.hamcrest.Matchers.emptyIterable;
6 import static org.hamcrest.Matchers.hasSize;
7 import static org.hamcrest.Matchers.is;
8 import static org.hamcrest.Matchers.sameInstance;
9 import static org.mockito.ArgumentMatchers.any;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import javax.annotation.Nullable;
18
19 import net.pterodactylus.sone.data.Post;
20 import net.pterodactylus.sone.data.PostReply;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.sone.data.SoneOptions;
23 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
24 import net.pterodactylus.util.notify.Notification;
25
26 import com.google.common.base.Predicate;
27 import com.google.common.base.Predicates;
28 import com.google.inject.Guice;
29 import com.google.inject.Injector;
30 import org.hamcrest.Matchers;
31 import org.junit.Test;
32 import org.mockito.ArgumentMatchers;
33
34 /**
35  * Unit test for {@link ListNotificationFilterTest}.
36  */
37 public class ListNotificationFilterTest {
38
39         private static final String LOCAL_ID = "local-id";
40
41         private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class);
42         private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class);
43         private final ListNotificationFilter listNotificationFilter = new ListNotificationFilter(postVisibilityFilter, replyVisibilityFilter);
44
45         private final Sone localSone = mock(Sone.class);
46         private final SoneOptions soneOptions = mock(SoneOptions.class);
47         private final OwnIdentity localIdentity = mock(OwnIdentity.class);
48         private final List<ListNotification<Post>> newPostNotifications = Arrays.asList(createNewPostNotification());
49         private final List<ListNotification<PostReply>> newReplyNotifications = Arrays.asList(createNewReplyNotification());
50         private final List<ListNotification<Post>> mentionNotifications = Arrays.asList(createMentionNotification());
51
52         public ListNotificationFilterTest() {
53                 when(localSone.getId()).thenReturn(LOCAL_ID);
54                 when(localSone.isLocal()).thenReturn(true);
55                 when(localSone.getIdentity()).thenReturn(localIdentity);
56                 when(localIdentity.getId()).thenReturn(LOCAL_ID);
57                 when(localSone.getOptions()).thenReturn(soneOptions);
58         }
59
60         @Test
61         public void filterIsOnlyCreatedOnce() {
62             Injector injector = Guice.createInjector();
63                 ListNotificationFilter firstFilter = injector.getInstance(ListNotificationFilter.class);
64                 ListNotificationFilter secondFilter = injector.getInstance(ListNotificationFilter.class);
65                 assertThat(firstFilter, sameInstance(secondFilter));
66         }
67
68         @Test
69         public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() {
70                 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
71                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
72                 assertThat(filteredNotifications, contains(notifications.get(0)));
73         }
74
75         private Notification createNewSoneNotification() {
76                 ListNotification<Sone> newSoneNotification = mock(ListNotification.class);
77                 when(newSoneNotification.getId()).thenReturn("new-sone-notification");
78                 return newSoneNotification;
79         }
80
81         @Test
82         public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() {
83                 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
84                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
85                 assertThat(filteredNotifications, emptyIterable());
86         }
87
88         @Test
89         public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() {
90                 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
91                 when(soneOptions.isShowNewSoneNotifications()).thenReturn(true);
92                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
93                 assertThat(filteredNotifications, contains(notifications.get(0)));
94         }
95
96         private ListNotification<Post> createNewPostNotification() {
97                 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
98                 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
99                 when(newSoneNotification.getId()).thenReturn("new-post-notification");
100                 return newSoneNotification;
101         }
102
103         @Test
104         public void newPostNotificationIsNotShownIfOptionsSetAccordingly() {
105                 List<ListNotification<Post>> notifications = Arrays.asList(createNewPostNotification());
106                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
107                 assertThat(filteredNotifications, hasSize(0));
108         }
109
110         private void activateNewPostNotifications() {
111                 when(soneOptions.isShowNewPostNotifications()).thenReturn(true);
112         }
113
114         private boolean addPostToPostNotification(List<ListNotification<Post>> notifications) {
115                 return notifications.get(0).getElements().add(mock(Post.class));
116         }
117
118         private void setPostVisibilityPredicate(Predicate<Post> value) {
119                 when(postVisibilityFilter.isVisible(ArgumentMatchers.<Sone>any())).thenReturn(value);
120         }
121
122         @Test
123         public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() {
124                 activateNewPostNotifications();
125                 addPostToPostNotification(newPostNotifications);
126                 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
127                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
128                 assertThat(filteredNotifications, hasSize(0));
129         }
130
131         @Test
132         public void newPostNotificationIsShownIfNewPostsAreVisible() {
133                 activateNewPostNotifications();
134                 addPostToPostNotification(newPostNotifications);
135                 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
136                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
137                 assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0)));
138         }
139
140         @Test
141         public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() {
142                 activateNewPostNotifications();
143                 addPostToPostNotification(newPostNotifications);
144                 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
145                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null);
146                 assertThat(filteredNotifications, Matchers.<Notification>emptyIterable());
147         }
148
149         @Test
150         public void newPostNotificationContainsOnlyVisiblePosts() {
151                 activateNewPostNotifications();
152                 addPostToPostNotification(newPostNotifications);
153                 addPostToPostNotification(newPostNotifications);
154                 setPostVisibilityPredicate(new Predicate<Post>() {
155                         @Override
156                         public boolean apply(@Nullable Post post) {
157                                 return post.equals(newPostNotifications.get(0).getElements().get(1));
158                         }
159                 });
160                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
161                 assertThat(filteredNotifications, hasSize(1));
162                 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1)));
163         }
164
165         private ListNotification<PostReply> createNewReplyNotification() {
166                 ListNotification<PostReply> newReplyNotifications = mock(ListNotification.class);
167                 when(newReplyNotifications.getElements()).thenReturn(new ArrayList<PostReply>());
168                 when(newReplyNotifications.getId()).thenReturn("new-reply-notification");
169                 return newReplyNotifications;
170         }
171
172         private void activateNewReplyNotifications() {
173                 when(soneOptions.isShowNewReplyNotifications()).thenReturn(true);
174         }
175
176         private void addReplyToNewReplyNotification(List<ListNotification<PostReply>> notifications) {
177                 notifications.get(0).getElements().add(mock(PostReply.class));
178         }
179
180         private void setReplyVisibilityPredicate(Predicate<PostReply> value) {
181                 when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
182         }
183
184         @Test
185         public void newReplyNotificationContainsOnlyVisibleReplies() {
186                 activateNewReplyNotifications();
187                 addReplyToNewReplyNotification(newReplyNotifications);
188                 addReplyToNewReplyNotification(newReplyNotifications);
189                 setReplyVisibilityPredicate(new Predicate<PostReply>() {
190                         @Override
191                         public boolean apply(@Nullable PostReply postReply) {
192                                 return postReply.equals(newReplyNotifications.get(0).getElements().get(1));
193                         }
194                 });
195                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
196                 assertThat(filteredNotifications, hasSize(1));
197                 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1)));
198         }
199
200         @Test
201         public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() {
202                 activateNewReplyNotifications();
203                 addReplyToNewReplyNotification(newReplyNotifications);
204                 addReplyToNewReplyNotification(newReplyNotifications);
205                 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
206                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
207                 assertThat(filteredNotifications, hasSize(1));
208                 assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0)));
209                 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements(), hasSize(2));
210         }
211
212         @Test
213         public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() {
214                 activateNewReplyNotifications();
215                 addReplyToNewReplyNotification(newReplyNotifications);
216                 addReplyToNewReplyNotification(newReplyNotifications);
217                 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysFalse());
218                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
219                 assertThat(filteredNotifications, hasSize(0));
220         }
221
222         @Test
223         public void newReplyNotificationIsNotShownIfDeactivatedInOptions() {
224                 addReplyToNewReplyNotification(newReplyNotifications);
225                 addReplyToNewReplyNotification(newReplyNotifications);
226                 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
227                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
228                 assertThat(filteredNotifications, hasSize(0));
229         }
230
231         @Test
232         public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() {
233                 addReplyToNewReplyNotification(newReplyNotifications);
234                 addReplyToNewReplyNotification(newReplyNotifications);
235                 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
236                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null);
237                 assertThat(filteredNotifications, hasSize(0));
238         }
239
240         private ListNotification<Post> createMentionNotification() {
241                 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
242                 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
243                 when(newSoneNotification.getId()).thenReturn("mention-notification");
244                 return newSoneNotification;
245         }
246
247         @Test
248         public void mentionNotificationContainsOnlyVisiblePosts() {
249                 addPostToPostNotification(mentionNotifications);
250                 addPostToPostNotification(mentionNotifications);
251                 setPostVisibilityPredicate(new Predicate<Post>() {
252                         @Override
253                         public boolean apply(@Nullable Post post) {
254                                 return post.equals(mentionNotifications.get(0).getElements().get(1));
255                         }
256                 });
257                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
258                 assertThat(filteredNotifications, hasSize(1));
259                 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1)));
260         }
261
262         @Test
263         public void mentionNotificationIsNotShownIfNoPostsAreVisible() {
264                 addPostToPostNotification(mentionNotifications);
265                 addPostToPostNotification(mentionNotifications);
266                 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
267                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
268                 assertThat(filteredNotifications, hasSize(0));
269         }
270
271         @Test
272         public void unfilterableNotificationIsNotFiltered() {
273                 Notification notification = mock(Notification.class);
274                 when(notification.getId()).thenReturn("random-notification");
275                 List<Notification> notifications = Arrays.asList(notification);
276                 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
277                 assertThat(filteredNotifications, contains(notification));
278         }
279
280 }