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