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.freenet.wot.*
29 import net.pterodactylus.sone.main.*
30 import net.pterodactylus.sone.notify.*
31 import net.pterodactylus.sone.test.*
32 import net.pterodactylus.sone.text.*
33 import net.pterodactylus.sone.utils.*
34 import net.pterodactylus.util.notify.*
35 import org.hamcrest.MatcherAssert.*
36 import org.hamcrest.Matchers
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 NotificationHandlerModule()
66 fun `notification handler is created as singleton`() {
67 injector.verifySingletonInstance<NotificationHandler>()
71 fun `mark-post-known-during-first-start handler is created as singleton`() {
72 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
76 fun `mark-post-known-during-first-start handler is created with correct action`() {
77 notificationManager.firstStart()
78 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
79 val post = mock<Post>()
80 handler.newPostFound(NewPostFoundEvent(post))
81 verify(core).markPostKnown(post)
85 fun `sone-locked-on-startup handler is created as singleton`() {
86 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
90 fun `module can create sone-locked-on-startup notification with correct id`() {
91 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
92 assertThat(notification.id, equalTo("sone-locked-on-startup"))
96 fun `sone-locked-on-startup notification is created as singleton`() {
97 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
101 fun `module can create sone-locked-on-startup notification with correct template and key`() {
102 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
103 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
104 val sone1 = IdOnlySone("sone1")
105 val sone2 = IdOnlySone("sone2")
106 notification.add(sone1)
107 notification.add(sone2)
108 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
112 fun `sone-locked-on-startup notification is dismissable`() {
113 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
117 fun `new-sone handler is created as singleton`() {
118 injector.verifySingletonInstance<NewSoneHandler>()
122 fun `new-sone notification has correct ID`() {
123 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
127 fun `new-sone notification has correct key and template`() {
128 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
129 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
130 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
131 sones.forEach(notification::add)
132 assertThat(notification.render(), equalTo(sones.toString()))
136 fun `new-sone notification is not dismissable`() {
137 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
141 fun `new-remote-post handler is created as singleton`() {
142 injector.verifySingletonInstance<NewRemotePostHandler>()
146 fun `new-remote-post notification is created as singleton`() {
147 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
151 fun `new-remote-post notification has correct ID`() {
152 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
156 fun `new-remote-post notification is not dismissable`() {
157 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
161 fun `new-remote-post notification has correct key and template`() {
162 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
163 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
164 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
165 posts.forEach(notification::add)
166 assertThat(notification.render(), equalTo(posts.toString()))
170 fun `sone-locked notification is created as singleton`() {
171 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
175 fun `sone-locked notification is dismissable`() {
176 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
180 fun `sone-locked notification has correct ID`() {
181 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
185 fun `sone-locked notification has correct key and template`() {
186 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
187 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
188 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
189 sones.forEach(notification::add)
190 assertThat(notification.render(), equalTo(sones.toString()))
194 fun `sone-locked handler is created as singleton`() {
195 injector.verifySingletonInstance<SoneLockedHandler>()
199 fun `local-post notification is not dismissable`() {
200 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
204 fun `local-post notification has correct ID`() {
205 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
209 fun `local-post notification has correct key and template`() {
210 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
211 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
212 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
213 posts.forEach(notification::add)
214 assertThat(notification.render(), equalTo(posts.toString()))
218 fun `local-post notification is created as singleton`() {
219 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
223 fun `local-post handler is created as singleton`() {
224 injector.verifySingletonInstance<LocalPostHandler>()
228 fun `new-version notification is created as singleton`() {
229 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
233 fun `new-version notification has correct ID`() {
234 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
238 fun `new-version notification is dismissable`() {
239 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
243 fun `new-version notification loads correct template`() {
244 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
245 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
246 assertThat(notification.render(), equalTo("1"))
250 fun `new-version handler is created as singleton`() {
251 injector.verifySingletonInstance<NewVersionHandler>()
255 fun `inserting-image notification is created as singleton`() {
256 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
260 fun `inserting-image notification has correct ID`() {
261 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
265 fun `inserting-image notification is dismissable`() {
266 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
270 fun `inserting-image notification loads correct template`() {
271 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
272 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
273 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
274 assertThat(notification.render(), equalTo(images.toString()))
278 fun `inserting-image-failed notification is created as singleton`() {
279 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
283 fun `inserting-image-failed notification has correct ID`() {
284 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
288 fun `inserting-image-failed notification is dismissable`() {
289 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
293 fun `inserting-image-failed notification loads correct template`() {
294 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
295 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
296 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
297 assertThat(notification.render(), equalTo(images.toString()))
301 fun `inserted-image notification is created as singleton`() {
302 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
306 fun `inserted-image notification has correct ID`() {
307 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
311 fun `inserted-image notification is dismissable`() {
312 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
316 fun `inserted-image notification loads correct template`() {
317 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
318 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
319 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
320 assertThat(notification.render(), equalTo(images.toString()))
324 fun `image insert handler is created as singleton`() {
325 injector.verifySingletonInstance<ImageInsertHandler>()
329 fun `first-start notification is created as singleton`() {
330 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
334 fun `first-start notification has correct ID`() {
335 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
339 fun `first-start notification is dismissable`() {
340 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
344 fun `first-start notification loads correct template`() {
345 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
346 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
347 assertThat(notification.render(), equalTo("1"))
351 fun `first-start handler is created as singleton`() {
352 injector.verifySingletonInstance<FirstStartHandler>()
356 fun `config-not-read notification is created as singleton`() {
357 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
361 fun `config-not-read notification has correct ID `() {
362 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
366 fun `config-not-read notification is dismissable`() {
367 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
371 fun `config-not-read notification loads correct template`() {
372 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
373 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
374 assertThat(notification.render(), equalTo("1"))
378 fun `config-not-read handler is created as singleton`() {
379 injector.verifySingletonInstance<ConfigNotReadHandler>()
383 fun `startup notification can be created`() {
384 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
388 fun `startup notification has correct ID`() {
389 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
393 fun `startup notification is dismissable`() {
394 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
398 fun `startup notification loads correct template`() {
399 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
400 val notification = injector.getInstance<TemplateNotification>(named("startup"))
401 assertThat(notification.render(), equalTo("1"))
405 fun `startup handler is created as singleton`() {
406 injector.verifySingletonInstance<StartupHandler>()
410 fun `web-of-trust notification is created as singleton`() {
411 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
415 fun `web-of-trust notification has correct ID`() {
416 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
420 fun `web-of-trust notification is dismissable`() {
421 assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
425 fun `web-of-trust notification loads correct template`() {
426 loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
427 val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
428 assertThat(notification.render(), equalTo("1"))
432 fun `web-of-trust handler is created as singleton`() {
433 injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
437 fun `web-of-trust reacher is created as singleton`() {
438 injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
442 fun `web-of-trust reacher access the wot connector`() {
443 injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
444 verify(webOfTrustConnector).ping()
448 fun `web-of-trust reschedule is created as singleton`() {
449 injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
453 fun `web-of-trust reschedule schedules at the correct delay`() {
454 val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
455 injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
456 verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
460 fun `sone mention detector is created as singleton`() {
461 assertThat(injector.getInstance<SoneMentionDetector>(), notNullValue())