1 package net.pterodactylus.sone.fcp
3 import freenet.support.SimpleFieldSet
4 import net.pterodactylus.sone.core.Core
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.utils.asOptional
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.containsInAnyOrder
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.nullValue
13 import org.junit.Before
17 * Unit test for [GetPostCommand].
19 class GetPostCommandTest : SoneCommandTest() {
21 private val sone = mock<Sone>().apply {
22 whenever(id).thenReturn("SoneId")
24 private val post = createPost("ValidPostId", sone, null, 1000, "Post Text\r\nSecond \\Line")
25 private val sone1 = mock<Sone>().apply { whenever(id).thenReturn("Sone1") }
26 private val sone2 = mock<Sone>().apply { whenever(id).thenReturn("Sone2") }
27 private val postReply1 = createReply("ReplyId1", sone1, post, 1000, "Reply 1")
28 private val postReply2 = createReply("ReplyId2", sone2, post, 2000, "Reply 2")
30 override fun createCommand(core: Core) = GetPostCommand(core)
33 fun setupPostWithLikesAndReplies() {
34 whenever(core.getPost("ValidPostId")).thenReturn(post.asOptional())
35 whenever(core.getLikes(post)).thenReturn(setOf(sone1, sone2))
36 val replies = listOf(postReply1, postReply2)
37 whenever(core.getReplies("ValidPostId")).thenReturn(replies)
41 fun `command does not require write access`() {
42 assertThat(command.requiresWriteAccess(), equalTo(false))
46 fun `request without any parameter results in fcp exception`() {
47 requestWithoutAnyParameterResultsInFcpException()
51 fun `request with invalid post parameter results in fcp exception`() {
52 parameters += "Post" to "InvalidPostId"
53 executeCommandAndExpectFcpException()
56 private fun verifyPostWithLikes(replyParameters: SimpleFieldSet) {
57 assertThat(replyParameters["Message"], equalTo("Post"))
58 assertThat(replyParameters.parsePost("Post."), matchesPost(post))
59 assertThat(replyParameters["Post.Likes.Count"], equalTo("2"))
60 assertThat((0..1).map { replyParameters["Post.Likes.$it.ID"] }, containsInAnyOrder("Sone1", "Sone2"))
63 private fun verifyReplies(replyParameters: SimpleFieldSet) {
64 assertThat(replyParameters["Post.Replies.Count"], equalTo("2"))
65 assertThat(replyParameters.parsePost("Post.Replies.0."), matchesReply(postReply1))
66 assertThat(replyParameters.parsePost("Post.Replies.1."), matchesReply(postReply2))
70 fun `request with valid post parameter returns post response`() {
71 parameters += "Post" to "ValidPostId"
72 val replyParameters = command.execute(parameters).replyParameters
73 verifyPostWithLikes(replyParameters)
74 assertThat(replyParameters["Post.Recipient"], nullValue())
75 verifyReplies(replyParameters)
79 fun `request with valid post parameter without replies returns post response without replies`() {
80 parameters += "Post" to "ValidPostId"
81 parameters += "IncludeReplies" to "false"
82 val replyParameters = command.execute(parameters).replyParameters
83 verifyPostWithLikes(replyParameters)
84 assertThat(replyParameters["Post.Recipient"], nullValue())
85 assertThat(replyParameters["Post.Replies.Count"], nullValue())
89 fun `request with valid post parameter returns post response with recipient`() {
90 parameters += "Post" to "ValidPostId"
91 whenever(post.recipientId).thenReturn("Sone2".asOptional())
92 val replyParameters = command.execute(parameters).replyParameters
93 verifyPostWithLikes(replyParameters)
94 assertThat(replyParameters["Post.Recipient"], equalTo("Sone2"))
95 verifyReplies(replyParameters)
99 fun `request with valid post parameter without replies returns post response without replies but with recipient`() {
100 parameters += "Post" to "ValidPostId"
101 parameters += "IncludeReplies" to "false"
102 whenever(post.recipientId).thenReturn("Sone2".asOptional())
103 val replyParameters = command.execute(parameters).replyParameters
104 verifyPostWithLikes(replyParameters)
105 assertThat(replyParameters["Post.Recipient"], equalTo("Sone2"))
106 assertThat(replyParameters["Post.Replies.Count"], nullValue())