Use more concise SAM implementation
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / GetSoneCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.freenet.fcp.FcpException
5 import net.pterodactylus.sone.test.asOptional
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.hamcrest.Matchers.nullValue
10 import org.junit.Test
11
12 /**
13  * Unit test for [GetSoneCommand].
14  */
15 class GetSoneCommandTest : SoneCommandTest() {
16
17         private val sone = createSone("SoneId", "Sone", "Sone", "#1", 1000).apply {
18                 profile.addField("Test").value = "true"
19                 profile.addField("More Test").value = "also true"
20         }
21
22         override fun createCommand(core: Core) = GetSoneCommand(core)
23
24         @Test
25         fun `command does not require write access`() {
26                 assertThat(command.requiresWriteAccess(), equalTo(false))
27         }
28
29         @Test
30         fun `request without any parameters results in fcp exception`() {
31                 requestWithoutAnyParameterResultsInFcpException()
32         }
33
34         @Test
35         fun `request with empty Sone parameter results in fcp exception`() {
36                 requestWithEmptySoneParameterResultsInFcpException()
37         }
38
39         @Test
40         fun `request with invalid Sone parameter results in fcp exception`() {
41                 requestWithInvalidSoneParameterResultsInFcpException()
42         }
43
44         @Test
45         fun `request with valid Sone parameter results in response with Sone information`() {
46                 whenever(core.getSone("SoneId")).thenReturn(sone.asOptional())
47                 whenever(core.getSone(null)).thenReturn(null.asOptional())
48                 parameters += "Sone" to "SoneId"
49                 val replyParameters = command.execute(parameters).replyParameters
50                 assertThat(replyParameters["Message"], equalTo("Sone"))
51                 assertThat(replyParameters.parseSone("Sone."), matchesSone(sone))
52                 assertThat(replyParameters["Sone.Followed"], nullValue())
53         }
54
55         @Test
56         fun `request with local sone parameter results in followed being true for friend sone`() {
57                 whenever(core.getSone("SoneId")).thenReturn(sone.asOptional())
58                 whenever(core.getSone("LocalSone")).thenReturn(localSone.asOptional())
59                 whenever(localSone.id).thenReturn("LocalSone")
60                 whenever(localSone.hasFriend("SoneId")).thenReturn(true)
61                 parameters += "Sone" to "SoneId"
62                 parameters += "LocalSone" to "LocalSone"
63                 val replyParameters = command.execute(parameters).replyParameters
64                 assertThat(replyParameters["Message"], equalTo("Sone"))
65                 assertThat(replyParameters.parseSone("Sone."), matchesSone(sone))
66                 assertThat(replyParameters["Sone.Followed"], equalTo("true"))
67         }
68         
69         @Test
70         fun `request with local sone parameter results in followed being false for non-friend sone`() {
71                 whenever(core.getSone("SoneId")).thenReturn(sone.asOptional())
72                 whenever(core.getSone("LocalSone")).thenReturn(localSone.asOptional())
73                 whenever(localSone.id).thenReturn("LocalSone")
74                 parameters += "Sone" to "SoneId"
75                 parameters += "LocalSone" to "LocalSone"
76                 val replyParameters = command.execute(parameters).replyParameters
77                 assertThat(replyParameters["Message"], equalTo("Sone"))
78                 assertThat(replyParameters.parseSone("Sone."), matchesSone(sone))
79                 assertThat(replyParameters["Sone.Followed"], equalTo("false"))
80         }
81
82         @Test
83         fun `request with remote sone as local sone parameter results in fcp exception`() {
84                 whenever(core.getSone("SoneId")).thenReturn(sone.asOptional())
85                 whenever(core.getSone("RemoteSone")).thenReturn(remoteSone.asOptional())
86                 whenever(localSone.id).thenReturn("RemoteSone")
87                 parameters += "Sone" to "SoneId"
88                 parameters += "LocalSone" to "RemoteSone"
89                 expectedException.expect(FcpException::class.java)
90                 command.execute(parameters)
91         }
92
93 }