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 [GetPostsCommand].
14 class GetPostsCommandTest : SoneCommandTest() {
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")
23 override fun createCommand(core: Core) = GetPostsCommand(core)
26 fun `command does not require write access`() {
27 assertThat(command.requiresWriteAccess, equalTo(false))
31 fun `request without any parameters results in fcp exception`() {
32 requestWithoutAnyParameterResultsInFcpException()
36 fun `request with empty Sone parameter results in fcp exception`() {
37 requestWithEmptySoneParameterResultsInFcpException()
41 fun `request with invalid Sone parameter results in fcp exception`() {
42 requestWithInvalidSoneParameterResultsInFcpException()
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"
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))
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"))
73 fun `request with valid sone parameter lists all posts of the sone`() {
74 setupPostsAndReplies()
76 val replyParameters = command.execute(parameters).replyParameters
78 assertThat(replyParameters["Message"], equalTo("Posts"))
79 assertThat(replyParameters["Posts.Count"], equalTo("2"))
80 verifyFirstPost(replyParameters)
81 verifySecondPost(replyParameters)
85 fun `request with a maximum of 1 post returns only 1 post`() {
86 setupPostsAndReplies()
87 parameters += "MaxPosts" to "1"
89 val replyParameters = command.execute(parameters).replyParameters
90 assertThat(replyParameters["Message"], equalTo("Posts"))
91 assertThat(replyParameters["Posts.Count"], equalTo("1"))
92 verifyFirstPost(replyParameters)
96 fun `request starting at the second post returns only 1 post`() {
97 setupPostsAndReplies()
98 parameters += "StartPost" to "1"
100 val replyParameters = command.execute(parameters).replyParameters
101 assertThat(replyParameters["Message"], equalTo("Posts"))
102 assertThat(replyParameters["Posts.Count"], equalTo("1"))
103 verifySecondPost(replyParameters, 0)
107 fun `request skipping more posts than exist returns an empty list`() {
108 setupPostsAndReplies()
109 parameters += "StartPost" to "20"
111 val replyParameters = command.execute(parameters).replyParameters
112 assertThat(replyParameters["Message"], equalTo("Posts"))
113 assertThat(replyParameters["Posts.Count"], equalTo("0"))