Improve names of startup-related tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / SoneLockedHandlerTest.kt
1 /**
2  * Sone - SoneLockedHandlerTest.kt - Copyright © 2019–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 com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.data.*
23 import net.pterodactylus.sone.data.impl.*
24 import net.pterodactylus.sone.notify.*
25 import net.pterodactylus.sone.utils.*
26 import net.pterodactylus.util.notify.*
27 import net.pterodactylus.util.template.*
28 import org.hamcrest.MatcherAssert.*
29 import org.hamcrest.Matchers.*
30 import java.util.concurrent.*
31 import kotlin.test.*
32
33 /**
34  * Unit test for [SoneLockedHandler].
35  */
36 @Suppress("UnstableApiUsage")
37 class SoneLockedHandlerTest {
38
39         private val eventBus = EventBus()
40         private val notificationManager = NotificationManager()
41         private val notification = ListNotification<Sone>("", "", Template())
42         private val executor = TestScheduledThreadPoolExecutor()
43
44         init {
45                 SoneLockedHandler(notificationManager, notification, executor).also(eventBus::register)
46         }
47
48         @AfterTest
49         fun shutdownExecutor() = executor.shutdown()
50
51         @Test
52         fun `notification is not added before the command is run`() {
53                 eventBus.post(SoneLockedEvent(sone))
54                 assertThat(notificationManager.notifications, emptyIterable())
55         }
56
57         @Test
58         fun `sone is added to notification immediately`() {
59                 eventBus.post(SoneLockedEvent(sone))
60                 assertThat(notification.elements, contains(sone))
61         }
62
63         @Test
64         fun `notification is added to notification manager from command`() {
65                 eventBus.post(SoneLockedEvent(sone))
66                 executor.scheduleds.single().command()
67                 assertThat(notificationManager.notifications, contains<Any>(notification))
68         }
69
70         @Test
71         fun `command is registered with a delay of five minutes`() {
72                 eventBus.post(SoneLockedEvent(sone))
73                 with(executor.scheduleds.single()) {
74                         assertThat(timeUnit.toNanos(delay), equalTo(TimeUnit.MINUTES.toNanos(5)))
75                 }
76         }
77
78         @Test
79         fun `unlocking sone after locking will cancel the future`() {
80                 eventBus.post(SoneLockedEvent(sone))
81                 eventBus.post(SoneUnlockedEvent(sone))
82                 assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
83         }
84
85         @Test
86         fun `unlocking sone after locking will remove the sone from the notification`() {
87                 eventBus.post(SoneLockedEvent(sone))
88                 eventBus.post(SoneUnlockedEvent(sone))
89                 assertThat(notification.elements, emptyIterable())
90         }
91
92         @Test
93         fun `unlocking sone after showing the notification will remove the sone from the notification`() {
94                 eventBus.post(SoneLockedEvent(sone))
95                 executor.scheduleds.single().command()
96                 eventBus.post(SoneUnlockedEvent(sone))
97                 assertThat(notification.elements, emptyIterable())
98         }
99
100         @Test
101         fun `locking two sones will cancel the first command`() {
102                 eventBus.post(SoneLockedEvent(sone))
103                 eventBus.post(SoneLockedEvent(sone))
104                 assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
105         }
106
107         @Test
108         fun `locking two sones will schedule a second command`() {
109                 eventBus.post(SoneLockedEvent(sone))
110                 eventBus.post(SoneLockedEvent(sone))
111                 assertThat(executor.scheduleds[1], notNullValue())
112         }
113
114 }
115
116 private val sone: Sone = IdOnlySone("sone")