b02aa9e53015fcb2875d7f029e23476cc3fcd5b9
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / SoneCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import com.google.common.base.Optional
4 import com.google.common.base.Optional.absent
5 import freenet.support.SimpleFieldSet
6 import net.pterodactylus.sone.core.Core
7 import net.pterodactylus.sone.data.Sone
8 import net.pterodactylus.sone.freenet.fcp.FcpException
9 import net.pterodactylus.sone.test.mock
10 import net.pterodactylus.sone.test.whenever
11 import org.junit.Before
12 import org.junit.Rule
13 import org.junit.rules.ExpectedException
14 import org.mockito.ArgumentMatchers.anyString
15
16 /**
17  * Base class for Sone FCP command tests.
18  */
19 abstract class SoneCommandTest {
20
21         @Rule @JvmField val expectedException = ExpectedException.none()!!
22
23         protected val core = mock<Core>()
24         protected val command: AbstractSoneCommand by lazy { createCommand(core) }
25
26         protected val parameters = SimpleFieldSet(true)
27         protected val localSone = mock<Sone>().apply {
28                 whenever(isLocal).thenReturn(true)
29         }
30         protected val remoteSone = mock<Sone>()
31
32         protected abstract fun createCommand(core: Core): AbstractSoneCommand
33
34         @Before
35         fun setupCore() {
36                 whenever(core.getSone(anyString())).thenReturn(absent())
37                 whenever(core.getPost(anyString())).thenReturn(absent())
38                 whenever(core.getPostReply(anyString())).thenReturn(absent())
39         }
40
41         protected fun executeCommandAndExpectFcpException() {
42                 expectedException.expect(FcpException::class.java)
43                 command.execute(parameters, null, null)
44         }
45
46         protected fun requestWithoutAnyParameterResultsInFcpException() {
47                 expectedException.expect(FcpException::class.java)
48                 command.execute(parameters, null, null)
49         }
50
51         protected fun requestWithEmptySoneParameterResultsInFcpException() {
52                 parameters.putSingle("Sone", null)
53                 executeCommandAndExpectFcpException()
54         }
55
56         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
57                 parameters.putSingle("Sone", "InvalidSoneId")
58                 executeCommandAndExpectFcpException()
59         }
60
61         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
62                 parameters.putSingle("Sone", "RemoteSoneId")
63                 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
64                 executeCommandAndExpectFcpException()
65         }
66
67 }