ffc1e5a47552e2bd347ca21c41561163a65c0518
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / notify / ListNotificationFilterTest.kt
1 package net.pterodactylus.sone.notify
2
3 import com.google.inject.Guice
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.PostReply
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.createLocalSone
8 import net.pterodactylus.sone.test.createPost
9 import net.pterodactylus.sone.test.createPostReply
10 import net.pterodactylus.sone.test.verifySingletonInstance
11 import net.pterodactylus.util.notify.Notification
12 import net.pterodactylus.util.notify.TemplateNotification
13 import net.pterodactylus.util.template.Template
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.contains
16 import org.hamcrest.Matchers.emptyIterable
17 import org.hamcrest.Matchers.equalTo
18 import org.hamcrest.Matchers.hasSize
19 import org.junit.Test
20
21 /**
22  * Unit test for [ListNotificationFilterTest].
23  */
24 class ListNotificationFilterTest {
25
26         private val listNotificationFilter = DefaultListNotificationFilter(showAllPosts, showAllReplies)
27
28         @Test
29         fun `filter is only created once`() {
30                 val injector = Guice.createInjector()
31                 injector.verifySingletonInstance<ListNotificationFilter>()
32         }
33
34         @Test
35         fun `new sone notifications are not removed if not logged in`() {
36                 val notification = createNewSoneNotification()
37                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null)
38                 assertThat(filteredNotifications, contains<Notification>(notification))
39         }
40
41         @Test
42         fun `new sone notifications are removed if logged in and new sones should not be shown`() {
43                 val notification = createNewSoneNotification()
44                 val localSone = createLocalSone()
45                 localSone.options.isShowNewSoneNotifications = false
46                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone)
47                 assertThat(filteredNotifications, emptyIterable())
48         }
49
50         @Test
51         fun `new sone notifications are not removed if logged in and new sones should be shown`() {
52                 val notification = createNewSoneNotification()
53                 val localSone = createLocalSone()
54                 localSone.options.isShowNewSoneNotifications = true
55                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone)
56                 assertThat(filteredNotifications, contains<Notification>(notification))
57         }
58
59         @Test
60         fun `new post notification is not shown if options set accordingly`() {
61                 val newPostNotification = createNewPostNotification()
62                 newPostNotification.add(createPost())
63                 val localSone = createLocalSone()
64                 localSone.options.isShowNewPostNotifications = false
65                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
66                 assertThat(filteredNotifications, emptyIterable())
67         }
68
69         @Test
70         fun `new post notification is not shown if no new posts are visible`() {
71                 val localSone = createLocalSone()
72                 localSone.options.isShowNewPostNotifications = true
73                 val newPostNotification = createNewPostNotification()
74                 newPostNotification.add(createPost())
75                 val listNotificationFilter = DefaultListNotificationFilter(showNoPosts, showAllReplies)
76                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
77                 assertThat(filteredNotifications, emptyIterable())
78         }
79
80         @Test
81         fun `new post notification is shown if new posts are visible`() {
82                 val localSone = createLocalSone()
83                 localSone.options.isShowNewPostNotifications = true
84                 val newPostNotification = createNewPostNotification()
85                 newPostNotification.add(createPost())
86                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
87                 assertThat(filteredNotifications, contains<Notification>(newPostNotification))
88         }
89
90         @Test
91         fun `new post notification is not shown if new posts are visible but local sone is null`() {
92                 val localSone = createLocalSone()
93                 localSone.options.isShowNewPostNotifications = true
94                 val newPostNotification = createNewPostNotification()
95                 newPostNotification.add(createPost())
96                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), null)
97                 assertThat(filteredNotifications, emptyIterable())
98         }
99
100         @Test
101         fun `new post notification contains only visible posts`() {
102                 val localSone = createLocalSone()
103                 localSone.options.isShowNewPostNotifications = true
104                 val newPostNotification = createNewPostNotification()
105                 newPostNotification.add(createPost())
106                 newPostNotification.add(createPost())
107                 val listNotificationFilter = DefaultListNotificationFilter(matchThisPost(newPostNotification.elements[1]), showAllReplies)
108                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
109                 assertThat((filteredNotifications[0] as ListNotification<Post>).elements, contains(newPostNotification.elements[1]))
110         }
111
112         @Test
113         fun `new reply notification contains only visible replies`() {
114                 val localSone = createLocalSone()
115                 localSone.options.isShowNewReplyNotifications = true
116                 val newReplyNotification = createNewReplyNotification()
117                 newReplyNotification.add(createPostReply())
118                 newReplyNotification.add(createPostReply())
119                 val listNotificationFilter = DefaultListNotificationFilter(showAllPosts, matchThisReply(newReplyNotification.elements[1]))
120                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
121                 assertThat(filteredNotifications, hasSize(1))
122                 assertThat((filteredNotifications[0] as ListNotification<PostReply?>).elements[0], equalTo(newReplyNotification.elements[1]))
123         }
124
125         @Test
126         fun `new reply notification is not modified if all replies are visible`() {
127                 val localSone = createLocalSone()
128                 localSone.options.isShowNewReplyNotifications = true
129                 val newReplyNotification = createNewReplyNotification()
130                 newReplyNotification.add(createPostReply())
131                 newReplyNotification.add(createPostReply())
132                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
133                 assertThat(filteredNotifications, contains<Notification>(newReplyNotification))
134         }
135
136         @Test
137         fun `new reply notification is not shown if no replies are visible`() {
138                 val localSone = createLocalSone()
139                 localSone.options.isShowNewReplyNotifications = true
140                 val newReplyNotification = createNewReplyNotification()
141                 newReplyNotification.add(createPostReply())
142                 newReplyNotification.add(createPostReply())
143                 val listNotificationFilter = DefaultListNotificationFilter(showAllPosts, showNoReplies)
144                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
145                 assertThat(filteredNotifications, emptyIterable())
146         }
147
148         @Test
149         fun `new reply notification is not shown if deactivated in options`() {
150                 val localSone = createLocalSone()
151                 localSone.options.isShowNewReplyNotifications = false
152                 val newReplyNotification = createNewReplyNotification()
153                 newReplyNotification.add(createPostReply())
154                 newReplyNotification.add(createPostReply())
155                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
156                 assertThat(filteredNotifications, emptyIterable())
157         }
158
159         @Test
160         fun `new reply notification is not shown if current sone is null`() {
161                 val newReplyNotification = createNewReplyNotification()
162                 newReplyNotification.add(createPostReply())
163                 newReplyNotification.add(createPostReply())
164                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), null)
165                 assertThat(filteredNotifications, emptyIterable())
166         }
167
168         @Test
169         fun `mention notification contains only visible posts`() {
170                 val mentionNotification = createMentionNotification()
171                 mentionNotification.add(createPost())
172                 mentionNotification.add(createPost())
173                 val listNotificationFilter = DefaultListNotificationFilter(matchThisPost(mentionNotification.elements[1]), showAllReplies)
174                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null)
175                 assertThat(filteredNotifications, hasSize(1))
176                 assertThat((filteredNotifications[0] as ListNotification<Post?>).elements[0], equalTo(mentionNotification.elements[1]))
177         }
178
179         @Test
180         fun `mention notification is not shown if no posts are visible`() {
181                 val mentionNotification = createMentionNotification()
182                 mentionNotification.add(createPost())
183                 mentionNotification.add(createPost())
184                 val listNotificationFilter = DefaultListNotificationFilter(showNoPosts, showAllReplies)
185                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null)
186                 assertThat(filteredNotifications, emptyIterable())
187         }
188
189
190         @Test
191         fun `unfilterable notification is not filtered`() {
192                 val notification: Notification = TemplateNotification("random-notification", Template())
193                 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null)
194                 assertThat(filteredNotifications, contains(notification))
195         }
196
197 }
198
199 private fun createNewSoneNotification() =
200                 ListNotification<Sone>("new-sone-notification", "", Template())
201
202 private fun createNewPostNotification() =
203                 ListNotification<Post>("new-post-notification", "", Template())
204
205 private fun createNewReplyNotification() =
206                 ListNotification<PostReply>("new-reply-notification", "", Template())
207
208 private fun createMentionNotification() =
209                 ListNotification<Post>("mention-notification", "", Template())