✨ Add handler for all notifications
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / NotificationHandlerTest.kt
1 /**
2  * Sone - NotificationHandlerTest.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.common.eventbus.*
21 import com.google.inject.*
22 import com.google.inject.Guice.*
23 import net.pterodactylus.sone.main.*
24 import net.pterodactylus.sone.test.*
25 import net.pterodactylus.util.notify.*
26 import net.pterodactylus.util.template.*
27 import net.pterodactylus.util.web.*
28 import org.hamcrest.MatcherAssert.*
29 import org.hamcrest.Matchers.*
30 import kotlin.test.*
31
32 /**
33  * Unit test for [NotificationHandler].
34  */
35 class NotificationHandlerTest {
36
37         private val eventBus = TestEventBus()
38         private val loaders = TestLoaders()
39         private val notificationManager = NotificationManager()
40         private val handler = NotificationHandler(eventBus, loaders, notificationManager)
41
42         @Test
43         fun `notification handler can be created by guice`() {
44                 val injector = createInjector(
45                                 EventBus::class.isProvidedBy(eventBus),
46                                 NotificationManager::class.isProvidedBy(notificationManager),
47                                 Loaders::class.isProvidedBy(loaders)
48                 )
49                 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
50         }
51
52         @Test
53         fun `notification handler registers handler for sone-locked event`() {
54                 handler.start()
55                 assertThat(eventBus.registeredObjects.any { it.javaClass == SoneLockedOnStartupHandler::class.java }, equalTo(true))
56         }
57
58         @Test
59         fun `notification handler loads sone-locked notification template`() {
60                 handler.start()
61                 assertThat(loaders.requestedTemplatePaths.any { it == "/templates/notify/soneLockedOnStartupNotification.html" }, equalTo(true))
62         }
63
64 }
65
66 @Suppress("UnstableApiUsage")
67 private class TestEventBus : EventBus() {
68         private val _registeredObjects = mutableListOf<Any>()
69         val registeredObjects: List<Any>
70                 get() = _registeredObjects
71
72         override fun register(`object`: Any) {
73                 super.register(`object`)
74                 _registeredObjects += `object`
75         }
76
77 }
78
79 private class TestLoaders : Loaders {
80         val requestedTemplatePaths = mutableListOf<String>()
81
82         override fun loadTemplate(path: String) =
83                         Template().also { requestedTemplatePaths += path }
84
85         override fun <REQ : Request> loadStaticPage(basePath: String, prefix: String, mimeType: String) = object : Page<REQ> {
86
87                 override fun getPath() = ""
88                 override fun isPrefixPage() = false
89                 override fun handleRequest(request: REQ, response: Response) = response
90
91         }
92
93         override fun getTemplateProvider() = TemplateProvider { _, _ -> Template() }
94
95 }