a4ed7afcc207ac632731f6351aeca507fc68750d
[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 `sone-locked notification is created as singleton`() {
186                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
187         }
188
189         @Test
190         fun `sone-locked notification is dismissable`() {
191                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
192         }
193
194         @Test
195         fun `sone-locked notification has correct ID`() {
196                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
197         }
198
199         @Test
200         fun `sone-locked notification has correct key and template`() {
201                 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
202                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
203                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
204                 sones.forEach(notification::add)
205                 assertThat(notification.render(), equalTo(sones.toString()))
206         }
207
208         @Test
209         fun `sone-locked handler is created as singleton`() {
210                 injector.verifySingletonInstance<SoneLockedHandler>()
211         }
212
213         @Test
214         fun `local-post notification is not dismissable`() {
215                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
216         }
217
218         @Test
219         fun `local-post notification has correct ID`() {
220                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
221         }
222
223         @Test
224         fun `local-post notification has correct key and template`() {
225                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
226                 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
227                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
228                 posts.forEach(notification::add)
229                 assertThat(notification.render(), equalTo(posts.toString()))
230         }
231
232         @Test
233         fun `local-post notification is created as singleton`() {
234                 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
235         }
236
237         @Test
238         fun `local-post handler is created as singleton`() {
239                 injector.verifySingletonInstance<LocalPostHandler>()
240         }
241
242         @Test
243         fun `new-version notification is created as singleton`() {
244                 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
245         }
246
247         @Test
248         fun `new-version notification has correct ID`() {
249                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
250         }
251
252         @Test
253         fun `new-version notification is dismissable`() {
254                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
255         }
256
257         @Test
258         fun `new-version notification loads correct template`() {
259                 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
260                 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
261                 assertThat(notification.render(), equalTo("1"))
262         }
263
264         @Test
265         fun `new-version handler is created as singleton`() {
266                 injector.verifySingletonInstance<NewVersionHandler>()
267         }
268
269         @Test
270         fun `inserting-image notification is created as singleton`() {
271                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
272         }
273
274         @Test
275         fun `inserting-image notification has correct ID`() {
276                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
277         }
278
279         @Test
280         fun `inserting-image notification is dismissable`() {
281                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
282         }
283
284         @Test
285         fun `inserting-image notification loads correct template`() {
286                 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
287                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
288                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
289                 assertThat(notification.render(), equalTo(images.toString()))
290         }
291
292         @Test
293         fun `inserting-image-failed notification is created as singleton`() {
294                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
295         }
296
297         @Test
298         fun `inserting-image-failed notification has correct ID`() {
299                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
300         }
301
302         @Test
303         fun `inserting-image-failed notification is dismissable`() {
304                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
305         }
306
307         @Test
308         fun `inserting-image-failed notification loads correct template`() {
309                 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
310                 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
311                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
312                 assertThat(notification.render(), equalTo(images.toString()))
313         }
314
315         @Test
316         fun `inserted-image notification is created as singleton`() {
317                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
318         }
319
320         @Test
321         fun `inserted-image notification has correct ID`() {
322                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
323         }
324
325         @Test
326         fun `inserted-image notification is dismissable`() {
327                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
328         }
329
330         @Test
331         fun `inserted-image notification loads correct template`() {
332                 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
333                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
334                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
335                 assertThat(notification.render(), equalTo(images.toString()))
336         }
337
338         @Test
339         fun `image insert handler is created as singleton`() {
340                 injector.verifySingletonInstance<ImageInsertHandler>()
341         }
342
343         @Test
344         fun `first-start notification is created as singleton`() {
345                 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
346         }
347
348         @Test
349         fun `first-start notification has correct ID`() {
350                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
351         }
352
353         @Test
354         fun `first-start notification is dismissable`() {
355                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
356         }
357
358         @Test
359         fun `first-start notification loads correct template`() {
360                 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
361                 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
362                 assertThat(notification.render(), equalTo("1"))
363         }
364
365         @Test
366         fun `first-start handler is created as singleton`() {
367                 injector.verifySingletonInstance<FirstStartHandler>()
368         }
369
370         @Test
371         fun `config-not-read notification is created as singleton`() {
372                 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
373         }
374
375         @Test
376         fun `config-not-read notification has correct ID `() {
377                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
378         }
379
380         @Test
381         fun `config-not-read notification is dismissable`() {
382                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
383         }
384
385         @Test
386         fun `config-not-read notification loads correct template`() {
387                 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
388                 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
389                 assertThat(notification.render(), equalTo("1"))
390         }
391
392         @Test
393         fun `config-not-read handler is created as singleton`() {
394                 injector.verifySingletonInstance<ConfigNotReadHandler>()
395         }
396
397         @Test
398         fun `startup notification can be created`() {
399                 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
400         }
401
402         @Test
403         fun `startup notification has correct ID`() {
404                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
405         }
406
407         @Test
408         fun `startup notification is dismissable`() {
409                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
410         }
411
412         @Test
413         fun `startup notification loads correct template`() {
414                 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
415                 val notification = injector.getInstance<TemplateNotification>(named("startup"))
416                 assertThat(notification.render(), equalTo("1"))
417         }
418
419         @Test
420         fun `startup handler is created as singleton`() {
421                 injector.verifySingletonInstance<StartupHandler>()
422         }
423
424         @Test
425         fun `web-of-trust notification is created as singleton`() {
426                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
427         }
428
429         @Test
430         fun `web-of-trust notification has correct ID`() {
431                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
432         }
433
434         @Test
435         fun `web-of-trust notification is dismissable`() {
436                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
437         }
438
439         @Test
440         fun `web-of-trust notification loads correct template`() {
441                 loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
442                 val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
443                 assertThat(notification.render(), equalTo("1"))
444         }
445
446         @Test
447         fun `web-of-trust handler is created as singleton`() {
448                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
449         }
450
451         @Test
452         fun `web-of-trust reacher is created as singleton`() {
453                 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
454         }
455
456         @Test
457         fun `web-of-trust reacher access the wot connector`() {
458                 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
459                 verify(webOfTrustConnector).ping()
460         }
461
462         @Test
463         fun `web-of-trust reschedule is created as singleton`() {
464                 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
465         }
466
467         @Test
468         fun `web-of-trust reschedule schedules at the correct delay`() {
469                 val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
470                 injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
471                 verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
472         }
473
474         @Test
475         fun `sone mention detector is created as singleton`() {
476                 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
477         }
478
479         @Test
480         fun `sone-mentioned notification is created as singleton`() {
481                 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
482         }
483
484         @Test
485         fun `sone-mentioned notification has correct ID`() {
486                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
487         }
488
489         @Test
490         fun `sone-mentioned notification is not dismissable`() {
491                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
492         }
493
494         @Test
495         fun `sone-mentioned notification loads correct template`() {
496                 loaders.templates += "/templates/notify/mentionNotification.html" to "<% posts>".asTemplate()
497                 val notification = injector.getInstance<ListNotification<Post>>(named("soneMentioned"))
498                 val posts = listOf(EmptyPost("1"), EmptyPost("2")).onEach(notification::add)
499                 assertThat(notification.render(), equalTo(posts.toString()))
500         }
501
502         @Test
503         fun `sone-mentioned handler is created as singleton`() {
504                 injector.verifySingletonInstance<SoneMentionedHandler>()
505         }
506
507 }