🚧 Use ticker from Sone module for notifications
[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 java.util.concurrent.*
38 import kotlin.test.*
39
40 /**
41  * Unit test for [NotificationHandlerModule].
42  */
43 class NotificationHandlerModuleTest {
44
45         private val core = mock<Core>()
46         private val ticker = mock<ScheduledExecutorService>()
47         private val notificationManager = NotificationManager()
48         private val loaders = TestLoaders()
49         private val injector: Injector = createInjector(
50                         Core::class.isProvidedBy(core),
51                         NotificationManager::class.isProvidedBy(notificationManager),
52                         Loaders::class.isProvidedBy(loaders),
53                         ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
54                         NotificationHandlerModule()
55         )
56
57         @Test
58         fun `notification handler is created as singleton`() {
59                 injector.verifySingletonInstance<NotificationHandler>()
60         }
61
62         @Test
63         fun `mark-post-known-during-first-start handler is created as singleton`() {
64                 injector.verifySingletonInstance<MarkPostKnownDuringFirstStartHandler>()
65         }
66
67         @Test
68         fun `mark-post-known-during-first-start handler is created with correct action`() {
69                 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
70                         override fun render(writer: Writer?) = Unit
71                 })
72                 val handler = injector.getInstance<MarkPostKnownDuringFirstStartHandler>()
73                 val post = mock<Post>()
74                 handler.newPostFound(NewPostFoundEvent(post))
75                 verify(core).markPostKnown(post)
76         }
77
78         @Test
79         fun `sone-locked-on-startup handler is created as singleton`() {
80                 injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
81         }
82
83         @Test
84         fun `module can create sone-locked-on-startup notification with correct id`() {
85                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
86                 assertThat(notification.id, equalTo("sone-locked-on-startup"))
87         }
88
89         @Test
90         fun `sone-locked-on-startup notification is created as singleton`() {
91                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
92         }
93
94         @Test
95         fun `module can create sone-locked-on-startup notification with correct template and key`() {
96                 loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
97                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup"))
98                 val sone1 = IdOnlySone("sone1")
99                 val sone2 = IdOnlySone("sone2")
100                 notification.add(sone1)
101                 notification.add(sone2)
102                 assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
103         }
104
105         @Test
106         fun `sone-locked-on-startup notification is dismissable`() {
107                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
108         }
109
110         @Test
111         fun `new-sone handler is created as singleton`() {
112                 injector.verifySingletonInstance<NewSoneHandler>()
113         }
114
115         @Test
116         fun `new-sone notification has correct ID`() {
117                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).id, equalTo("new-sone-notification"))
118         }
119
120         @Test
121         fun `new-sone notification has correct key and template`() {
122                 loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
123                 val notification = injector.getInstance<ListNotification<Sone>>(named("newSone"))
124                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
125                 sones.forEach(notification::add)
126                 assertThat(notification.render(), equalTo(sones.toString()))
127         }
128
129         @Test
130         fun `new-sone notification is not dismissable`() {
131                 assertThat(injector.getInstance<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
132         }
133
134         @Test
135         fun `new-remote-post handler is created as singleton`() {
136                 injector.verifySingletonInstance<NewRemotePostHandler>()
137         }
138
139         @Test
140         fun `new-remote-post notification is created as singleton`() {
141                 injector.verifySingletonInstance<ListNotification<Post>>(named("newRemotePost"))
142         }
143
144         @Test
145         fun `new-remote-post notification has correct ID`() {
146                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).id, equalTo("new-post-notification"))
147         }
148
149         @Test
150         fun `new-remote-post notification is not dismissable`() {
151                 assertThat(injector.getInstance<ListNotification<Post>>(named("newRemotePost")).isDismissable, equalTo(false))
152         }
153
154         @Test
155         fun `new-remote-post notification has correct key and template`() {
156                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
157                 val notification = injector.getInstance<ListNotification<Post>>(named("newRemotePost"))
158                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
159                 posts.forEach(notification::add)
160                 assertThat(notification.render(), equalTo(posts.toString()))
161         }
162
163         @Test
164         fun `sone-locked notification is created as singleton`() {
165                 injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
166         }
167
168         @Test
169         fun `sone-locked notification is dismissable`() {
170                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).isDismissable, equalTo(true))
171         }
172
173         @Test
174         fun `sone-locked notification has correct ID`() {
175                 assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLocked")).id, equalTo("sones-locked-notification"))
176         }
177
178         @Test
179         fun `sone-locked notification has correct key and template`() {
180                 loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
181                 val notification = injector.getInstance<ListNotification<Sone>>(named("soneLocked"))
182                 val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
183                 sones.forEach(notification::add)
184                 assertThat(notification.render(), equalTo(sones.toString()))
185         }
186
187         @Test
188         fun `sone-locked handler is created as singleton`() {
189                 injector.verifySingletonInstance<SoneLockedHandler>()
190         }
191
192         @Test
193         fun `local-post notification is not dismissable`() {
194                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
195         }
196
197         @Test
198         fun `local-post notification has correct ID`() {
199                 assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
200         }
201
202         @Test
203         fun `local-post notification has correct key and template`() {
204                 loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
205                 val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
206                 val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
207                 posts.forEach(notification::add)
208                 assertThat(notification.render(), equalTo(posts.toString()))
209         }
210
211         @Test
212         fun `local-post notification is created as singleton`() {
213                 injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
214         }
215
216         @Test
217         fun `local-post handler is created as singleton`() {
218                 injector.verifySingletonInstance<LocalPostHandler>()
219         }
220
221         @Test
222         fun `new-version notification is created as singleton`() {
223                 injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
224         }
225
226         @Test
227         fun `new-version notification has correct ID`() {
228                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
229         }
230
231         @Test
232         fun `new-version notification is dismissable`() {
233                 assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
234         }
235
236         @Test
237         fun `new-version notification loads correct template`() {
238                 loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
239                 val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
240                 assertThat(notification.render(), equalTo("1"))
241         }
242
243         @Test
244         fun `new-version handler is created as singleton`() {
245                 injector.verifySingletonInstance<NewVersionHandler>()
246         }
247
248         @Test
249         fun `inserting-image notification is created as singleton`() {
250                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserting"))
251         }
252
253         @Test
254         fun `inserting-image notification has correct ID`() {
255                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).id, equalTo("inserting-images-notification"))
256         }
257
258         @Test
259         fun `inserting-image notification is dismissable`() {
260                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserting")).isDismissable, equalTo(true))
261         }
262
263         @Test
264         fun `inserting-image notification loads correct template`() {
265                 loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
266                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserting"))
267                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
268                 assertThat(notification.render(), equalTo(images.toString()))
269         }
270
271         @Test
272         fun `inserting-image-failed notification is created as singleton`() {
273                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageFailed"))
274         }
275
276         @Test
277         fun `inserting-image-failed notification has correct ID`() {
278                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
279         }
280
281         @Test
282         fun `inserting-image-failed notification is dismissable`() {
283                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageFailed")).isDismissable, equalTo(true))
284         }
285
286         @Test
287         fun `inserting-image-failed notification loads correct template`() {
288                 loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
289                 val notification = injector.getInstance<ListNotification<Image>>(named("imageFailed"))
290                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
291                 assertThat(notification.render(), equalTo(images.toString()))
292         }
293
294         @Test
295         fun `inserted-image notification is created as singleton`() {
296                 injector.verifySingletonInstance<ListNotification<Image>>(named("imageInserted"))
297         }
298
299         @Test
300         fun `inserted-image notification has correct ID`() {
301                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).id, equalTo("inserted-images-notification"))
302         }
303
304         @Test
305         fun `inserted-image notification is dismissable`() {
306                 assertThat(injector.getInstance<ListNotification<Image>>(named("imageInserted")).isDismissable, equalTo(true))
307         }
308
309         @Test
310         fun `inserted-image notification loads correct template`() {
311                 loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
312                 val notification = injector.getInstance<ListNotification<Image>>(named("imageInserted"))
313                 val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
314                 assertThat(notification.render(), equalTo(images.toString()))
315         }
316
317         @Test
318         fun `image insert handler is created as singleton`() {
319                 injector.verifySingletonInstance<ImageInsertHandler>()
320         }
321
322         @Test
323         fun `first-start notification is created as singleton`() {
324                 injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
325         }
326
327         @Test
328         fun `first-start notification has correct ID`() {
329                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
330         }
331
332         @Test
333         fun `first-start notification is dismissable`() {
334                 assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
335         }
336
337         @Test
338         fun `first-start notification loads correct template`() {
339                 loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
340                 val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
341                 assertThat(notification.render(), equalTo("1"))
342         }
343
344         @Test
345         fun `first-start handler is created as singleton`() {
346                 injector.verifySingletonInstance<FirstStartHandler>()
347         }
348
349         @Test
350         fun `config-not-read notification is created as singleton`() {
351                 injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
352         }
353
354         @Test
355         fun `config-not-read notification has correct ID `() {
356                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
357         }
358
359         @Test
360         fun `config-not-read notification is dismissable`() {
361                 assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
362         }
363
364         @Test
365         fun `config-not-read notification loads correct template`() {
366                 loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
367                 val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
368                 assertThat(notification.render(), equalTo("1"))
369         }
370
371         @Test
372         fun `config-not-read handler is created as singleton`() {
373                 injector.verifySingletonInstance<ConfigNotReadHandler>()
374         }
375
376         @Test
377         fun `startup notification can be created`() {
378                 injector.verifySingletonInstance<TemplateNotification>(named("startup"))
379         }
380
381         @Test
382         fun `startup notification has correct ID`() {
383                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
384         }
385
386         @Test
387         fun `startup notification is dismissable`() {
388                 assertThat(injector.getInstance<TemplateNotification>(named("startup")).isDismissable, equalTo(true))
389         }
390
391         @Test
392         fun `startup notification loads correct template`() {
393                 loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
394                 val notification = injector.getInstance<TemplateNotification>(named("startup"))
395                 assertThat(notification.render(), equalTo("1"))
396         }
397
398         @Test
399         fun `startup handler is created as singleton`() {
400                 injector.verifySingletonInstance<StartupHandler>()
401         }
402
403 }