497ae1db4d82dc48c1d49241ab02a6721eb02d17
[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.hamcrest.Matchers.*
39 import org.mockito.*
40 import org.mockito.Mockito.*
41 import java.util.concurrent.*
42 import java.util.concurrent.TimeUnit.*
43 import java.util.function.*
44 import kotlin.test.*
45
46 /**
47  * Unit test for [NotificationHandlerModule].
48  */
49 class NotificationHandlerModuleTest {
50
51         private val core = mock<Core>()
52         private val webOfTrustConnector = mock<WebOfTrustConnector>()
53         private val ticker = mock<ScheduledExecutorService>()
54         private val notificationManager = NotificationManager()
55         private val loaders = TestLoaders()
56         private val injector: Injector = createInjector(
57                         Core::class.isProvidedBy(core),
58                         NotificationManager::class.isProvidedBy(notificationManager),
59                         Loaders::class.isProvidedBy(loaders),
60                         WebOfTrustConnector::class.isProvidedBy(webOfTrustConnector),
61                         ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
62                         SoneTextParser::class.isProvidedByMock(),
63                         PostReplyProvider::class.isProvidedByMock(),
64                         NotificationHandlerModule()
65         )
66
67         @Test
68         fun `notification handler is created as singleton`() {
69                 injector.verifySingletonInstance<NotificationHandler>()
70         }
71
72         @Test
73         fun `mark-post-known-during-first-start handler is created as singleton`() {
74                 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
75         }
76
77         @Test
78         fun `mark-post-known-during-first-start handler is created with correct action`() {
79                 notificationManager.firstStart()
80                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
81                 val post = mock<Post>()
82                 handler.newPostFound(NewPostFoundEvent(post))
83                 verify(core).markPostKnown(post)
84         }
85
86         @Test
87         fun `sone-locked-on-startup handler is created as singleton`() {
88                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
89         }
90
91         @Test
92         fun `module can create sone-locked-on-startup notification with correct id`() {
93                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
94                 assertThat(notification.id, equalTo("sone-locked-on-startup"))
95         }
96
97         @Test
98         fun `sone-locked-on-startup notification is created as singleton`() {
99                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
100         }
101
102         @Test
103         fun `module can create sone-locked-on-startup notification with correct template and key`() {
104                 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
105                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
106                 val sone1 = IdOnlySone("sone1")
107                 val sone2 = IdOnlySone("sone2")
108                 notification.add(sone1)
109                 notification.add(sone2)
110                 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
111         }
112
113         @Test
114         fun `sone-locked-on-startup notification is dismissable`() {
115                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
116         }
117
118         @Test
119         fun `new-sone handler is created as singleton`() {
120                 injector.verifySingletonInstance<NewSoneHandler>()
121         }
122
123         @Test
124         fun `new-sone notification has correct ID`() {
125                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
126         }
127
128         @Test
129         fun `new-sone notification has correct key and template`() {
130                 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
131                 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
132                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
133                 sones.forEach(notification::add)
134                 assertThat(notification.render(), equalTo(sones.toString()))
135         }
136
137         @Test
138         fun `new-sone notification is not dismissable`() {
139                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
140         }
141
142         @Test
143         fun `new-remote-post handler is created as singleton`() {
144                 injector.verifySingletonInstance<NewRemotePostHandler>()
145         }
146
147         @Test
148         fun `new-remote-post notification is created as singleton`() {
149                 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
150         }
151
152         @Test
153         fun `new-remote-post notification has correct ID`() {
154                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
155         }
156
157         @Test
158         fun `new-remote-post notification is not dismissable`() {
159                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
160         }
161
162         @Test
163         fun `new-remote-post notification has correct key and template`() {
164                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
165                 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
166                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
167                 posts.forEach(notification::add)
168                 assertThat(notification.render(), equalTo(posts.toString()))
169         }
170
171         @Test
172         fun `sone-locked notification is created as singleton`() {
173                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
174         }
175
176         @Test
177         fun `sone-locked notification is dismissable`() {
178                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
179         }
180
181         @Test
182         fun `sone-locked notification has correct ID`() {
183                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
184         }
185
186         @Test
187         fun `sone-locked notification has correct key and template`() {
188                 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
189                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
190                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
191                 sones.forEach(notification::add)
192                 assertThat(notification.render(), equalTo(sones.toString()))
193         }
194
195         @Test
196         fun `sone-locked handler is created as singleton`() {
197                 injector.verifySingletonInstance<SoneLockedHandler>()
198         }
199
200         @Test
201         fun `local-post notification is not dismissable`() {
202                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
203         }
204
205         @Test
206         fun `local-post notification has correct ID`() {
207                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
208         }
209
210         @Test
211         fun `local-post notification has correct key and template`() {
212                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
213                 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
214                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
215                 posts.forEach(notification::add)
216                 assertThat(notification.render(), equalTo(posts.toString()))
217         }
218
219         @Test
220         fun `local-post notification is created as singleton`() {
221                 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
222         }
223
224         @Test
225         fun `local-post handler is created as singleton`() {
226                 injector.verifySingletonInstance<LocalPostHandler>()
227         }
228
229         @Test
230         fun `new-version notification is created as singleton`() {
231                 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
232         }
233
234         @Test
235         fun `new-version notification has correct ID`() {
236                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
237         }
238
239         @Test
240         fun `new-version notification is dismissable`() {
241                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
242         }
243
244         @Test
245         fun `new-version notification loads correct template`() {
246                 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
247                 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
248                 assertThat(notification.render(), equalTo("1"))
249         }
250
251         @Test
252         fun `new-version handler is created as singleton`() {
253                 injector.verifySingletonInstance<NewVersionHandler>()
254         }
255
256         @Test
257         fun `inserting-image notification is created as singleton`() {
258                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
259         }
260
261         @Test
262         fun `inserting-image notification has correct ID`() {
263                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
264         }
265
266         @Test
267         fun `inserting-image notification is dismissable`() {
268                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
269         }
270
271         @Test
272         fun `inserting-image notification loads correct template`() {
273                 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
274                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
275                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
276                 assertThat(notification.render(), equalTo(images.toString()))
277         }
278
279         @Test
280         fun `inserting-image-failed notification is created as singleton`() {
281                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
282         }
283
284         @Test
285         fun `inserting-image-failed notification has correct ID`() {
286                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
287         }
288
289         @Test
290         fun `inserting-image-failed notification is dismissable`() {
291                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
292         }
293
294         @Test
295         fun `inserting-image-failed notification loads correct template`() {
296                 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
297                 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
298                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
299                 assertThat(notification.render(), equalTo(images.toString()))
300         }
301
302         @Test
303         fun `inserted-image notification is created as singleton`() {
304                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
305         }
306
307         @Test
308         fun `inserted-image notification has correct ID`() {
309                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
310         }
311
312         @Test
313         fun `inserted-image notification is dismissable`() {
314                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
315         }
316
317         @Test
318         fun `inserted-image notification loads correct template`() {
319                 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
320                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
321                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
322                 assertThat(notification.render(), equalTo(images.toString()))
323         }
324
325         @Test
326         fun `image insert handler is created as singleton`() {
327                 injector.verifySingletonInstance<ImageInsertHandler>()
328         }
329
330         @Test
331         fun `first-start notification is created as singleton`() {
332                 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
333         }
334
335         @Test
336         fun `first-start notification has correct ID`() {
337                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
338         }
339
340         @Test
341         fun `first-start notification is dismissable`() {
342                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
343         }
344
345         @Test
346         fun `first-start notification loads correct template`() {
347                 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
348                 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
349                 assertThat(notification.render(), equalTo("1"))
350         }
351
352         @Test
353         fun `first-start handler is created as singleton`() {
354                 injector.verifySingletonInstance<FirstStartHandler>()
355         }
356
357         @Test
358         fun `config-not-read notification is created as singleton`() {
359                 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
360         }
361
362         @Test
363         fun `config-not-read notification has correct ID `() {
364                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
365         }
366
367         @Test
368         fun `config-not-read notification is dismissable`() {
369                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
370         }
371
372         @Test
373         fun `config-not-read notification loads correct template`() {
374                 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
375                 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
376                 assertThat(notification.render(), equalTo("1"))
377         }
378
379         @Test
380         fun `config-not-read handler is created as singleton`() {
381                 injector.verifySingletonInstance<ConfigNotReadHandler>()
382         }
383
384         @Test
385         fun `startup notification can be created`() {
386                 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
387         }
388
389         @Test
390         fun `startup notification has correct ID`() {
391                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
392         }
393
394         @Test
395         fun `startup notification is dismissable`() {
396                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
397         }
398
399         @Test
400         fun `startup notification loads correct template`() {
401                 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
402                 val notification = injector.getInstance<TemplateNotification>(named("startup"))
403                 assertThat(notification.render(), equalTo("1"))
404         }
405
406         @Test
407         fun `startup handler is created as singleton`() {
408                 injector.verifySingletonInstance<StartupHandler>()
409         }
410
411         @Test
412         fun `web-of-trust notification is created as singleton`() {
413                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
414         }
415
416         @Test
417         fun `web-of-trust notification has correct ID`() {
418                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
419         }
420
421         @Test
422         fun `web-of-trust notification is dismissable`() {
423                 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
424         }
425
426         @Test
427         fun `web-of-trust notification loads correct template`() {
428                 loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
429                 val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
430                 assertThat(notification.render(), equalTo("1"))
431         }
432
433         @Test
434         fun `web-of-trust handler is created as singleton`() {
435                 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
436         }
437
438         @Test
439         fun `web-of-trust reacher is created as singleton`() {
440                 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
441         }
442
443         @Test
444         fun `web-of-trust reacher access the wot connector`() {
445                 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
446                 verify(webOfTrustConnector).ping()
447         }
448
449         @Test
450         fun `web-of-trust reschedule is created as singleton`() {
451                 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
452         }
453
454         @Test
455         fun `web-of-trust reschedule schedules at the correct delay`() {
456                 val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
457                 injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
458                 verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
459         }
460
461         @Test
462         fun `sone mention detector is created as singleton`() {
463                 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
464         }
465
466         @Test
467         fun `sone-mentioned notification is created as singleton`() {
468                 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
469         }
470
471         @Test
472         fun `sone-mentioned notification has correct ID`() {
473                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
474         }
475
476         @Test
477         fun `sone-mentioned notification is not dismissable`() {
478                 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
479         }
480
481         @Test
482         fun `sone-mentioned notification loads correct template`() {
483                 loaders.templates += "/templates/notify/mentionNotification.html" to "<% posts>".asTemplate()
484                 val notification = injector.getInstance<ListNotification<Post>>(named("soneMentioned"))
485                 val posts = listOf(EmptyPost("1"), EmptyPost("2")).onEach(notification::add)
486                 assertThat(notification.render(), equalTo(posts.toString()))
487         }
488
489         @Test
490         fun `sone-mentioned handler is created as singleton`() {
491                 injector.verifySingletonInstance<SoneMentionedHandler>()
492         }
493
494 }