🔀 Merge branch 'release/v82'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / LikePostCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import net.pterodactylus.sone.core.*
4 import net.pterodactylus.sone.freenet.fcp.*
5 import net.pterodactylus.sone.test.*
6 import org.hamcrest.MatcherAssert.*
7 import org.hamcrest.Matchers.*
8 import org.junit.*
9 import org.mockito.Mockito.*
10
11 /**
12  * Unit test for [LikePostCommand].
13  */
14 class LikePostCommandTest : SoneCommandTest() {
15
16         private val post = createPost("PostId", mock(), null, 1000, "Text")
17
18         override fun createCommand(core: Core) = LikePostCommand(core)
19
20         @Before
21         fun setupPostAndSones() {
22                 whenever(core.getPost("PostId")).thenReturn(post)
23                 whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
24                 whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
25         }
26
27         @Test
28         fun `command requires write access`() {
29                 assertThat(command.requiresWriteAccess, equalTo(true))
30         }
31
32         @Test
33         fun `request without parameters results in FCP exception`() {
34                 requestWithoutAnyParameterResultsInFcpException()
35         }
36
37         @Test
38         fun `request with invalid post id results in FCP exception`() {
39                 parameters += "Post" to "InvalidPostId"
40                 expectedException.expect(FcpException::class.java)
41                 command.execute(parameters)
42         }
43
44         @Test
45         fun `request with missing local sone results in FCP exception`() {
46                 parameters += "Post" to "PostId"
47                 expectedException.expect(FcpException::class.java)
48                 command.execute(parameters)
49         }
50
51         @Test
52         fun `request with invalid sone results in FCP exception`() {
53                 parameters += "Post" to "PostId"
54                 parameters += "Sone" to "InvalidSoneId"
55                 expectedException.expect(FcpException::class.java)
56                 command.execute(parameters)
57         }
58
59         @Test
60         fun `request with valid remote sone results in FCP exception`() {
61                 parameters += "Post" to "PostId"
62                 parameters += "Sone" to "RemoteSoneId"
63                 expectedException.expect(FcpException::class.java)
64                 command.execute(parameters)
65         }
66
67         @Test
68         fun `request with valid parameters adds post to liked posts for sone`() {
69                 whenever(core.getLikes(post)).thenReturn(setOf(mock(), mock(), mock()))
70                 parameters += "Post" to "PostId"
71                 parameters += "Sone" to "LocalSoneId"
72                 val replyParameters = command.execute(parameters).replyParameters
73                 assertThat(replyParameters["Message"], equalTo("PostLiked"))
74                 assertThat(replyParameters["LikeCount"], equalTo("3"))
75                 verify(localSone).addLikedPostId("PostId")
76         }
77
78 }