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