72daa07ce5584c9bf251ff6d18259752d9e3e632
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / GetPostFeedCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import freenet.support.SimpleFieldSet
4 import net.pterodactylus.sone.core.Core
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.utils.asOptional
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.containsInAnyOrder
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Test
11
12 /**
13  * Unit test for [GetPostFeedCommand].
14  */
15 class GetPostFeedCommandTest : SoneCommandTest() {
16
17         private val sone1 = createSone("Sone1", "Sone 1", "Sone", "#1", 1000)
18         private val sone2 = createSone("Sone2", "Sone 2", "Sone", "#2", 2000)
19         private val sone3 = createSone("Sone3", "Sone 3", "Sone", "#3", 3000)
20         private val sone4 = createSone("Sone4", "Sone 4", "Sone", "#4", 4000)
21         private val friend1 = createSone("Friend1", "Friend 1", "Friend", "#1", 5000)
22         private val post1 = createPost("Post1", sone1, null, 1000, "Post 1")
23         private val post1Reply1 = createReply("Post1Reply1", sone1, post1, 10000, "Post 1, Reply 1")
24         private val post1Reply2 = createReply("Post1Reply2", sone2, post1, 20000, "Post 1, Reply 2")
25         private val post2 = createPost("Post2", sone2, "Recipient 2", 2000, "Post 2")
26         private val post2Reply1 = createReply("Post2Reply1", sone3, post2, 30000, "Post 2, Reply 1")
27         private val post2Reply2 = createReply("Post2Reply2", sone4, post2, 40000, "Post 2, Reply 2")
28         private val friendPost1 = createPost("FriendPost1", friend1, null, 1500, "Friend Post 1")
29         private val directedPost = createPost("DirectedPost1", sone3, "ValidSoneId", 500, "Hey!")
30
31         override fun createCommand(core: Core) = GetPostFeedCommand(core)
32
33         @Test
34         fun `command does not require write access`() {
35                 assertThat(command.requiresWriteAccess(), equalTo(false))
36         }
37
38         @Test
39         fun `request without any parameters results in fcp exception`() {
40                 requestWithoutAnyParameterResultsInFcpException()
41         }
42
43         @Test
44         fun `request with empty Sone parameter results in fcp exception`() {
45                 requestWithEmptySoneParameterResultsInFcpException()
46         }
47
48         @Test
49         fun `request with invalid Sone parameter results in fcp exception`() {
50                 requestWithInvalidSoneParameterResultsInFcpException()
51         }
52
53         @Test
54         fun `request with valid remote Sone parameter results in fcp exception`() {
55                 requestWithValidRemoteSoneParameterResultsInFcpException()
56         }
57
58         private fun setupAllPostsAndReplies() {
59                 parameters += "Sone" to "ValidSoneId"
60                 whenever(localSone.id).thenReturn("ValidSoneId")
61                 whenever(core.getSone("ValidSoneId")).thenReturn(localSone.asOptional())
62                 whenever(core.getSone("Friend1")).thenReturn(friend1.asOptional())
63                 whenever(core.getLikes(post1)).thenReturn(setOf(sone3, sone4))
64                 whenever(core.getLikes(post1Reply1)).thenReturn(setOf(sone2, sone3))
65                 whenever(core.getLikes(post1Reply2)).thenReturn(setOf(sone3))
66                 whenever(core.getReplies("Post1")).thenReturn(listOf(post1Reply1, post1Reply2))
67                 whenever(core.getLikes(post2)).thenReturn(setOf(sone1, sone2))
68                 whenever(core.getLikes(post2Reply1)).thenReturn(setOf(sone4, sone1))
69                 whenever(core.getLikes(post2Reply2)).thenReturn(setOf(sone1, sone2, sone3))
70                 whenever(core.getReplies("Post2")).thenReturn(listOf(post2Reply1, post2Reply2))
71                 whenever(localSone.posts).thenReturn(listOf(post2, post1))
72                 whenever(core.getLikes(friendPost1)).thenReturn(setOf(sone1, friend1))
73                 whenever(friend1.posts).thenReturn(listOf(friendPost1))
74                 whenever(localSone.friends).thenReturn(setOf("Friend1", "Friend2"))
75                 whenever(core.getDirectedPosts("ValidSoneId")).thenReturn(setOf(directedPost))
76                 whenever(core.getLikes(directedPost)).thenReturn(setOf(sone2, sone4))
77         }
78
79         private fun verifyFirstPost(replyParameters: SimpleFieldSet) {
80                 assertThat(replyParameters.parsePost("Posts.0."), matchesPost(post2))
81                 assertThat(replyParameters["Posts.0.Replies.Count"], equalTo("2"))
82                 assertThat(replyParameters.parseReply("Posts.0.Replies.0."), matchesReply(post2Reply1))
83                 assertThat(replyParameters["Posts.0.Replies.0.Likes.Count"], equalTo("2"))
84                 assertThat((0..1).map { replyParameters["Posts.0.Replies.0.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Sone4"))
85                 assertThat(replyParameters.parseReply("Posts.0.Replies.1."), matchesReply(post2Reply2))
86                 assertThat(replyParameters["Posts.0.Replies.1.Likes.Count"], equalTo("3"))
87                 assertThat((0..2).map { replyParameters["Posts.0.Replies.1.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Sone2", "Sone3"))
88                 assertThat(replyParameters["Posts.0.Likes.Count"], equalTo("2"))
89                 assertThat((0..1).map { replyParameters["Posts.0.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Sone2"))
90         }
91
92         private fun verifySecondPost(replyParameters: SimpleFieldSet, index: Int = 1) {
93                 assertThat(replyParameters.parsePost("Posts.$index."), matchesPost(friendPost1))
94                 assertThat(replyParameters["Posts.$index.Replies.Count"], equalTo("0"))
95                 assertThat(replyParameters["Posts.$index.Likes.Count"], equalTo("2"))
96                 assertThat((0..1).map { replyParameters["Posts.$index.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Friend1"))
97         }
98
99         private fun verifyThirdPost(replyParameters: SimpleFieldSet, index: Int = 2) {
100                 assertThat(replyParameters.parsePost("Posts.$index."), matchesPost(post1))
101                 assertThat(replyParameters.parseReply("Posts.$index.Replies.0."), matchesReply(post1Reply1))
102                 assertThat(replyParameters.parseReply("Posts.$index.Replies.1."), matchesReply(post1Reply2))
103                 assertThat(replyParameters["Posts.$index.Replies.0.Likes.Count"], equalTo("2"))
104                 assertThat((0..1).map { replyParameters["Posts.$index.Replies.0.Likes.$it.ID"] }, containsInAnyOrder("Sone2", "Sone3"))
105                 assertThat(replyParameters["Posts.$index.Replies.1.Likes.Count"], equalTo("1"))
106                 assertThat(replyParameters["Posts.$index.Replies.1.Likes.0.ID"], equalTo("Sone3"))
107                 assertThat(replyParameters["Posts.$index.Likes.Count"], equalTo("2"))
108                 assertThat((0..1).map { replyParameters["Posts.$index.Likes.$it.ID"] }, containsInAnyOrder("Sone3", "Sone4"))
109         }
110
111         private fun verifyFourthPost(replyParameters: SimpleFieldSet, index: Int = 3) {
112                 assertThat(replyParameters.parsePost("Posts.$index."), matchesPost(directedPost))
113                 assertThat(replyParameters["Posts.$index.Replies.Count"], equalTo("0"))
114                 assertThat(replyParameters["Posts.$index.Likes.Count"], equalTo("2"))
115                 assertThat((0..1).map { replyParameters["Posts.$index.Likes.$it.ID"] }, containsInAnyOrder("Sone2", "Sone4"))
116         }
117
118         @Test
119         fun `request with valid local Sone parameter results in the post feed with all required posts`() {
120                 setupAllPostsAndReplies()
121
122                 val replyParameters = command.execute(parameters).replyParameters
123
124                 assertThat(replyParameters["Message"], equalTo("PostFeed"))
125                 assertThat(replyParameters["Posts.Count"], equalTo("4"))
126
127                 verifyFirstPost(replyParameters)
128                 verifySecondPost(replyParameters)
129                 verifyThirdPost(replyParameters)
130                 verifyFourthPost(replyParameters)
131         }
132
133         @Test
134         fun `request with larger start than number of posts returns empty feed`() {
135                 setupAllPostsAndReplies()
136                 parameters += "StartPost" to "20"
137                 val replyParameters = command.execute(parameters).replyParameters
138                 assertThat(replyParameters["Message"], equalTo("PostFeed"))
139                 assertThat(replyParameters["Posts.Count"], equalTo("0"))
140         }
141
142         @Test
143         fun `request with max posts of 2 returns the first two posts`() {
144                 setupAllPostsAndReplies()
145                 parameters += "MaxPosts" to "2"
146                 val replyParameters = command.execute(parameters).replyParameters
147                 assertThat(replyParameters["Message"], equalTo("PostFeed"))
148                 assertThat(replyParameters["Posts.Count"], equalTo("2"))
149
150                 verifyFirstPost(replyParameters)
151                 verifySecondPost(replyParameters)
152         }
153
154         @Test
155         fun `request with max posts of 2 and start post of 1 returns the center two posts`() {
156                 setupAllPostsAndReplies()
157                 parameters += "StartPost" to "1"
158                 parameters += "MaxPosts" to "2"
159                 val replyParameters = command.execute(parameters).replyParameters
160                 assertThat(replyParameters["Message"], equalTo("PostFeed"))
161                 assertThat(replyParameters["Posts.Count"], equalTo("2"))
162
163                 verifySecondPost(replyParameters, 0)
164                 verifyThirdPost(replyParameters, 1)
165         }
166
167         @Test
168         fun `request with max posts of 2 and start post of 3 returns the last post`() {
169                 setupAllPostsAndReplies()
170                 parameters += "StartPost" to "3"
171                 parameters += "MaxPosts" to "2"
172                 val replyParameters = command.execute(parameters).replyParameters
173                 assertThat(replyParameters["Message"], equalTo("PostFeed"))
174                 assertThat(replyParameters["Posts.Count"], equalTo("1"))
175
176                 verifyFourthPost(replyParameters, 0)
177         }
178
179 }