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 `module can create notification handler`() {
56 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
60 fun `notification handler is created as singleton`() {
61 injector.verifySingletonInstance<NotificationHandler>()
65 fun `module can create mark-post-known-during-first-start handler`() {
66 assertThat(injector.getInstance<MarkPostKnownDuringFirstStartHandler>(), notNullValue())
70 fun `mark-post-known-during-first-start handler is created as singleton`() {
71 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
75 fun `mark-post-known-during-first-start handler is created with correct action`() {
76 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
77 override fun render(writer: Writer?) = Unit
79 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
80 val post = mock<Post>()
81 handler.newPostFound(NewPostFoundEvent(post))
82 verify(core).markPostKnown(post)
86 fun `module can create sone-locked-on-startup handler`() {
87 assertThat(injector.getInstance<SoneLockedOnStartupHandler>(), notNullValue())
91 fun `sone-locked-on-startup handler is created as singleton`() {
92 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
96 fun `module can create sone-locked-on-startup notification with correct id`() {
97 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
98 assertThat(notification.id, equalTo("sone-locked-on-startup"))
102 fun `sone-locked-on-startup notification is created as singleton`() {
103 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
107 fun `module can create sone-locked-on-startup notification with correct template and key`() {
108 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
109 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
110 val sone1 = IdOnlySone("sone1")
111 val sone2 = IdOnlySone("sone2")
112 notification.add(sone1)
113 notification.add(sone2)
114 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
118 fun `sone-locked-on-startup notification is dismissable`() {
119 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
123 fun `new-sone handler can be created`() {
124 assertThat(injector.getInstance<NewSoneHandler>(), notNullValue())
128 fun `new-sone handler is created as singleton`() {
129 injector.verifySingletonInstance<NewSoneHandler>()
133 fun `new-sone notification has correct ID`() {
134 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
138 fun `new-sone notification has correct key and template`() {
139 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
140 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
141 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
142 sones.forEach(notification::add)
143 assertThat(notification.render(), equalTo(sones.toString()))
147 fun `new-sone notification is not dismissable`() {
148 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
152 fun `new-remote-post handler can be created`() {
153 assertThat(injector.getInstance<NewRemotePostHandler>(), notNullValue())
157 fun `new-remote-post handler is created as singleton`() {
158 injector.verifySingletonInstance<NewRemotePostHandler>()
162 fun `new-remote-post notification can be created`() {
163 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")), notNullValue())
167 fun `new-remote-post notification is created as singleton`() {
168 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
172 fun `new-remote-post notification has correct ID`() {
173 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
177 fun `new-remote-post notification is not dismissable`() {
178 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
182 fun `new-remote-post notification has correct key and template`() {
183 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
184 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
185 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
186 posts.forEach(notification::add)
187 assertThat(notification.render(), equalTo(posts.toString()))
191 fun `sone-locked notification can be created`() {
192 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")), notNullValue())
196 fun `sone-locked notification is created as singleton`() {
197 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
201 fun `sone-locked notification is dismissable`() {
202 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
206 fun `sone-locked notification has correct ID`() {
207 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
211 fun `sone-locked notification has correct key and template`() {
212 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
213 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
214 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
215 sones.forEach(notification::add)
216 assertThat(notification.render(), equalTo(sones.toString()))
220 fun `sone-locked handler can be created`() {
221 assertThat(injector.getInstance<SoneLockedHandler>(), notNullValue())
225 fun `sone-locked handler is created as singleton`() {
226 injector.verifySingletonInstance<SoneLockedHandler>()