2 * Sone - NewLocalPostHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.notification
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.*
33 * Unit test for [LocalPostHandler].
35 class LocalPostHandlerTest {
37 private val eventBus = EventBus()
38 private val notificationManager = NotificationManager()
39 private val notification = ListNotification<Post>("", "", Template())
42 eventBus.register(LocalPostHandler(notificationManager, notification))
46 fun `handler adds post by local sone to notification`() {
47 eventBus.post(NewPostFoundEvent(localPost))
48 assertThat(notification.elements, contains<Post>(localPost))
52 fun `handler does not add post by remote sone to notification`() {
53 eventBus.post(NewPostFoundEvent(remotePost))
54 assertThat(notification.elements, emptyIterable())
58 fun `handler adds notification to manager`() {
59 eventBus.post(NewPostFoundEvent(remotePost))
60 assertThat(notificationManager.notifications, contains<Notification>(notification))
64 fun `handler does not add notification during first start`() {
65 notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
66 override fun render(writer: Writer?) = Unit
68 eventBus.post(NewPostFoundEvent(remotePost))
69 assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
73 fun `handler removes post from notification when post is removed`() {
74 notification.add(localPost)
75 notificationManager.addNotification(notification)
76 eventBus.post(PostRemovedEvent(localPost))
77 assertThat(notification.elements, emptyIterable())
81 fun `handler does not remove remote post from notification when post is removed`() {
82 notification.add(remotePost)
83 notificationManager.addNotification(notification)
84 eventBus.post(PostRemovedEvent(remotePost))
85 assertThat(notification.elements, contains(remotePost))
89 fun `handler removes post from notification when post is marked as known`() {
90 notification.add(localPost)
91 notificationManager.addNotification(notification)
92 eventBus.post(MarkPostKnownEvent(localPost))
93 assertThat(notification.elements, emptyIterable())
97 fun `handler does not remove remote post from notification when post is marked as known`() {
98 notification.add(remotePost)
99 notificationManager.addNotification(notification)
100 eventBus.post(MarkPostKnownEvent(remotePost))
101 assertThat(notification.elements, contains(remotePost))
106 private val localSone: Sone = object : IdOnlySone("local-sone") {
107 override fun isLocal() = true
109 private val localPost: Post = object : Post.EmptyPost("local-post") {
110 override fun getSone() = localSone
112 private val remoteSone: Sone = IdOnlySone("remote-sone")
113 private val remotePost: Post = object : Post.EmptyPost("remote-post") {
114 override fun getSone() = remoteSone