1 package net.pterodactylus.sone.fcp
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.hamcrest.Matchers.notNullValue
13 * Unit test for [CreatePostCommand].
15 class CreatePostCommandTest : SoneCommandTest() {
17 override fun createCommand(core: Core) = CreatePostCommand(core)
20 fun `command requires write access`() {
21 assertThat(command.requiresWriteAccess, equalTo(true))
25 fun `request without any parameters results in fcp exception`() {
26 requestWithoutAnyParameterResultsInFcpException()
30 fun `request with empty Sone parameter results in fcp exception`() {
31 requestWithEmptySoneParameterResultsInFcpException()
35 fun `request with invalid Sone parameter results in fcp exception`() {
36 requestWithInvalidSoneParameterResultsInFcpException()
40 fun `request with valid remote Sone parameter results in fcp exception`() {
41 requestWithValidRemoteSoneParameterResultsInFcpException()
45 fun `request without text results in fcp exception`() {
46 parameters += "Sone" to "LocalSoneId"
47 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
48 executeCommandAndExpectFcpException()
52 fun `request with text creates post`() {
53 parameters += "Sone" to "LocalSoneId"
54 parameters += "Text" to "Test"
55 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
56 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
57 whenever(core.createPost(localSone, null, "Test")).thenReturn(post)
58 val response = command.execute(parameters)
59 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
60 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
64 fun `request with invalid recipient results in fcp exception`() {
65 parameters += "Sone" to "LocalSoneId"
66 parameters += "Text" to "Test"
67 parameters += "Recipient" to "InvalidSoneId"
68 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
69 executeCommandAndExpectFcpException()
73 fun `request with recipient the same as the sender returns an error response`() {
74 parameters += "Sone" to "LocalSoneId"
75 parameters += "Text" to "Test"
76 parameters += "Recipient" to "LocalSoneId"
77 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
78 val response = command.execute(parameters)
79 assertThat(response.replyParameters["Message"], equalTo("Error"))
80 assertThat(response.replyParameters["ErrorMessage"], notNullValue())
84 fun `request with text and recipient creates post`() {
85 parameters += "Sone" to "LocalSoneId"
86 parameters += "Text" to "Test"
87 parameters += "Recipient" to "RemoteSoneId"
88 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
89 whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
90 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
91 whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post)
92 val response = command.execute(parameters)
93 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
94 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))