2 * Sone - NotificationHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.notification
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.*
33 * Unit test for [NotificationHandler].
35 class NotificationHandlerTest {
37 private val eventBus = TestEventBus()
38 private val loaders = TestLoaders()
39 private val notificationManager = NotificationManager()
40 private val handler = NotificationHandler(eventBus, loaders, notificationManager)
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)
49 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
53 fun `notification handler registers handler for sone-locked event`() {
55 assertThat(eventBus.registeredObjects.any { it.javaClass == SoneLockedOnStartupHandler::class.java }, equalTo(true))
59 fun `notification handler loads sone-locked notification template`() {
61 assertThat(loaders.requestedTemplatePaths.any { it == "/templates/notify/soneLockedOnStartupNotification.html" }, equalTo(true))
66 @Suppress("UnstableApiUsage")
67 private class TestEventBus : EventBus() {
68 private val _registeredObjects = mutableListOf<Any>()
69 val registeredObjects: List<Any>
70 get() = _registeredObjects
72 override fun register(`object`: Any) {
73 super.register(`object`)
74 _registeredObjects += `object`
79 private class TestLoaders : Loaders {
80 val requestedTemplatePaths = mutableListOf<String>()
82 override fun loadTemplate(path: String) =
83 Template().also { requestedTemplatePaths += path }
85 override fun <REQ : Request> loadStaticPage(basePath: String, prefix: String, mimeType: String) = object : Page<REQ> {
87 override fun getPath() = ""
88 override fun isPrefixPage() = false
89 override fun handleRequest(request: REQ, response: Response) = response
93 override fun getTemplateProvider() = TemplateProvider { _, _ -> Template() }