1 package net.pterodactylus.sone.fcp
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
13 * Unit test for [GetSoneCommand].
15 class GetSoneCommandTest : SoneCommandTest() {
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"
22 override fun createCommand(core: Core) = GetSoneCommand(core)
25 fun `command does not require write access`() {
26 assertThat(command.requiresWriteAccess(), equalTo(false))
30 fun `request without any parameters results in fcp exception`() {
31 requestWithoutAnyParameterResultsInFcpException()
35 fun `request with empty Sone parameter results in fcp exception`() {
36 requestWithEmptySoneParameterResultsInFcpException()
40 fun `request with invalid Sone parameter results in fcp exception`() {
41 requestWithInvalidSoneParameterResultsInFcpException()
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())
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"))
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"))
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)