🔥 Remove unnecessary not-null checks
[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 }