138a41e8f38e2df5dbac4265d69788cd25b189e7
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / NotificationHandlerModuleTest.kt
1 /**
2  * Sone - NotificationHandlerModuleTest.kt - Copyright © 2019 David ‘Bombe’ Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web.notification
19
20 import com.google.inject.*
21 import com.google.inject.Guice.*
22 import com.google.inject.name.Names.*
23 import net.pterodactylus.sone.core.*
24 import net.pterodactylus.sone.core.event.*
25 import net.pterodactylus.sone.data.*
26 import net.pterodactylus.sone.data.Post.*
27 import net.pterodactylus.sone.data.impl.*
28 import net.pterodactylus.sone.database.*
29 import net.pterodactylus.sone.freenet.wot.*
30 import net.pterodactylus.sone.main.*
31 import net.pterodactylus.sone.notify.*
32 import net.pterodactylus.sone.test.*
33 import net.pterodactylus.sone.text.*
34 import net.pterodactylus.sone.utils.*
35 import net.pterodactylus.util.notify.*
36 import org.hamcrest.MatcherAssert.*
37 import org.hamcrest.Matchers.*
38 import org.mockito.*
39 import org.mockito.Mockito.*
40 import java.util.concurrent.*
41 import java.util.concurrent.TimeUnit.*
42 import java.util.function.*
43 import kotlin.test.*
44
45 /**
46  * Unit test for [NotificationHandlerModule].
47  */
48 class NotificationHandlerModuleTest {
49
50         private val core = mock<Core>()
51         private val webOfTrustConnector = mock<WebOfTrustConnector>()
52         private val ticker = mock<ScheduledExecutorService>()
53         private val notificationManager = NotificationManager()
54         private val loaders = TestLoaders()
55         private val injector: Injector = createInjector(
56                         Core::class.isProvidedBy(core),
57                         NotificationManager::class.isProvidedBy(notificationManager),
58                         Loaders::class.isProvidedBy(loaders),
59                         WebOfTrustConnector::class.isProvidedBy(webOfTrustConnector),
60                         ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
61                         SoneTextParser::class.isProvidedByMock(),
62                         PostReplyProvider::class.isProvidedByMock(),
63                         NotificationHandlerModule()
64         )
65
66         @Test
67         fun `notification handler is created as singleton`() {
68                 injector.verifySingletonInstance<NotificationHandler>()
69         }
70
71         @Test
72         fun `mark-post-known-during-first-start handler is created as singleton`() {
73                 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
74         }
75
76         @Test
77         fun `mark-post-known-during-first-start handler is created with correct action`() {
78                 notificationManager.firstStart()
79                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
80                 val post = mock<Post>()
81                 handler.newPostFound(NewPostFoundEvent(post))
82                 verify(core).markPostKnown(post)
83         }
84
85         @Test
86         fun `mark-post-reply-known-during-first-start handler is created as singleton`() {
87                 injector.verifySingletonInstance<MarkPostReplyKnownDuringFirstStartHandler>()
88         }
89
90         @Test
91         fun `mark-post-reply-known-during-first-start handler is created with correct action`() {
92                 notificationManager.firstStart()
93                 val handler = injector.getInstance<MarkPostReplyKnownDuringFirstStartHandler>()
94                 val postReply = mock<PostReply>()
95                 handler.newPostReply(NewPostReplyFoundEvent(postReply))
96                 verify(core).markReplyKnown(postReply)
97         }
98
99         @Test
100         fun `sone-locked-on-startup handler is created as singleton`() {
101                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
102         }
103
104         @Test
105         fun `module can create sone-locked-on-startup notification with correct id`() {
106                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
107                 assertThat(notification.id, equalTo("sone-locked-on-startup"))
108         }
109
110         @Test
111         fun `sone-locked-on-startup notification is created as singleton`() {
112                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
113         }
114
115         @Test
116         fun `module can create sone-locked-on-startup notification with correct template and key`() {
117                 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
118                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
119                 val sone1 = IdOnlySone("sone1")
120                 val sone2 = IdOnlySone("sone2")
121                 notification.add(sone1)
122                 notification.add(sone2)
123                 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
124         }
125
126         @Test
127         fun `sone-locked-on-startup notification is dismissable`() {
128                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
129         }
130
131         @Test
132         fun `new-sone handler is created as singleton`() {
133                 injector.verifySingletonInstance<NewSoneHandler>()
134         }
135
136         @Test
137         fun `new-sone notification has correct ID`() {
138                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
139         }
140
141         @Test
142         fun `new-sone notification has correct key and template`() {
143                 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
144                 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
145                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
146                 sones.forEach(notification::add)
147                 assertThat(notification.render(), equalTo(sones.toString()))
148         }
149
150         @Test
151         fun `new-sone notification is not dismissable`() {
152                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
153         }
154
155         @Test
156         fun `new-remote-post handler is created as singleton`() {
157                 injector.verifySingletonInstance<NewRemotePostHandler>()
158         }
159
160         @Test
161         fun `new-remote-post notification is created as singleton`() {
162                 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
163         }
164
165         @Test
166         fun `new-remote-post notification has correct ID`() {
167                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
168         }
169
170         @Test
171         fun `new-remote-post notification is not dismissable`() {
172                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
173         }
174
175         @Test
176         fun `new-remote-post notification has correct key and template`() {
177                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
178                 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
179                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
180                 posts.forEach(notification::add)
181                 assertThat(notification.render(), equalTo(posts.toString()))
182         }
183
184         @Test
185         fun `remote-post handler is created as singleton`() {
186                 injector.verifySingletonInstance<RemotePostReplyHandler>()
187         }
188
189         @Test
190         fun `new-remote-post-reply notification is created as singleton`() {
191                 injector.verifySingletonInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
192         }
193
194         @Test
195         fun `new-remote-post-reply notification has correct ID`() {
196                 assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).id, equalTo("new-reply-notification"))
197         }
198
199         @Test
200         fun `new-remote-post-reply notification is not dismissable`() {
201                 assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).isDismissable, equalTo(false))
202         }
203
204         @Test
205         fun `new-remote-post-reply notification has correct key and template`() {
206                 loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
207                 val notification = injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
208                 val postReplies = listOf(emptyPostReply(), emptyPostReply())
209                 postReplies.forEach(notification::add)
210                 assertThat(notification.render(), equalTo(postReplies.toString()))
211         }
212
213         @Test
214         fun `sone-locked notification is created as singleton`() {
215                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
216         }
217
218         @Test
219         fun `sone-locked notification is dismissable`() {
220                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
221         }
222
223         @Test
224         fun `sone-locked notification has correct ID`() {
225                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
226         }
227
228         @Test
229         fun `sone-locked notification has correct key and template`() {
230                 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
231                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
232                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
233                 sones.forEach(notification::add)
234                 assertThat(notification.render(), equalTo(sones.toString()))
235         }
236
237         @Test
238         fun `sone-locked handler is created as singleton`() {
239                 injector.verifySingletonInstance<SoneLockedHandler>()
240         }
241
242         @Test
243         fun `local-post notification is not dismissable`() {
244                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
245         }
246
247         @Test
248         fun `local-post notification has correct ID`() {
249                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
250         }
251
252         @Test
253         fun `local-post notification has correct key and template`() {
254                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
255                 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
256                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
257                 posts.forEach(notification::add)
258                 assertThat(notification.render(), equalTo(posts.toString()))
259         }
260
261         @Test
262         fun `local-post notification is created as singleton`() {
263                 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
264         }
265
266         @Test
267         fun `local-post handler is created as singleton`() {
268                 injector.verifySingletonInstance<LocalPostHandler>()
269         }
270
271         @Test
272         fun `new-version notification is created as singleton`() {
273                 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
274         }
275
276         @Test
277         fun `new-version notification has correct ID`() {
278                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
279         }
280
281         @Test
282         fun `new-version notification is dismissable`() {
283                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
284         }
285
286         @Test
287         fun `new-version notification loads correct template`() {
288                 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
289                 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
290                 assertThat(notification.render(), equalTo("1"))
291         }
292
293         @Test
294         fun `new-version handler is created as singleton`() {
295                 injector.verifySingletonInstance<NewVersionHandler>()
296         }
297
298         @Test
299         fun `inserting-image notification is created as singleton`() {
300                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
301         }
302
303         @Test
304         fun `inserting-image notification has correct ID`() {
305                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
306         }
307
308         @Test
309         fun `inserting-image notification is dismissable`() {
310                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
311         }
312
313         @Test
314         fun `inserting-image notification loads correct template`() {
315                 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
316                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
317                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
318                 assertThat(notification.render(), equalTo(images.toString()))
319         }
320
321         @Test
322         fun `inserting-image-failed notification is created as singleton`() {
323                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
324         }
325
326         @Test
327         fun `inserting-image-failed notification has correct ID`() {
328                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
329         }
330
331         @Test
332         fun `inserting-image-failed notification is dismissable`() {
333                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
334         }
335
336         @Test
337         fun `inserting-image-failed notification loads correct template`() {
338                 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
339                 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
340                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
341                 assertThat(notification.render(), equalTo(images.toString()))
342         }
343
344         @Test
345         fun `inserted-image notification is created as singleton`() {
346                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
347         }
348
349         @Test
350         fun `inserted-image notification has correct ID`() {
351                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
352         }
353
354         @Test
355         fun `inserted-image notification is dismissable`() {
356                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
357         }
358
359         @Test
360         fun `inserted-image notification loads correct template`() {
361                 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
362                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
363                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
364                 assertThat(notification.render(), equalTo(images.toString()))
365         }
366
367         @Test
368         fun `image insert handler is created as singleton`() {
369                 injector.verifySingletonInstance<ImageInsertHandler>()
370         }
371
372         @Test
373         fun `first-start notification is created as singleton`() {
374                 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
375         }
376
377         @Test
378         fun `first-start notification has correct ID`() {
379                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
380         }
381
382         @Test
383         fun `first-start notification is dismissable`() {
384                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
385         }
386
387         @Test
388         fun `first-start notification loads correct template`() {
389                 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
390                 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
391                 assertThat(notification.render(), equalTo("1"))
392         }
393
394         @Test
395         fun `first-start handler is created as singleton`() {
396                 injector.verifySingletonInstance<FirstStartHandler>()
397         }
398
399         @Test
400         fun `config-not-read notification is created as singleton`() {
401                 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
402         }
403
404         @Test
405         fun `config-not-read notification has correct ID `() {
406                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
407         }
408
409         @Test
410         fun `config-not-read notification is dismissable`() {
411                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
412         }
413
414         @Test
415         fun `config-not-read notification loads correct template`() {
416                 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
417                 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
418                 assertThat(notification.render(), equalTo("1"))
419         }
420
421         @Test
422         fun `config-not-read handler is created as singleton`() {
423                 injector.verifySingletonInstance<ConfigNotReadHandler>()
424         }
425
426         @Test
427         fun `startup notification can be created`() {
428                 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
429         }
430
431         @Test
432         fun `startup notification has correct ID`() {
433                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
434         }
435
436         @Test
437         fun `startup notification is dismissable`() {
438                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
439         }
440
441         @Test
442         fun `startup notification loads correct template`() {
443                 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
444                 val notification = injector.getInstance<TemplateNotification>(named("startup"))
445                 assertThat(notification.render(), equalTo("1"))
446         }
447
448         @Test
449         fun `startup handler is created as singleton`() {
450                 injector.verifySingletonInstance<StartupHandler>()
451         }
452
453         @Test
454         fun `web-of-trust notification is created as singleton`() {
455                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
456         }
457
458         @Test
459         fun `web-of-trust notification has correct ID`() {
460                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
461         }
462
463         @Test
464         fun `web-of-trust notification is dismissable`() {
465                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
466         }
467
468         @Test
469         fun `web-of-trust notification loads correct template`() {
470                 loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
471                 val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
472                 assertThat(notification.render(), equalTo("1"))
473         }
474
475         @Test
476         fun `web-of-trust handler is created as singleton`() {
477                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
478         }
479
480         @Test
481         fun `web-of-trust reacher is created as singleton`() {
482                 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
483         }
484
485         @Test
486         fun `web-of-trust reacher access the wot connector`() {
487                 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
488                 verify(webOfTrustConnector).ping()
489         }
490
491         @Test
492         fun `web-of-trust reschedule is created as singleton`() {
493                 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
494         }
495
496         @Test
497         fun `web-of-trust reschedule schedules at the correct delay`() {
498                 val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
499                 injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
500                 verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
501         }
502
503         @Test
504         fun `sone mention detector is created as singleton`() {
505                 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
506         }
507
508         @Test
509         fun `sone-mentioned notification is created as singleton`() {
510                 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
511         }
512
513         @Test
514         fun `sone-mentioned notification has correct ID`() {
515                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
516         }
517
518         @Test
519         fun `sone-mentioned notification is not dismissable`() {
520                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
521         }
522
523         @Test
524         fun `sone-mentioned notification loads correct template`() {
525                 loaders.templates += "/templates/notify/mentionNotification.html" to "<% posts>".asTemplate()
526                 val notification = injector.getInstance<ListNotification<Post>>(named("soneMentioned"))
527                 val posts = listOf(EmptyPost("1"), EmptyPost("2")).onEach(notification::add)
528                 assertThat(notification.render(), equalTo(posts.toString()))
529         }
530
531         @Test
532         fun `sone-mentioned handler is created as singleton`() {
533                 injector.verifySingletonInstance<SoneMentionedHandler>()
534         }
535
536 }