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