c319809871bdc9fd5f68cdcfbb6420c868a5dadb
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / text / SoneMentionDetectorTest.kt
1 /**
2  * Sone - SoneMentionDetectorTest.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.text
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.database.*
24 import net.pterodactylus.sone.test.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
27 import kotlin.test.*
28
29 /**
30  * Unit test for [SoneMentionDetector].
31  */
32 class SoneMentionDetectorTest {
33
34         private val eventBus = EventBus()
35         private val soneProvider = TestSoneProvider()
36         private val postProvider = TestPostProvider()
37         private val soneTextParser = SoneTextParser(soneProvider, postProvider)
38         private val capturedEvents = mutableListOf<LocalSoneMentionedInPostEvent>()
39
40         init {
41                 eventBus.register(SoneMentionDetector(eventBus, soneTextParser))
42                 eventBus.register(object : Any() {
43                         @Subscribe
44                         fun captureEvent(localSoneMentionedInPostEvent: LocalSoneMentionedInPostEvent) {
45                                 capturedEvents += localSoneMentionedInPostEvent
46                         }
47                 })
48         }
49
50         @Test
51         fun `detector does not emit event on post that does not contain any sones`() {
52                 val post = createPost()
53                 eventBus.post(NewPostFoundEvent(post))
54                 assertThat(capturedEvents, emptyIterable())
55         }
56
57         @Test
58         fun `detector does not emit event on post that does contain two remote sones`() {
59                 val post = createPost("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
60                 eventBus.post(NewPostFoundEvent(post))
61                 assertThat(capturedEvents, emptyIterable())
62         }
63
64         @Test
65         fun `detector emits event on post that contains links to a remote and a local sone`() {
66                 val post = createPost("text mentions sone://${localSone1.id} and sone://${remoteSone2.id}.")
67                 eventBus.post(NewPostFoundEvent(post))
68                 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
69         }
70
71         @Test
72         fun `detector emits one event on post that contains two links to the same local sone`() {
73                 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone1.id}.")
74                 eventBus.post(NewPostFoundEvent(post))
75                 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
76         }
77
78         @Test
79         fun `detector emits one event on post that contains links to two local sones`() {
80                 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone2.id}.")
81                 eventBus.post(NewPostFoundEvent(post))
82                 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
83         }
84
85         @Test
86         fun `detector does not emit event for post by local sone`() {
87                 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", localSone1)
88                 eventBus.post(NewPostFoundEvent(post))
89                 assertThat(capturedEvents, emptyIterable())
90         }
91
92 }
93
94 private val remoteSone1 = createRemoteSone()
95 private val remoteSone2 = createRemoteSone()
96
97 private val localSone1 = createLocalSone()
98 private val localSone2 = createLocalSone()
99
100 private fun createPost(text: String = "", sone: Sone = remoteSone1): Post.EmptyPost {
101         return object : Post.EmptyPost("post-id") {
102                 override fun getSone() = sone
103                 override fun getText() = text
104         }
105 }
106
107 private class TestSoneProvider : SoneProvider {
108
109         override val sones: Collection<Sone> get() = remoteSones + localSones
110         override val localSones: Collection<Sone> get() = setOf(localSone1, localSone2)
111         override val remoteSones: Collection<Sone> get() = setOf(remoteSone1, remoteSone2)
112         override val soneLoader: (String) -> Sone? get() = this::getSone
113         override fun getSone(soneId: String): Sone? =
114                         localSones.firstOrNull { it.id == soneId } ?: remoteSones.firstOrNull { it.id == soneId }
115
116 }
117
118 private class TestPostProvider : PostProvider {
119
120         override fun getPost(postId: String): Post? = null
121         override fun getPosts(soneId: String): Collection<Post> = emptyList()
122         override fun getDirectedPosts(recipientId: String): Collection<Post> = emptyList()
123
124 }