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