🚧 Filter new replies using the current Sone
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / NewElementsTest.kt
1 /**
2  * Sone - NewElementsTest.kt - Copyright Â© 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 package net.pterodactylus.sone.web
18
19 import com.google.inject.Guice
20 import com.google.inject.name.Names.named
21 import net.pterodactylus.sone.data.Post
22 import net.pterodactylus.sone.data.PostReply
23 import net.pterodactylus.sone.data.Sone
24 import net.pterodactylus.sone.notify.ListNotification
25 import net.pterodactylus.sone.notify.PostVisibilityFilter
26 import net.pterodactylus.sone.notify.ReplyVisibilityFilter
27 import net.pterodactylus.sone.notify.matchThisPost
28 import net.pterodactylus.sone.notify.matchThisReply
29 import net.pterodactylus.sone.notify.showAllPosts
30 import net.pterodactylus.sone.notify.showAllReplies
31 import net.pterodactylus.sone.test.createLocalSone
32 import net.pterodactylus.sone.test.createPost
33 import net.pterodactylus.sone.test.createPostReply
34 import net.pterodactylus.sone.test.getInstance
35 import net.pterodactylus.sone.test.isProvidedBy
36 import net.pterodactylus.sone.test.key
37 import net.pterodactylus.util.template.Template
38 import org.hamcrest.MatcherAssert.assertThat
39 import org.hamcrest.Matchers.contains
40 import org.hamcrest.Matchers.containsInAnyOrder
41 import org.junit.Test
42
43 class NewElementsTest {
44
45         private val newPostNotification = ListNotification<Post>("", "", Template())
46         private val newReplyNotification = ListNotification<PostReply>("", "", Template())
47         private val localPostNotification = ListNotification<Post>("", "", Template())
48         private val localReplyNotification = ListNotification<PostReply>("", "", Template())
49         private val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, showAllPosts, showAllReplies)
50
51         init {
52                 localPostNotification.add(post1)
53                 newPostNotification.add(post2)
54                 localReplyNotification.add(reply1)
55                 newReplyNotification.add(reply2)
56         }
57
58         @Test
59         fun `new elements container can be created by guice`() {
60                 val injector = Guice.createInjector(
61                                 key<ListNotification<Post>>(named("newRemotePost")).isProvidedBy(newPostNotification),
62                                 key<ListNotification<Post>>(named("localPost")).isProvidedBy(localPostNotification),
63                                 key<ListNotification<PostReply>>(named("newRemotePostReply")).isProvidedBy(newReplyNotification),
64                                 key<ListNotification<PostReply>>(named("localReply")).isProvidedBy(localReplyNotification)
65                 )
66                 injector.getInstance<NewElements>()
67         }
68
69         @Test
70         fun `new posts include new and local posts`() {
71                 assertThat(newElements.newPosts(), containsInAnyOrder(post1, post2))
72         }
73
74         @Test
75         fun `new posts are filtered`() {
76                 val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, matchThisPost(post2), showAllReplies)
77                 assertThat(newElements.newPosts(), contains(post2))
78         }
79
80         @Test
81         fun `new replies include new and local replies`() {
82                 assertThat(newElements.newReplies(), containsInAnyOrder(reply1, reply2))
83         }
84
85         @Test
86         fun `new replies are filtered`() {
87                 val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, showAllPosts, matchThisReply(reply2))
88                 assertThat(newElements.newReplies(), containsInAnyOrder(reply2))
89         }
90
91         @Test
92         fun `new posts are filtered using the given Sone`() {
93                 val postVisibilityFilter = object : PostVisibilityFilter {
94                         override fun isPostVisible(sone: Sone?, post: Post) = (sone == localSone) && (post == post2)
95                 }
96                 val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, postVisibilityFilter, showAllReplies)
97                 assertThat(newElements.newPosts(localSone), contains(post2))
98         }
99
100         @Test
101         fun `new replies are filtered using the given Sone`() {
102                 val replyVisibilityFilter = object : ReplyVisibilityFilter {
103                         override fun isReplyVisible(sone: Sone?, reply: PostReply) = (sone == localSone) && (reply == reply2)
104                 }
105                 val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, showAllPosts, replyVisibilityFilter)
106                 assertThat(newElements.newReplies(localSone), contains(reply2))
107         }
108
109 }
110
111 private val post1 = createPost()
112 private val post2 = createPost()
113 private val reply1 = createPostReply()
114 private val reply2 = createPostReply()
115 private val localSone = createLocalSone()