a06123071649f7341cc0090af1cbbbfe7b78c747
[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 `notification handler is created as singleton`() {
56                 injector.verifySingletonInstance<NotificationHandler>()
57         }
58
59         @Test
60         fun `mark-post-known-during-first-start handler is created as singleton`() {
61                 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
62         }
63
64         @Test
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
68                 })
69                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
70                 val post = mock<Post>()
71                 handler.newPostFound(NewPostFoundEvent(post))
72                 verify(core).markPostKnown(post)
73         }
74
75         @Test
76         fun `sone-locked-on-startup handler is created as singleton`() {
77                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
78         }
79
80         @Test
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"))
84         }
85
86         @Test
87         fun `sone-locked-on-startup notification is created as singleton`() {
88                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
89         }
90
91         @Test
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()))
100         }
101
102         @Test
103         fun `sone-locked-on-startup notification is dismissable`() {
104                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
105         }
106
107         @Test
108         fun `new-sone handler is created as singleton`() {
109                 injector.verifySingletonInstance<NewSoneHandler>()
110         }
111
112         @Test
113         fun `new-sone notification has correct ID`() {
114                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
115         }
116
117         @Test
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()))
124         }
125
126         @Test
127         fun `new-sone notification is not dismissable`() {
128                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
129         }
130
131         @Test
132         fun `new-remote-post handler is created as singleton`() {
133                 injector.verifySingletonInstance<NewRemotePostHandler>()
134         }
135
136         @Test
137         fun `new-remote-post notification is created as singleton`() {
138                 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
139         }
140
141         @Test
142         fun `new-remote-post notification has correct ID`() {
143                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
144         }
145
146         @Test
147         fun `new-remote-post notification is not dismissable`() {
148                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
149         }
150
151         @Test
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()))
158         }
159
160         @Test
161         fun `sone-locked notification is created as singleton`() {
162                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
163         }
164
165         @Test
166         fun `sone-locked notification is dismissable`() {
167                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
168         }
169
170         @Test
171         fun `sone-locked notification has correct ID`() {
172                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
173         }
174
175         @Test
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()))
182         }
183
184         @Test
185         fun `sone-locked handler is created as singleton`() {
186                 injector.verifySingletonInstance<SoneLockedHandler>()
187         }
188
189         @Test
190         fun `local-post notification is not dismissable`() {
191                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
192         }
193
194         @Test
195         fun `local-post notification has correct ID`() {
196                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
197         }
198
199         @Test
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()))
206         }
207
208         @Test
209         fun `local-post notification is created as singleton`() {
210                 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
211         }
212
213         @Test
214         fun `local-post handler is created as singleton`() {
215                 injector.verifySingletonInstance<LocalPostHandler>()
216         }
217
218         @Test
219         fun `new-version notification is created as singleton`() {
220                 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
221         }
222
223         @Test
224         fun `new-version notification has correct ID`() {
225                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
226         }
227
228         @Test
229         fun `new-version notification is dismissable`() {
230                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
231         }
232
233         @Test
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"))
238         }
239
240         @Test
241         fun `new-version handler is created as singleton`() {
242                 injector.verifySingletonInstance<NewVersionHandler>()
243         }
244
245         @Test
246         fun `inserting-image notification is created as singleton`() {
247                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
248         }
249
250         @Test
251         fun `inserting-image notification has correct ID`() {
252                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
253         }
254
255         @Test
256         fun `inserting-image notification is dismissable`() {
257                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
258         }
259
260         @Test
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()))
266         }
267
268         @Test
269         fun `inserting-image-failed notification is created as singleton`() {
270                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
271         }
272
273         @Test
274         fun `inserting-image-failed notification has correct ID`() {
275                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
276         }
277
278         @Test
279         fun `inserting-image-failed notification is dismissable`() {
280                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
281         }
282
283         @Test
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()))
289         }
290
291         @Test
292         fun `inserted-image notification is created as singleton`() {
293                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
294         }
295
296         @Test
297         fun `inserted-image notification has correct ID`() {
298                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
299         }
300
301         @Test
302         fun `inserted-image notification is dismissable`() {
303                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
304         }
305
306         @Test
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()))
312         }
313
314         @Test
315         fun `image insert handler is created as singleton`() {
316                 injector.verifySingletonInstance<ImageInsertHandler>()
317         }
318
319         @Test
320         fun `first-start notification is created as singleton`() {
321                 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
322         }
323
324         @Test
325         fun `first-start notification has correct ID`() {
326                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
327         }
328
329         @Test
330         fun `first-start notification is dismissable`() {
331                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
332         }
333
334         @Test
335         fun `first-start notification loads correct template`() {
336                 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
337                 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
338                 assertThat(notification.render(), equalTo("1"))
339         }
340
341         @Test
342         fun `first-start handler is created as singleton`() {
343                 injector.verifySingletonInstance<FirstStartHandler>()
344         }
345
346         @Test
347         fun `config-not-read notification is created as singleton`() {
348                 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
349         }
350
351         @Test
352         fun `config-not-read notification has correct ID `() {
353                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
354         }
355
356         @Test
357         fun `config-not-read notification is dismissable`() {
358                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
359         }
360
361         @Test
362         fun `config-not-read notification loads correct template`() {
363                 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
364                 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
365                 assertThat(notification.render(), equalTo("1"))
366         }
367
368         @Test
369         fun `config-not-read handler is created as singleton`() {
370                 injector.verifySingletonInstance<ConfigNotReadHandler>()
371         }
372
373 }