2 * Sone - NotificationHandlerModuleTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.notification
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.*
39 import org.mockito.Mockito.*
40 import java.util.concurrent.*
41 import java.util.concurrent.TimeUnit.*
42 import java.util.function.*
46 * Unit test for [NotificationHandlerModule].
48 class NotificationHandlerModuleTest {
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()
67 fun `notification handler is created as singleton`() {
68 injector.verifySingletonInstance<NotificationHandler>()
72 fun `mark-post-known-during-first-start handler is created as singleton`() {
73 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
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)
86 fun `mark-post-reply-known-during-first-start handler is created as singleton`() {
87 injector.verifySingletonInstance<MarkPostReplyKnownDuringFirstStartHandler>()
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)
100 fun `sone-locked-on-startup handler is created as singleton`() {
101 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
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"))
111 fun `sone-locked-on-startup notification is created as singleton`() {
112 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
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()))
127 fun `sone-locked-on-startup notification is dismissable`() {
128 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
132 fun `new-sone handler is created as singleton`() {
133 injector.verifySingletonInstance<NewSoneHandler>()
137 fun `new-sone notification has correct ID`() {
138 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
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()))
151 fun `new-sone notification is not dismissable`() {
152 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
156 fun `new-remote-post handler is created as singleton`() {
157 injector.verifySingletonInstance<NewRemotePostHandler>()
161 fun `new-remote-post notification is created as singleton`() {
162 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
166 fun `new-remote-post notification has correct ID`() {
167 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
171 fun `new-remote-post notification is not dismissable`() {
172 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
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()))
185 fun `remote-post handler is created as singleton`() {
186 injector.verifySingletonInstance<RemotePostReplyHandler>()
190 fun `new-remote-post-reply notification is created as singleton`() {
191 injector.verifySingletonInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
195 fun `new-remote-post-reply notification has correct ID`() {
196 assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).id, equalTo("new-reply-notification"))
200 fun `new-remote-post-reply notification is not dismissable`() {
201 assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).isDismissable, equalTo(false))
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(createPostReply(), createPostReply())
209 postReplies.forEach(notification::add)
210 assertThat(notification.render(), equalTo(postReplies.toString()))
214 fun `sone-locked notification is created as singleton`() {
215 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
219 fun `sone-locked notification is dismissable`() {
220 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
224 fun `sone-locked notification has correct ID`() {
225 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
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()))
238 fun `sone-locked handler is created as singleton`() {
239 injector.verifySingletonInstance<SoneLockedHandler>()
243 fun `local-post notification is not dismissable`() {
244 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
248 fun `local-post notification has correct ID`() {
249 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
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()))
262 fun `local-post notification is created as singleton`() {
263 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
267 fun `local-post handler is created as singleton`() {
268 injector.verifySingletonInstance<LocalPostHandler>()
272 fun `local-reply notification is not dismissable`() {
273 assertThat(injector.getInstance<ListNotification<PostReply>>(named("localReply")).isDismissable, equalTo(false))
277 fun `local-reply notification has correct ID`() {
278 assertThat(injector.getInstance<ListNotification<PostReply>>(named("localReply")).id, equalTo("local-reply-notification"))
282 fun `local-reply notification has correct key and template`() {
283 loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
284 val notification = injector.getInstance<ListNotification<PostReply>>(named("localReply"))
285 val replies = listOf(createPostReply("reply1"), createPostReply("reply2"))
286 replies.forEach(notification::add)
287 assertThat(notification.render(), equalTo(replies.toString()))
291 fun `local-reply notification is created as singleton`() {
292 injector.verifySingletonInstance<ListNotification<PostReply>>(named("localReply"))
296 fun `local-reply handler is created as singleton`() {
297 injector.verifySingletonInstance<LocalReplyHandler>()
301 fun `new-version notification is created as singleton`() {
302 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
306 fun `new-version notification has correct ID`() {
307 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
311 fun `new-version notification is dismissable`() {
312 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
316 fun `new-version notification loads correct template`() {
317 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
318 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
319 assertThat(notification.render(), equalTo("1"))
323 fun `new-version handler is created as singleton`() {
324 injector.verifySingletonInstance<NewVersionHandler>()
328 fun `inserting-image notification is created as singleton`() {
329 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
333 fun `inserting-image notification has correct ID`() {
334 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
338 fun `inserting-image notification is dismissable`() {
339 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
343 fun `inserting-image notification loads correct template`() {
344 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
345 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
346 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
347 assertThat(notification.render(), equalTo(images.toString()))
351 fun `inserting-image-failed notification is created as singleton`() {
352 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
356 fun `inserting-image-failed notification has correct ID`() {
357 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
361 fun `inserting-image-failed notification is dismissable`() {
362 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
366 fun `inserting-image-failed notification loads correct template`() {
367 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
368 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
369 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
370 assertThat(notification.render(), equalTo(images.toString()))
374 fun `inserted-image notification is created as singleton`() {
375 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
379 fun `inserted-image notification has correct ID`() {
380 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
384 fun `inserted-image notification is dismissable`() {
385 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
389 fun `inserted-image notification loads correct template`() {
390 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
391 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
392 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
393 assertThat(notification.render(), equalTo(images.toString()))
397 fun `image insert handler is created as singleton`() {
398 injector.verifySingletonInstance<ImageInsertHandler>()
402 fun `first-start notification is created as singleton`() {
403 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
407 fun `first-start notification has correct ID`() {
408 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
412 fun `first-start notification is dismissable`() {
413 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
417 fun `first-start notification loads correct template`() {
418 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
419 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
420 assertThat(notification.render(), equalTo("1"))
424 fun `first-start handler is created as singleton`() {
425 injector.verifySingletonInstance<FirstStartHandler>()
429 fun `config-not-read notification is created as singleton`() {
430 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
434 fun `config-not-read notification has correct ID `() {
435 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
439 fun `config-not-read notification is dismissable`() {
440 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
444 fun `config-not-read notification loads correct template`() {
445 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
446 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
447 assertThat(notification.render(), equalTo("1"))
451 fun `config-not-read handler is created as singleton`() {
452 injector.verifySingletonInstance<ConfigNotReadHandler>()
456 fun `startup notification can be created`() {
457 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
461 fun `startup notification has correct ID`() {
462 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
466 fun `startup notification is dismissable`() {
467 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
471 fun `startup notification loads correct template`() {
472 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
473 val notification = injector.getInstance<TemplateNotification>(named("startup"))
474 assertThat(notification.render(), equalTo("1"))
478 fun `startup handler is created as singleton`() {
479 injector.verifySingletonInstance<StartupHandler>()
483 fun `web-of-trust notification is created as singleton`() {
484 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
488 fun `web-of-trust notification has correct ID`() {
489 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
493 fun `web-of-trust notification is dismissable`() {
494 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
498 fun `web-of-trust notification loads correct template`() {
499 loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
500 val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
501 assertThat(notification.render(), equalTo("1"))
505 fun `web-of-trust handler is created as singleton`() {
506 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
510 fun `web-of-trust reacher is created as singleton`() {
511 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
515 fun `web-of-trust reacher access the wot connector`() {
516 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
517 verify(webOfTrustConnector).ping()
521 fun `web-of-trust reschedule is created as singleton`() {
522 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
526 fun `web-of-trust reschedule schedules at the correct delay`() {
527 val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
528 injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
529 verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
533 fun `sone mention detector is created as singleton`() {
534 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
538 fun `sone-mentioned notification is created as singleton`() {
539 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
543 fun `sone-mentioned notification has correct ID`() {
544 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
548 fun `sone-mentioned notification is not dismissable`() {
549 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
553 fun `sone-mentioned notification loads correct template`() {
554 loaders.templates += "/templates/notify/mentionNotification.html" to "<% posts>".asTemplate()
555 val notification = injector.getInstance<ListNotification<Post>>(named("soneMentioned"))
556 val posts = listOf(EmptyPost("1"), EmptyPost("2")).onEach(notification::add)
557 assertThat(notification.render(), equalTo(posts.toString()))
561 fun `sone-mentioned handler is created as singleton`() {
562 injector.verifySingletonInstance<SoneMentionedHandler>()
566 fun `sone insert notification supplier is created as singleton`() {
567 injector.verifySingletonInstance<SoneInsertNotificationSupplier>()
571 fun `sone insert notification template is loaded correctly`() {
572 loaders.templates += "/templates/notify/soneInsertNotification.html" to "foo".asTemplate()
573 injector.getInstance<SoneInsertNotificationSupplier>()
574 .invoke(createRemoteSone())
576 .let { assertThat(it, equalTo("foo")) }
580 fun `sone notification supplier returns different notifications for different sones`() {
581 val supplier = injector.getInstance<SoneInsertNotificationSupplier>()
582 listOf(createRemoteSone(), createRemoteSone(), createRemoteSone())
585 .let { assertThat(it, hasSize(3)) }
589 fun `sone notification supplier caches notifications for a sone`() {
590 val supplier = injector.getInstance<SoneInsertNotificationSupplier>()
591 val sone = createRemoteSone()
592 listOf(sone, sone, sone)
595 .let { assertThat(it, hasSize(1)) }
599 fun `sone notification supplier sets sone in notification template`() {
600 val supplier = injector.getInstance<SoneInsertNotificationSupplier>()
601 val sone = createRemoteSone()
602 val templateNotification = supplier(sone)
603 assertThat(templateNotification["insertSone"], sameInstance<Any>(sone))
607 fun `sone insert handler is created as singleton`() {
608 injector.verifySingletonInstance<SoneInsertHandler>()