♻️ Extract handler for sone-locked notification
[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.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.*
36 import java.io.*
37 import kotlin.test.*
38
39 /**
40  * Unit test for [NotificationHandlerModule].
41  */
42 class NotificationHandlerModuleTest {
43
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()
52         )
53
54         @Test
55         fun `module can create notification handler`() {
56                 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
57         }
58
59         @Test
60         fun `notification handler is created as singleton`() {
61                 injector.verifySingletonInstance<NotificationHandler>()
62         }
63
64         @Test
65         fun `module can create mark-post-known-during-first-start handler`() {
66                 assertThat(injector.getInstance<MarkPostKnownDuringFirstStartHandler>(), notNullValue())
67         }
68
69         @Test
70         fun `mark-post-known-during-first-start handler is created as singleton`() {
71                 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
72         }
73
74         @Test
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
78                 })
79                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
80                 val post = mock<Post>()
81                 handler.newPostFound(NewPostFoundEvent(post))
82                 verify(core).markPostKnown(post)
83         }
84
85         @Test
86         fun `module can create sone-locked-on-startup handler`() {
87                 assertThat(injector.getInstance<SoneLockedOnStartupHandler>(), notNullValue())
88         }
89
90         @Test
91         fun `sone-locked-on-startup handler is created as singleton`() {
92                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
93         }
94
95         @Test
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"))
99         }
100
101         @Test
102         fun `sone-locked-on-startup notification is created as singleton`() {
103                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
104         }
105
106         @Test
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()))
115         }
116
117         @Test
118         fun `sone-locked-on-startup notification is dismissable`() {
119                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
120         }
121
122         @Test
123         fun `new-sone handler can be created`() {
124                 assertThat(injector.getInstance<NewSoneHandler>(), notNullValue())
125         }
126
127         @Test
128         fun `new-sone handler is created as singleton`() {
129                 injector.verifySingletonInstance<NewSoneHandler>()
130         }
131
132         @Test
133         fun `new-sone notification has correct ID`() {
134                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
135         }
136
137         @Test
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()))
144         }
145
146         @Test
147         fun `new-sone notification is not dismissable`() {
148                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
149         }
150
151         @Test
152         fun `new-remote-post handler can be created`() {
153                 assertThat(injector.getInstance<NewRemotePostHandler>(), notNullValue())
154         }
155
156         @Test
157         fun `new-remote-post handler is created as singleton`() {
158                 injector.verifySingletonInstance<NewRemotePostHandler>()
159         }
160
161         @Test
162         fun `new-remote-post notification can be created`() {
163                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")), notNullValue())
164         }
165
166         @Test
167         fun `new-remote-post notification is created as singleton`() {
168                 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
169         }
170
171         @Test
172         fun `new-remote-post notification has correct ID`() {
173                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
174         }
175
176         @Test
177         fun `new-remote-post notification is not dismissable`() {
178                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
179         }
180
181         @Test
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()))
188         }
189
190         @Test
191         fun `sone-locked notification can be created`() {
192                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")), notNullValue())
193         }
194
195         @Test
196         fun `sone-locked notification is created as singleton`() {
197                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
198         }
199
200         @Test
201         fun `sone-locked notification is dismissable`() {
202                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
203         }
204
205         @Test
206         fun `sone-locked notification has correct ID`() {
207                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
208         }
209
210         @Test
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()))
217         }
218
219         @Test
220         fun `sone-locked handler can be created`() {
221                 assertThat(injector.getInstance<SoneLockedHandler>(), notNullValue())
222         }
223
224         @Test
225         fun `sone-locked handler is created as singleton`() {
226                 injector.verifySingletonInstance<SoneLockedHandler>()
227         }
228
229 }