b2fb4aeea47e61a85bd3ca266232e5299eb4cfd3
[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.Command
9 import net.pterodactylus.sone.freenet.fcp.FcpException
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import org.junit.Before
13 import org.junit.Rule
14 import org.junit.Test
15 import org.junit.rules.ExpectedException
16 import org.mockito.ArgumentMatchers
17 import org.mockito.ArgumentMatchers.anyString
18
19 /**
20  * TODO
21  */
22 abstract class SoneCommandTest {
23
24         @Rule @JvmField val expectedException = ExpectedException.none()!!
25
26         protected val core = mock<Core>()
27         protected val command: AbstractSoneCommand by lazy { createCommand(core) }
28
29         protected val parameters = SimpleFieldSet(true)
30         protected val localSone = mock<Sone>().apply {
31                 whenever(isLocal).thenReturn(true)
32         }
33         protected val remoteSone = mock<Sone>()
34
35         protected abstract fun createCommand(core: Core): AbstractSoneCommand
36
37         @Before
38         fun setupCore() {
39                 whenever(core.getSone(anyString())).thenReturn(absent())
40         }
41
42         protected fun requestWithoutAnyParameterResultsInFcpException() {
43                 expectedException.expect(FcpException::class.java)
44                 command.execute(parameters, null, null)
45         }
46
47         protected fun requestWithEmptySoneParameterResultsInFcpException() {
48                 parameters.putSingle("Sone", null)
49                 expectedException.expect(FcpException::class.java)
50                 command.execute(parameters, null, null)
51         }
52
53         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
54                 parameters.putSingle("Sone", "InvalidSoneId")
55                 expectedException.expect(FcpException::class.java)
56                 command.execute(parameters, null, null)
57         }
58
59         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
60                 parameters.putSingle("Sone", "RemoteSoneId")
61                 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
62                 expectedException.expect(FcpException::class.java)
63                 command.execute(parameters, null, null)
64         }
65
66 }