1 package net.pterodactylus.sone.fcp
3 import com.google.common.base.Optional.absent
4 import com.google.common.base.Optional.of
5 import freenet.support.SimpleFieldSet
6 import net.pterodactylus.sone.core.Core
7 import net.pterodactylus.sone.data.Post
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.freenet.fcp.FcpException
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Before
18 import org.junit.rules.ExpectedException
19 import org.mockito.ArgumentMatchers.anyString
22 * Unit test for [CreatePostCommand].
24 class CreatePostCommandTest {
26 @Rule @JvmField val expectedException = ExpectedException.none()
27 private val core = mock<Core>()
28 private val command = CreatePostCommand(core)
30 private val parameters = SimpleFieldSet(true)
31 private val localSone = mock<Sone>().apply {
32 whenever(isLocal).thenReturn(true)
34 private val remoteSone = mock<Sone>()
38 whenever(core.getSone(anyString())).thenReturn(absent())
42 fun `command requires write access`() {
43 assertThat(command.requiresWriteAccess(), equalTo(true))
47 fun `request without any parameters results in fcp exception`() {
48 expectedException.expect(FcpException::class.java)
49 command.execute(parameters, null, null)
53 fun `request with empty sone parameter results in fcp exception`() {
54 parameters.putSingle("Sone", null)
55 expectedException.expect(FcpException::class.java)
56 command.execute(parameters, null, null)
60 fun `request with invalid Sone parameter results in fcp exception`() {
61 parameters.putSingle("Sone", "InvalidSoneId")
62 expectedException.expect(FcpException::class.java)
63 command.execute(parameters, null, null)
67 fun `request with valid remote Sone parameter results in fcp exception`() {
68 parameters.putSingle("Sone", "RemoteSoneId")
69 whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone))
70 expectedException.expect(FcpException::class.java)
71 command.execute(parameters, null, null)
75 fun `request without text results in fcp exception`() {
76 parameters.putSingle("Sone", "LocalSoneId")
77 whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone))
78 expectedException.expect(FcpException::class.java)
79 command.execute(parameters, null, null)
83 fun `request with text creates post`() {
84 parameters.putSingle("Sone", "LocalSoneId")
85 parameters.putSingle("Text", "Test")
86 whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone))
87 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
88 whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post)
89 val response = command.execute(parameters, null, null)
90 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
91 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
95 fun `request with invalid recipient results in fcp exception`() {
96 parameters.putSingle("Sone", "LocalSoneId")
97 parameters.putSingle("Text", "Test")
98 parameters.putSingle("Recipient", "InvalidSoneId")
99 whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone))
100 expectedException.expect(FcpException::class.java)
101 command.execute(parameters, null, null)
105 fun `request with recipient the same as the sender returns an error response`() {
106 parameters.putSingle("Sone", "LocalSoneId")
107 parameters.putSingle("Text", "Test")
108 parameters.putSingle("Recipient", "LocalSoneId")
109 whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone))
110 val response = command.execute(parameters, null, null)
111 assertThat(response.replyParameters["Message"], equalTo("Error"))
112 assertThat(response.replyParameters["ErrorMessage"], notNullValue())
116 fun `request with text and recipient creates post`() {
117 parameters.putSingle("Sone", "LocalSoneId")
118 parameters.putSingle("Text", "Test")
119 parameters.putSingle("Recipient", "RemoteSoneId")
120 whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone))
121 whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone))
122 val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
123 whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post)
124 val response = command.execute(parameters, null, null)
125 assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
126 assertThat(response.replyParameters.get("Post"), equalTo("PostId"))