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.impl.*
27 import net.pterodactylus.sone.main.*
28 import net.pterodactylus.sone.notify.*
29 import net.pterodactylus.sone.test.*
30 import net.pterodactylus.sone.utils.*
31 import net.pterodactylus.util.notify.*
32 import org.hamcrest.MatcherAssert.*
33 import org.hamcrest.Matchers.*
34 import org.mockito.Mockito.*
39 * Unit test for [NotificationHandlerModule].
41 class NotificationHandlerModuleTest {
43 private val core = mock<Core>()
44 private val notificationManager = NotificationManager()
45 private val loaders = TestLoaders()
46 private val injector: Injector = createInjector(
47 Core::class.isProvidedBy(core),
48 NotificationManager::class.isProvidedBy(notificationManager),
49 Loaders::class.isProvidedBy(loaders),
50 NotificationHandlerModule()
54 fun `module can create notification handler`() {
55 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
59 fun `notification handler is created as singleton`() {
60 injector.verifySingletonInstance<NotificationHandler>()
64 fun `module can create mark-post-known-during-first-start handler`() {
65 assertThat(injector.getInstance<MarkPostKnownDuringFirstStartHandler>(), notNullValue())
69 fun `mark-post-known-during-first-start handler is created with correct action`() {
70 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
71 override fun render(writer: Writer?) = Unit
73 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
74 val post = mock<Post>()
75 handler.newPostFound(NewPostFoundEvent(post))
76 verify(core).markPostKnown(post)
80 fun `module can create sone-locked-on-startup handler`() {
81 assertThat(injector.getInstance<SoneLockedOnStartupHandler>(), notNullValue())
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 can be created`() {
118 assertThat(injector.getInstance<NewSoneHandler>(), notNullValue())
122 fun `new-sone handler is created as singleton`() {
123 injector.verifySingletonInstance<NewSoneHandler>()
127 fun `new-sone notification has correct ID`() {
128 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
132 fun `new-sone notification has correct key and template`() {
133 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
134 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
135 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
136 sones.forEach(notification::add)
137 assertThat(notification.render(), equalTo(sones.toString()))
141 fun `new-sone notification is not dismissable`() {
142 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))