3f570a9a20e02288eaac79f7ebefec627b51aae6
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / ImageInsertHandlerTest.kt
1 /**
2  * Sone - ImageInsertHandler.kt - Copyright © 2019 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 freenet.keys.FreenetURI.*
22 import net.pterodactylus.sone.core.event.*
23 import net.pterodactylus.sone.data.*
24 import net.pterodactylus.sone.data.impl.*
25 import net.pterodactylus.sone.notify.*
26 import net.pterodactylus.util.notify.*
27 import net.pterodactylus.util.template.*
28 import org.hamcrest.MatcherAssert.*
29 import org.hamcrest.Matchers.*
30 import kotlin.test.*
31
32 /**
33  * Unit test for [ImageInsertHandler].
34  */
35 @Suppress("UnstableApiUsage")
36 class ImageInsertHandlerTest {
37
38         private val eventBus = EventBus()
39         private val notificationManager = NotificationManager()
40         private val imageInsertingNotification = ListNotification<Image>("", "", Template())
41         private val imageFailedNotification = ListNotification<Image>("", "", Template())
42         private val imageInsertedNotification = ListNotification<Image>("", "", Template())
43
44         init {
45                 eventBus.register(ImageInsertHandler(notificationManager, imageInsertingNotification, imageFailedNotification, imageInsertedNotification))
46         }
47
48         @Test
49         fun `handler adds notification when image insert starts`() {
50                 eventBus.post(ImageInsertStartedEvent(image))
51                 assertThat(notificationManager.notifications, contains<Notification>(imageInsertingNotification))
52         }
53
54         @Test
55         fun `handler adds image to notification when image insert starts`() {
56                 eventBus.post(ImageInsertStartedEvent(image))
57                 assertThat(imageInsertingNotification.elements, contains(image))
58         }
59
60         @Test
61         fun `handler removes image from inserting notification when insert is aborted`() {
62                 eventBus.post(ImageInsertStartedEvent(image))
63                 eventBus.post(ImageInsertAbortedEvent(image))
64                 assertThat(imageInsertingNotification.elements, emptyIterable())
65         }
66
67         @Test
68         fun `handler removes image from inserting notification when insert fails`() {
69                 eventBus.post(ImageInsertStartedEvent(image))
70                 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
71                 assertThat(imageInsertingNotification.elements, emptyIterable())
72         }
73
74         @Test
75         fun `handler adds image to insert-failed notification when insert fails`() {
76                 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
77                 assertThat(imageFailedNotification.elements, contains(image))
78         }
79
80         @Test
81         fun `handler adds insert-failed notification to manager when insert fails`() {
82                 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
83                 assertThat(notificationManager.notifications, contains<Notification>(imageFailedNotification))
84         }
85
86         @Test
87         fun `handler removes image from inserting notification when insert succeeds`() {
88                 eventBus.post(ImageInsertStartedEvent(image))
89                 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
90                 assertThat(imageInsertingNotification.elements, emptyIterable())
91         }
92
93         @Test
94         fun `handler adds image to inserted notification when insert succeeds`() {
95                 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
96                 assertThat(imageInsertedNotification.elements, contains(image))
97         }
98
99         @Test
100         fun `handler adds inserted notification to manager when insert succeeds`() {
101                 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
102                 assertThat(notificationManager.notifications, contains<Notification>(imageInsertedNotification))
103         }
104
105 }
106
107 private val image: Image = ImageImpl()