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.*
40 * Unit test for [NotificationHandlerModule].
42 class NotificationHandlerModuleTest {
44 private val core = mock<Core>()
45 private val notificationManager = NotificationManager()
46 private val loaders = TestLoaders()
47 private val injector: Injector = createInjector(
48 Core::class.isProvidedBy(core),
49 NotificationManager::class.isProvidedBy(notificationManager),
50 Loaders::class.isProvidedBy(loaders),
51 NotificationHandlerModule()
55 fun `notification handler is created as singleton`() {
56 injector.verifySingletonInstance<NotificationHandler>()
60 fun `mark-post-known-during-first-start handler is created as singleton`() {
61 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
65 fun `mark-post-known-during-first-start handler is created with correct action`() {
66 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
67 override fun render(writer: Writer?) = Unit
69 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
70 val post = mock<Post>()
71 handler.newPostFound(NewPostFoundEvent(post))
72 verify(core).markPostKnown(post)
76 fun `sone-locked-on-startup handler is created as singleton`() {
77 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
81 fun `module can create sone-locked-on-startup notification with correct id`() {
82 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
83 assertThat(notification.id, equalTo("sone-locked-on-startup"))
87 fun `sone-locked-on-startup notification is created as singleton`() {
88 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
92 fun `module can create sone-locked-on-startup notification with correct template and key`() {
93 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
94 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
95 val sone1 = IdOnlySone("sone1")
96 val sone2 = IdOnlySone("sone2")
97 notification.add(sone1)
98 notification.add(sone2)
99 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
103 fun `sone-locked-on-startup notification is dismissable`() {
104 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
108 fun `new-sone handler is created as singleton`() {
109 injector.verifySingletonInstance<NewSoneHandler>()
113 fun `new-sone notification has correct ID`() {
114 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
118 fun `new-sone notification has correct key and template`() {
119 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
120 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
121 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
122 sones.forEach(notification::add)
123 assertThat(notification.render(), equalTo(sones.toString()))
127 fun `new-sone notification is not dismissable`() {
128 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
132 fun `new-remote-post handler is created as singleton`() {
133 injector.verifySingletonInstance<NewRemotePostHandler>()
137 fun `new-remote-post notification is created as singleton`() {
138 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
142 fun `new-remote-post notification has correct ID`() {
143 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
147 fun `new-remote-post notification is not dismissable`() {
148 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
152 fun `new-remote-post notification has correct key and template`() {
153 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
154 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
155 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
156 posts.forEach(notification::add)
157 assertThat(notification.render(), equalTo(posts.toString()))
161 fun `sone-locked notification is created as singleton`() {
162 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
166 fun `sone-locked notification is dismissable`() {
167 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
171 fun `sone-locked notification has correct ID`() {
172 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
176 fun `sone-locked notification has correct key and template`() {
177 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
178 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
179 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
180 sones.forEach(notification::add)
181 assertThat(notification.render(), equalTo(sones.toString()))
185 fun `sone-locked handler is created as singleton`() {
186 injector.verifySingletonInstance<SoneLockedHandler>()
190 fun `local-post notification is not dismissable`() {
191 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
195 fun `local-post notification has correct ID`() {
196 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
200 fun `local-post notification has correct key and template`() {
201 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
202 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
203 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
204 posts.forEach(notification::add)
205 assertThat(notification.render(), equalTo(posts.toString()))
209 fun `local-post notification is created as singleton`() {
210 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
214 fun `local-post handler is created as singleton`() {
215 injector.verifySingletonInstance<LocalPostHandler>()
219 fun `new-version notification is created as singleton`() {
220 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
224 fun `new-version notification has correct ID`() {
225 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
229 fun `new-version notification is dismissable`() {
230 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
234 fun `new-version notification loads correct template`() {
235 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
236 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
237 assertThat(notification.render(), equalTo("1"))
241 fun `new-version handler is created as singleton`() {
242 injector.verifySingletonInstance<NewVersionHandler>()
246 fun `inserting-image notification is created as singleton`() {
247 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
251 fun `inserting-image notification has correct ID`() {
252 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
256 fun `inserting-image notification is dismissable`() {
257 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
261 fun `inserting-image notification loads correct template`() {
262 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
263 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
264 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
265 assertThat(notification.render(), equalTo(images.toString()))
269 fun `inserting-image-failed notification is created as singleton`() {
270 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
274 fun `inserting-image-failed notification has correct ID`() {
275 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
279 fun `inserting-image-failed notification is dismissable`() {
280 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
284 fun `inserting-image-failed notification loads correct template`() {
285 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
286 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
287 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
288 assertThat(notification.render(), equalTo(images.toString()))
292 fun `inserted-image notification is created as singleton`() {
293 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
297 fun `inserted-image notification has correct ID`() {
298 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
302 fun `inserted-image notification is dismissable`() {
303 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
307 fun `inserted-image notification loads correct template`() {
308 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
309 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
310 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
311 assertThat(notification.render(), equalTo(images.toString()))
315 fun `image insert handler is created as singleton`() {
316 injector.verifySingletonInstance<ImageInsertHandler>()