🎨 Move new-sone template creation into module
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / NotificationHandlerModuleTest.kt
1 /**
2  * Sone - NotificationHandlerModuleTest.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.notification
19
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.*
35 import java.io.*
36 import kotlin.test.*
37
38 /**
39  * Unit test for [NotificationHandlerModule].
40  */
41 class NotificationHandlerModuleTest {
42
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()
51         )
52
53         @Test
54         fun `module can create notification handler`() {
55                 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
56         }
57
58         @Test
59         fun `notification handler is created as singleton`() {
60                 injector.verifySingletonInstance<NotificationHandler>()
61         }
62
63         @Test
64         fun `module can create mark-post-known-during-first-start handler`() {
65                 assertThat(injector.getInstance<MarkPostKnownDuringFirstStartHandler>(), notNullValue())
66         }
67
68         @Test
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
72                 })
73                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
74                 val post = mock<Post>()
75                 handler.newPostFound(NewPostFoundEvent(post))
76                 verify(core).markPostKnown(post)
77         }
78
79         @Test
80         fun `module can create sone-locked-on-startup handler`() {
81                 assertThat(injector.getInstance<SoneLockedOnStartupHandler>(), notNullValue())
82         }
83
84         @Test
85         fun `sone-locked-on-startup handler is created as singleton`() {
86                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
87         }
88
89         @Test
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"))
93         }
94
95         @Test
96         fun `sone-locked-on-startup notification is created as singleton`() {
97                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
98         }
99
100         @Test
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()))
109         }
110
111         @Test
112         fun `sone-locked-on-startup notification is dismissable`() {
113                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
114         }
115
116         @Test
117         fun `new-sone handler can be created`() {
118                 assertThat(injector.getInstance<NewSoneHandler>(), notNullValue())
119         }
120
121         @Test
122         fun `new-sone handler is created as singleton`() {
123                 injector.verifySingletonInstance<NewSoneHandler>()
124         }
125
126         @Test
127         fun `new-sone notification has correct ID`() {
128                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
129         }
130
131         @Test
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()))
138         }
139
140         @Test
141         fun `new-sone notification is not dismissable`() {
142                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
143         }
144
145 }