Improve names of startup-related tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / LocalPostHandlerTest.kt
1 /**
2  * Sone - LocalPostHandlerTest.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.test.createPost
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 [LocalPostHandler].
34  */
35 class LocalPostHandlerTest {
36
37         private val eventBus = EventBus()
38         private val notificationManager = NotificationManager()
39         private val notification = ListNotification<Post>("", "", Template())
40
41         init {
42                 eventBus.register(LocalPostHandler(notificationManager, notification))
43         }
44
45         @Test
46         fun `handler adds post by local sone to notification`() {
47                 eventBus.post(NewPostFoundEvent(localPost))
48                 assertThat(notification.elements, contains<Post>(localPost))
49         }
50
51         @Test
52         fun `handler does not add post by remote sone to notification`() {
53                 eventBus.post(NewPostFoundEvent(remotePost))
54                 assertThat(notification.elements, emptyIterable())
55         }
56
57         @Test
58         fun `handler does not add notification to manager for post by remote sone`() {
59                 eventBus.post(NewPostFoundEvent(remotePost))
60                 assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
61         }
62
63         @Test
64         fun `handler adds notification to manager`() {
65                 eventBus.post(NewPostFoundEvent(localPost))
66                 assertThat(notificationManager.notifications, contains<Notification>(notification))
67         }
68
69         @Test
70         fun `handler does not add notification during first start`() {
71                 notificationManager.firstStart()
72                 eventBus.post(NewPostFoundEvent(localPost))
73                 assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
74         }
75
76         @Test
77         fun `handler removes post from notification when post is removed`() {
78                 notification.add(localPost)
79                 notificationManager.addNotification(notification)
80                 eventBus.post(PostRemovedEvent(localPost))
81                 assertThat(notification.elements, emptyIterable())
82         }
83
84         @Test
85         fun `handler does not remove remote post from notification when post is removed`() {
86                 notification.add(remotePost)
87                 notificationManager.addNotification(notification)
88                 eventBus.post(PostRemovedEvent(remotePost))
89                 assertThat(notification.elements, contains(remotePost))
90         }
91
92         @Test
93         // this scenario can happen when sones are removed.
94         fun `handler removes post from notification if sone is missing`() {
95                 val post = createPost(sone = null)
96                 notification.add(post)
97                 eventBus.post(PostRemovedEvent(post))
98                 assertThat(notification.elements, emptyIterable())
99         }
100
101         @Test
102         fun `handler removes post from notification when post is marked as known`() {
103                 notification.add(localPost)
104                 notificationManager.addNotification(notification)
105                 eventBus.post(MarkPostKnownEvent(localPost))
106                 assertThat(notification.elements, emptyIterable())
107         }
108
109         @Test
110         fun `handler does not remove remote post from notification when post is marked as known`() {
111                 notification.add(remotePost)
112                 notificationManager.addNotification(notification)
113                 eventBus.post(MarkPostKnownEvent(remotePost))
114                 assertThat(notification.elements, contains(remotePost))
115         }
116
117 }
118
119 private val localSone: Sone = object : IdOnlySone("local-sone") {
120         override fun isLocal() = true
121 }
122 private val localPost: Post = object : Post.EmptyPost("local-post") {
123         override fun getSone() = localSone
124 }
125 private val remoteSone: Sone = IdOnlySone("remote-sone")
126 private val remotePost: Post = object : Post.EmptyPost("remote-post") {
127         override fun getSone() = remoteSone
128 }