🎨 Use equalTo instead of is
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / GetPostsCommandTest.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 org.hamcrest.MatcherAssert.assertThat
7 import org.hamcrest.Matchers.containsInAnyOrder
8 import org.hamcrest.Matchers.equalTo
9 import org.junit.Test
10
11 /**
12  * Unit test for [GetPostsCommand].
13  */
14 class GetPostsCommandTest : SoneCommandTest() {
15
16         private val sone1 = createSone("Sone1", "Sone1", "Sone", "#1", 1000)
17         private val sone2 = createSone("Sone2", "Sone2", "Sone", "#2", 2000)
18         private val post1 = createPost("Post1", remoteSone, null, 1000, "Post \\1\n")
19         private val post2 = createPost("Post2", localSone, null, 2000, "Post \\2\r")
20         private val post2Reply1 = createReply("Post2Reply1", localSone, post2, 2000, "Reply 1")
21         private val post2Reply2 = createReply("Post2Reply2", remoteSone, post2, 3000, "Reply 2")
22
23         override fun createCommand(core: Core) = GetPostsCommand(core)
24
25         @Test
26         fun `command does not require write access`() {
27                 assertThat(command.requiresWriteAccess, equalTo(false))
28         }
29
30         @Test
31         fun `request without any parameters results in fcp exception`() {
32                 requestWithoutAnyParameterResultsInFcpException()
33         }
34
35         @Test
36         fun `request with empty Sone parameter results in fcp exception`() {
37                 requestWithEmptySoneParameterResultsInFcpException()
38         }
39
40         @Test
41         fun `request with invalid Sone parameter results in fcp exception`() {
42                 requestWithInvalidSoneParameterResultsInFcpException()
43         }
44
45         private fun setupPostsAndReplies() {
46                 whenever(core.getLikes(post1)).thenReturn(setOf(localSone, sone1))
47                 whenever(core.getLikes(post2Reply1)).thenReturn(setOf(remoteSone, sone2))
48                 whenever(core.getReplies("Post2")).thenReturn(listOf(post2Reply1, post2Reply2))
49                 whenever(localSone.id).thenReturn("LocalSone")
50                 whenever(remoteSone.id).thenReturn("RemoteSone")
51                 whenever(core.getSone("LocalSone")).thenReturn(localSone)
52                 whenever(core.getSone("ValidSoneId")).thenReturn(remoteSone)
53                 whenever(remoteSone.posts).thenReturn(listOf(post2, post1))
54                 parameters += "Sone" to "ValidSoneId"
55         }
56
57         private fun verifyFirstPost(replyParameters: SimpleFieldSet, index: Int = 0) {
58                 assertThat(replyParameters.parsePost("Posts.$index."), matchesPost(post2))
59                 assertThat(replyParameters["Posts.$index.Replies.Count"], equalTo("2"))
60                 assertThat(replyParameters.parseReply("Posts.$index.Replies.0."), matchesReply(post2Reply1))
61                 assertThat(replyParameters["Posts.$index.Replies.0.Likes.Count"], equalTo("2"))
62                 assertThat((0..1).map { replyParameters["Posts.$index.Replies.0.Likes.$it.ID"] }, containsInAnyOrder("RemoteSone", "Sone2"))
63                 assertThat(replyParameters.parseReply("Posts.$index.Replies.1."), matchesReply(post2Reply2))
64         }
65
66         private fun verifySecondPost(replyParameters: SimpleFieldSet, index: Int = 1) {
67                 assertThat(replyParameters.parsePost("Posts.$index."), matchesPost(post1))
68                 assertThat(replyParameters["Posts.$index.Likes.Count"], equalTo("2"))
69                 assertThat((0..1).map { replyParameters["Posts.$index.Likes.$it.ID"] }, containsInAnyOrder("LocalSone", "Sone1"))
70         }
71
72         @Test
73         fun `request with valid sone parameter lists all posts of the sone`() {
74                 setupPostsAndReplies()
75
76                 val replyParameters = command.execute(parameters).replyParameters
77
78                 assertThat(replyParameters["Message"], equalTo("Posts"))
79                 assertThat(replyParameters["Posts.Count"], equalTo("2"))
80                 verifyFirstPost(replyParameters)
81                 verifySecondPost(replyParameters)
82         }
83
84         @Test
85         fun `request with a maximum of 1 post returns only 1 post`() {
86                 setupPostsAndReplies()
87                 parameters += "MaxPosts" to "1"
88
89                 val replyParameters = command.execute(parameters).replyParameters
90                 assertThat(replyParameters["Message"], equalTo("Posts"))
91                 assertThat(replyParameters["Posts.Count"], equalTo("1"))
92                 verifyFirstPost(replyParameters)
93         }
94
95         @Test
96         fun `request starting at the second post returns only 1 post`() {
97                 setupPostsAndReplies()
98                 parameters += "StartPost" to "1"
99
100                 val replyParameters = command.execute(parameters).replyParameters
101                 assertThat(replyParameters["Message"], equalTo("Posts"))
102                 assertThat(replyParameters["Posts.Count"], equalTo("1"))
103                 verifySecondPost(replyParameters, 0)
104         }
105
106         @Test
107         fun `request skipping more posts than exist returns an empty list`() {
108                 setupPostsAndReplies()
109                 parameters += "StartPost" to "20"
110
111                 val replyParameters = command.execute(parameters).replyParameters
112                 assertThat(replyParameters["Message"], equalTo("Posts"))
113                 assertThat(replyParameters["Posts.Count"], equalTo("0"))
114         }
115
116 }