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
38 import org.hamcrest.Matchers.*
40 import org.mockito.Mockito.*
41 import java.util.concurrent.*
42 import java.util.concurrent.TimeUnit.*
43 import java.util.function.*
47 * Unit test for [NotificationHandlerModule].
49 class NotificationHandlerModuleTest {
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()
68 fun `notification handler is created as singleton`() {
69 injector.verifySingletonInstance<NotificationHandler>()
73 fun `mark-post-known-during-first-start handler is created as singleton`() {
74 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
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)
87 fun `sone-locked-on-startup handler is created as singleton`() {
88 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
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"))
98 fun `sone-locked-on-startup notification is created as singleton`() {
99 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
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()))
114 fun `sone-locked-on-startup notification is dismissable`() {
115 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
119 fun `new-sone handler is created as singleton`() {
120 injector.verifySingletonInstance<NewSoneHandler>()
124 fun `new-sone notification has correct ID`() {
125 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
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()))
138 fun `new-sone notification is not dismissable`() {
139 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
143 fun `new-remote-post handler is created as singleton`() {
144 injector.verifySingletonInstance<NewRemotePostHandler>()
148 fun `new-remote-post notification is created as singleton`() {
149 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
153 fun `new-remote-post notification has correct ID`() {
154 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
158 fun `new-remote-post notification is not dismissable`() {
159 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
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()))
172 fun `sone-locked notification is created as singleton`() {
173 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
177 fun `sone-locked notification is dismissable`() {
178 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
182 fun `sone-locked notification has correct ID`() {
183 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
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()))
196 fun `sone-locked handler is created as singleton`() {
197 injector.verifySingletonInstance<SoneLockedHandler>()
201 fun `local-post notification is not dismissable`() {
202 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
206 fun `local-post notification has correct ID`() {
207 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
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()))
220 fun `local-post notification is created as singleton`() {
221 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
225 fun `local-post handler is created as singleton`() {
226 injector.verifySingletonInstance<LocalPostHandler>()
230 fun `new-version notification is created as singleton`() {
231 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
235 fun `new-version notification has correct ID`() {
236 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
240 fun `new-version notification is dismissable`() {
241 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
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"))
252 fun `new-version handler is created as singleton`() {
253 injector.verifySingletonInstance<NewVersionHandler>()
257 fun `inserting-image notification is created as singleton`() {
258 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
262 fun `inserting-image notification has correct ID`() {
263 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
267 fun `inserting-image notification is dismissable`() {
268 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
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()))
280 fun `inserting-image-failed notification is created as singleton`() {
281 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
285 fun `inserting-image-failed notification has correct ID`() {
286 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
290 fun `inserting-image-failed notification is dismissable`() {
291 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
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()))
303 fun `inserted-image notification is created as singleton`() {
304 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
308 fun `inserted-image notification has correct ID`() {
309 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
313 fun `inserted-image notification is dismissable`() {
314 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
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()))
326 fun `image insert handler is created as singleton`() {
327 injector.verifySingletonInstance<ImageInsertHandler>()
331 fun `first-start notification is created as singleton`() {
332 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
336 fun `first-start notification has correct ID`() {
337 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
341 fun `first-start notification is dismissable`() {
342 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
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"))
353 fun `first-start handler is created as singleton`() {
354 injector.verifySingletonInstance<FirstStartHandler>()
358 fun `config-not-read notification is created as singleton`() {
359 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
363 fun `config-not-read notification has correct ID `() {
364 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
368 fun `config-not-read notification is dismissable`() {
369 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
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"))
380 fun `config-not-read handler is created as singleton`() {
381 injector.verifySingletonInstance<ConfigNotReadHandler>()
385 fun `startup notification can be created`() {
386 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
390 fun `startup notification has correct ID`() {
391 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
395 fun `startup notification is dismissable`() {
396 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
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"))
407 fun `startup handler is created as singleton`() {
408 injector.verifySingletonInstance<StartupHandler>()
412 fun `web-of-trust notification is created as singleton`() {
413 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
417 fun `web-of-trust notification has correct ID`() {
418 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
422 fun `web-of-trust notification is dismissable`() {
423 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
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"))
434 fun `web-of-trust handler is created as singleton`() {
435 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
439 fun `web-of-trust reacher is created as singleton`() {
440 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
444 fun `web-of-trust reacher access the wot connector`() {
445 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
446 verify(webOfTrustConnector).ping()
450 fun `web-of-trust reschedule is created as singleton`() {
451 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
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))
462 fun `sone mention detector is created as singleton`() {
463 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())
467 fun `sone-mentioned notification is created as singleton`() {
468 injector.verifySingletonInstance<ListNotification<Post>>(named("soneMentioned"))
472 fun `sone-mentioned notification has correct ID`() {
473 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).id, equalTo("mention-notification"))
477 fun `sone-mentioned notification is not dismissable`() {
478 assertThat(injector.getInstance<ListNotification<Post>>(named("soneMentioned")).isDismissable, equalTo(false))
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()))
490 fun `sone-mentioned handler is created as singleton`() {
491 injector.verifySingletonInstance<SoneMentionedHandler>()