Add unit test for create post command
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / CreatePostCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
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
16 import org.junit.Rule
17 import org.junit.Test
18 import org.junit.rules.ExpectedException
19 import org.mockito.ArgumentMatchers.anyString
20
21 /**
22  * Unit test for [CreatePostCommand].
23  */
24 class CreatePostCommandTest {
25
26         @Rule @JvmField val expectedException = ExpectedException.none()
27         private val core = mock<Core>()
28         private val command = CreatePostCommand(core)
29
30         private val parameters = SimpleFieldSet(true)
31         private val localSone = mock<Sone>().apply {
32                 whenever(isLocal).thenReturn(true)
33         }
34         private val remoteSone = mock<Sone>()
35
36         @Before
37         fun setupCore() {
38                 whenever(core.getSone(anyString())).thenReturn(absent())
39         }
40
41         @Test
42         fun `command requires write access`() {
43             assertThat(command.requiresWriteAccess(), equalTo(true))
44         }
45
46         @Test
47         fun `request without any parameters results in fcp exception`() {
48                 expectedException.expect(FcpException::class.java)
49                 command.execute(parameters, null, null)
50         }
51
52         @Test
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)
57         }
58
59         @Test
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)
64         }
65
66         @Test
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)
72         }
73
74         @Test
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)
80         }
81
82         @Test
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"))
92         }
93
94         @Test
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)
102         }
103
104         @Test
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())
113         }
114
115         @Test
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"))
127         }
128
129 }