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