2 * Sone - NotificationHandlerModuleTest.kt - Copyright © 2019 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 `sone-locked notification is created as singleton`() {
186 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
190 fun `sone-locked notification is dismissable`() {
191 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
195 fun `sone-locked notification has correct ID`() {
196 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
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()))
209 fun `sone-locked handler is created as singleton`() {
210 injector.verifySingletonInstance<SoneLockedHandler>()
214 fun `local-post notification is not dismissable`() {
215 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
219 fun `local-post notification has correct ID`() {
220 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
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()))
233 fun `local-post notification is created as singleton`() {
234 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
238 fun `local-post handler is created as singleton`() {
239 injector.verifySingletonInstance<LocalPostHandler>()
243 fun `new-version notification is created as singleton`() {
244 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
248 fun `new-version notification has correct ID`() {
249 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
253 fun `new-version notification is dismissable`() {
254 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
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"))
265 fun `new-version handler is created as singleton`() {
266 injector.verifySingletonInstance<NewVersionHandler>()
270 fun `inserting-image notification is created as singleton`() {
271 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
275 fun `inserting-image notification has correct ID`() {
276 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
280 fun `inserting-image notification is dismissable`() {
281 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
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()))
293 fun `inserting-image-failed notification is created as singleton`() {
294 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
298 fun `inserting-image-failed notification has correct ID`() {
299 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
303 fun `inserting-image-failed notification is dismissable`() {
304 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
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()))
316 fun `inserted-image notification is created as singleton`() {
317 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
321 fun `inserted-image notification has correct ID`() {
322 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
326 fun `inserted-image notification is dismissable`() {
327 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
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()))
339 fun `image insert handler is created as singleton`() {
340 injector.verifySingletonInstance<ImageInsertHandler>()
344 fun `first-start notification is created as singleton`() {
345 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
349 fun `first-start notification has correct ID`() {
350 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
354 fun `first-start notification is dismissable`() {
355 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
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"))
366 fun `first-start handler is created as singleton`() {
367 injector.verifySingletonInstance<FirstStartHandler>()
371 fun `config-not-read notification is created as singleton`() {
372 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
376 fun `config-not-read notification has correct ID `() {
377 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
381 fun `config-not-read notification is dismissable`() {
382 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
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"))
393 fun `config-not-read handler is created as singleton`() {
394 injector.verifySingletonInstance<ConfigNotReadHandler>()
398 fun `startup notification can be created`() {
399 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
403 fun `startup notification has correct ID`() {
404 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
408 fun `startup notification is dismissable`() {
409 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
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"))
420 fun `startup handler is created as singleton`() {
421 injector.verifySingletonInstance<StartupHandler>()
425 fun `web-of-trust notification is created as singleton`() {
426 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
430 fun `web-of-trust notification has correct ID`() {
431 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
435 fun `web-of-trust notification is dismissable`() {
436 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
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"))
447 fun `web-of-trust handler is created as singleton`() {
448 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
452 fun `web-of-trust reacher is created as singleton`() {
453 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
457 fun `web-of-trust reacher access the wot connector`() {
458 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
459 verify(webOfTrustConnector).ping()
463 fun `web-of-trust reschedule is created as singleton`() {
464 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
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))
475 fun `sone mention detector is created as singleton`() {
476 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
480 fun `sone-mentioned notification is created as singleton`() {
481 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
485 fun `sone-mentioned notification has correct ID`() {
486 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
490 fun `sone-mentioned notification is not dismissable`() {
491 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
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()))
503 fun `sone-mentioned handler is created as singleton`() {
504 injector.verifySingletonInstance<SoneMentionedHandler>()