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.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.*
32 * Unit test for [NotificationHandler].
34 class NotificationHandlerTest {
36 private val eventBus = TestEventBus()
37 private val loaders = TestLoaders()
38 private val notificationManager = NotificationManager()
39 private val handler = NotificationHandler(eventBus, loaders, notificationManager)
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)
48 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
52 fun `notification handler registers handler for sone-locked event`() {
54 assertThat(eventBus.registeredObjects, hasItem<Any>(matches { it.javaClass == SoneLockedOnStartupHandler::class.java }))
58 fun `notification handler loads sone-locked notification template`() {
60 assertThat(loaders.requestedTemplatePaths, hasItem("/templates/notify/soneLockedOnStartupNotification.html"))
64 fun `notification handler registers handler for new sone events`() {
66 assertThat(eventBus.registeredObjects, hasItem<Any>(matches { it.javaClass == NewSoneHandler::class.java }))
70 fun `notification handler loads new sone notification template`() {
72 assertThat(loaders.requestedTemplatePaths, hasItem("/templates/notify/newSoneNotification.html"))
77 @Suppress("UnstableApiUsage")
78 private class TestEventBus : EventBus() {
79 private val _registeredObjects = mutableListOf<Any>()
80 val registeredObjects: List<Any>
81 get() = _registeredObjects
83 override fun register(`object`: Any) {
84 super.register(`object`)
85 _registeredObjects += `object`
90 private class TestLoaders : Loaders {
91 val requestedTemplatePaths = mutableListOf<String>()
93 override fun loadTemplate(path: String) =
94 Template().also { requestedTemplatePaths += path }
96 override fun <REQ : Request> loadStaticPage(basePath: String, prefix: String, mimeType: String) = object : Page<REQ> {
98 override fun getPath() = ""
99 override fun isPrefixPage() = false
100 override fun handleRequest(request: REQ, response: Response) = response
104 override fun getTemplateProvider() = TemplateProvider { _, _ -> Template() }