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