🐛 Don’t show notification for remote posts
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / LocalPostHandlerTest.kt
1 /**
2  * Sone - NewLocalPostHandlerTest.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 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.util.notify.*
26 import net.pterodactylus.util.template.*
27 import org.hamcrest.MatcherAssert.*
28 import org.hamcrest.Matchers.*
29 import kotlin.test.*
30
31 /**
32  * Unit test for [LocalPostHandler].
33  */
34 class LocalPostHandlerTest {
35
36         private val eventBus = EventBus()
37         private val notificationManager = NotificationManager()
38         private val notification = ListNotification<Post>("", "", Template())
39
40         init {
41                 eventBus.register(LocalPostHandler(notificationManager, notification))
42         }
43
44         @Test
45         fun `handler adds post by local sone to notification`() {
46                 eventBus.post(NewPostFoundEvent(localPost))
47                 assertThat(notification.elements, contains<Post>(localPost))
48         }
49
50         @Test
51         fun `handler does not add post by remote sone to notification`() {
52                 eventBus.post(NewPostFoundEvent(remotePost))
53                 assertThat(notification.elements, emptyIterable())
54         }
55
56         @Test
57         fun `handler does not add notification to manager for post by remote sone`() {
58                 eventBus.post(NewPostFoundEvent(remotePost))
59                 assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
60         }
61
62         @Test
63         fun `handler adds notification to manager`() {
64                 eventBus.post(NewPostFoundEvent(localPost))
65                 assertThat(notificationManager.notifications, contains<Notification>(notification))
66         }
67
68         @Test
69         fun `handler does not add notification during first start`() {
70                 notificationManager.firstStart()
71                 eventBus.post(NewPostFoundEvent(localPost))
72                 assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
73         }
74
75         @Test
76         fun `handler removes post from notification when post is removed`() {
77                 notification.add(localPost)
78                 notificationManager.addNotification(notification)
79                 eventBus.post(PostRemovedEvent(localPost))
80                 assertThat(notification.elements, emptyIterable())
81         }
82
83         @Test
84         fun `handler does not remove remote post from notification when post is removed`() {
85                 notification.add(remotePost)
86                 notificationManager.addNotification(notification)
87                 eventBus.post(PostRemovedEvent(remotePost))
88                 assertThat(notification.elements, contains(remotePost))
89         }
90
91         @Test
92         fun `handler removes post from notification when post is marked as known`() {
93                 notification.add(localPost)
94                 notificationManager.addNotification(notification)
95                 eventBus.post(MarkPostKnownEvent(localPost))
96                 assertThat(notification.elements, emptyIterable())
97         }
98
99         @Test
100         fun `handler does not remove remote post from notification when post is marked as known`() {
101                 notification.add(remotePost)
102                 notificationManager.addNotification(notification)
103                 eventBus.post(MarkPostKnownEvent(remotePost))
104                 assertThat(notification.elements, contains(remotePost))
105         }
106
107 }
108
109 private val localSone: Sone = object : IdOnlySone("local-sone") {
110         override fun isLocal() = true
111 }
112 private val localPost: Post = object : Post.EmptyPost("local-post") {
113         override fun getSone() = localSone
114 }
115 private val remoteSone: Sone = IdOnlySone("remote-sone")
116 private val remotePost: Post = object : Post.EmptyPost("remote-post") {
117         override fun getSone() = remoteSone
118 }