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.main.*
29 import net.pterodactylus.sone.notify.*
30 import net.pterodactylus.sone.test.*
31 import net.pterodactylus.sone.utils.*
32 import net.pterodactylus.util.notify.*
33 import org.hamcrest.MatcherAssert.*
34 import org.hamcrest.Matchers.*
35 import org.mockito.Mockito.*
37 import java.util.concurrent.*
41 * Unit test for [NotificationHandlerModule].
43 class NotificationHandlerModuleTest {
45 private val core = mock<Core>()
46 private val ticker = mock<ScheduledExecutorService>()
47 private val notificationManager = NotificationManager()
48 private val loaders = TestLoaders()
49 private val injector: Injector = createInjector(
50 Core::class.isProvidedBy(core),
51 NotificationManager::class.isProvidedBy(notificationManager),
52 Loaders::class.isProvidedBy(loaders),
53 ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
54 NotificationHandlerModule()
58 fun `notification handler is created as singleton`() {
59 injector.verifySingletonInstance<NotificationHandler>()
63 fun `mark-post-known-during-first-start handler is created as singleton`() {
64 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
68 fun `mark-post-known-during-first-start handler is created with correct action`() {
69 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
70 override fun render(writer: Writer?) = Unit
72 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
73 val post = mock<Post>()
74 handler.newPostFound(NewPostFoundEvent(post))
75 verify(core).markPostKnown(post)
79 fun `sone-locked-on-startup handler is created as singleton`() {
80 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
84 fun `module can create sone-locked-on-startup notification with correct id`() {
85 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
86 assertThat(notification.id, equalTo("sone-locked-on-startup"))
90 fun `sone-locked-on-startup notification is created as singleton`() {
91 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
95 fun `module can create sone-locked-on-startup notification with correct template and key`() {
96 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
97 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
98 val sone1 = IdOnlySone("sone1")
99 val sone2 = IdOnlySone("sone2")
100 notification.add(sone1)
101 notification.add(sone2)
102 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
106 fun `sone-locked-on-startup notification is dismissable`() {
107 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
111 fun `new-sone handler is created as singleton`() {
112 injector.verifySingletonInstance<NewSoneHandler>()
116 fun `new-sone notification has correct ID`() {
117 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
121 fun `new-sone notification has correct key and template`() {
122 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
123 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
124 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
125 sones.forEach(notification::add)
126 assertThat(notification.render(), equalTo(sones.toString()))
130 fun `new-sone notification is not dismissable`() {
131 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
135 fun `new-remote-post handler is created as singleton`() {
136 injector.verifySingletonInstance<NewRemotePostHandler>()
140 fun `new-remote-post notification is created as singleton`() {
141 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
145 fun `new-remote-post notification has correct ID`() {
146 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
150 fun `new-remote-post notification is not dismissable`() {
151 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
155 fun `new-remote-post notification has correct key and template`() {
156 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
157 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
158 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
159 posts.forEach(notification::add)
160 assertThat(notification.render(), equalTo(posts.toString()))
164 fun `sone-locked notification is created as singleton`() {
165 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
169 fun `sone-locked notification is dismissable`() {
170 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
174 fun `sone-locked notification has correct ID`() {
175 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
179 fun `sone-locked notification has correct key and template`() {
180 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
181 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
182 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
183 sones.forEach(notification::add)
184 assertThat(notification.render(), equalTo(sones.toString()))
188 fun `sone-locked handler is created as singleton`() {
189 injector.verifySingletonInstance<SoneLockedHandler>()
193 fun `local-post notification is not dismissable`() {
194 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
198 fun `local-post notification has correct ID`() {
199 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
203 fun `local-post notification has correct key and template`() {
204 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
205 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
206 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
207 posts.forEach(notification::add)
208 assertThat(notification.render(), equalTo(posts.toString()))
212 fun `local-post notification is created as singleton`() {
213 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
217 fun `local-post handler is created as singleton`() {
218 injector.verifySingletonInstance<LocalPostHandler>()
222 fun `new-version notification is created as singleton`() {
223 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
227 fun `new-version notification has correct ID`() {
228 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
232 fun `new-version notification is dismissable`() {
233 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
237 fun `new-version notification loads correct template`() {
238 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
239 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
240 assertThat(notification.render(), equalTo("1"))
244 fun `new-version handler is created as singleton`() {
245 injector.verifySingletonInstance<NewVersionHandler>()
249 fun `inserting-image notification is created as singleton`() {
250 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
254 fun `inserting-image notification has correct ID`() {
255 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
259 fun `inserting-image notification is dismissable`() {
260 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
264 fun `inserting-image notification loads correct template`() {
265 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
266 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
267 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
268 assertThat(notification.render(), equalTo(images.toString()))
272 fun `inserting-image-failed notification is created as singleton`() {
273 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
277 fun `inserting-image-failed notification has correct ID`() {
278 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
282 fun `inserting-image-failed notification is dismissable`() {
283 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
287 fun `inserting-image-failed notification loads correct template`() {
288 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
289 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
290 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
291 assertThat(notification.render(), equalTo(images.toString()))
295 fun `inserted-image notification is created as singleton`() {
296 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
300 fun `inserted-image notification has correct ID`() {
301 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
305 fun `inserted-image notification is dismissable`() {
306 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
310 fun `inserted-image notification loads correct template`() {
311 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
312 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
313 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
314 assertThat(notification.render(), equalTo(images.toString()))
318 fun `image insert handler is created as singleton`() {
319 injector.verifySingletonInstance<ImageInsertHandler>()
323 fun `first-start notification is created as singleton`() {
324 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
328 fun `first-start notification has correct ID`() {
329 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
333 fun `first-start notification is dismissable`() {
334 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
338 fun `first-start notification loads correct template`() {
339 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
340 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
341 assertThat(notification.render(), equalTo("1"))
345 fun `first-start handler is created as singleton`() {
346 injector.verifySingletonInstance<FirstStartHandler>()
350 fun `config-not-read notification is created as singleton`() {
351 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
355 fun `config-not-read notification has correct ID `() {
356 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
360 fun `config-not-read notification is dismissable`() {
361 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
365 fun `config-not-read notification loads correct template`() {
366 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
367 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
368 assertThat(notification.render(), equalTo("1"))
372 fun `config-not-read handler is created as singleton`() {
373 injector.verifySingletonInstance<ConfigNotReadHandler>()
377 fun `startup notification can be created`() {
378 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
382 fun `startup notification has correct ID`() {
383 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
387 fun `startup notification is dismissable`() {
388 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
392 fun `startup notification loads correct template`() {
393 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
394 val notification = injector.getInstance<TemplateNotification>(named("startup"))
395 assertThat(notification.render(), equalTo("1"))
399 fun `startup handler is created as singleton`() {
400 injector.verifySingletonInstance<StartupHandler>()