♻️ Use handler for Sone insertion notifications
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / SoneInsertHandlerTest.kt
1 /**
2  * Sone - SoneInsertHandlerTest.kt - Copyright © 2020 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 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.*
26 import kotlin.test.*
27
28 /**
29  * Unit test for [SoneInsertHandler].
30  */
31 class SoneInsertHandlerTest {
32
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
39                 }
40         }
41
42         @Test
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))
47         }
48
49         @Test
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"))
54         }
55
56         @Test
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)))
61         }
62
63         @Test
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))
68         }
69
70         @Test
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"))
75         }
76
77         @Test
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))
82         }
83
84         @Test
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)))
89         }
90
91         @Test
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))
96         }
97
98         @Test
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"))
103         }
104
105         @Test
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)))
110         }
111
112 }