2 * Sone - SoneInsertHandlerTest.kt - Copyright © 2020 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 net.pterodactylus.sone.core.event.*
21 import net.pterodactylus.sone.test.*
22 import net.pterodactylus.util.notify.*
23 import net.pterodactylus.util.template.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
29 * Unit test for [SoneInsertHandler].
31 class SoneInsertHandlerTest {
33 private val localSone = createLocalSone()
34 private val notification1 = TemplateNotification(Template())
35 private val notification2 = TemplateNotification(Template())
36 private val soneInsertHandlerTester = NotificationHandlerTester {
37 SoneInsertHandler(it) { sone ->
38 if (sone == localSone) notification1 else notification2
43 fun `handler adds notification to manager when sone insert starts`() {
44 localSone.options.isSoneInsertNotificationEnabled = true
45 soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone))
46 assertThat(soneInsertHandlerTester.notifications, hasItem(notification1))
50 fun `handler sets sone status in notification when sone insert starts`() {
51 localSone.options.isSoneInsertNotificationEnabled = true
52 soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone))
53 assertThat(notification1.get("soneStatus"), equalTo<Any>("inserting"))
57 fun `handler does not add notification to manager if option is disabled`() {
58 localSone.options.isSoneInsertNotificationEnabled = false
59 soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone))
60 assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1)))
64 fun `handler adds notification to manager when sone insert finishes`() {
65 localSone.options.isSoneInsertNotificationEnabled = true
66 soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, ""))
67 assertThat(soneInsertHandlerTester.notifications, hasItem(notification1))
71 fun `handler sets sone status in notification when sone insert finishes`() {
72 localSone.options.isSoneInsertNotificationEnabled = true
73 soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, ""))
74 assertThat(notification1.get("soneStatus"), equalTo<Any>("inserted"))
78 fun `handler sets insert duration in notification when sone insert finishes`() {
79 localSone.options.isSoneInsertNotificationEnabled = true
80 soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, ""))
81 assertThat(notification1.get("insertDuration"), equalTo<Any>(123L))
85 fun `handler does not add notification for finished insert to manager if option is disabled`() {
86 localSone.options.isSoneInsertNotificationEnabled = false
87 soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, ""))
88 assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1)))
92 fun `handler adds notification to manager when sone insert aborts`() {
93 localSone.options.isSoneInsertNotificationEnabled = true
94 soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception()))
95 assertThat(soneInsertHandlerTester.notifications, hasItem(notification1))
99 fun `handler sets sone status in notification when sone insert aborts`() {
100 localSone.options.isSoneInsertNotificationEnabled = true
101 soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception()))
102 assertThat(notification1.get("soneStatus"), equalTo<Any>("insert-aborted"))
106 fun `handler does not add notification for aborted insert to manager if option is disabled`() {
107 localSone.options.isSoneInsertNotificationEnabled = false
108 soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception()))
109 assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1)))