Add unit test for create reply command
[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                 whenever(core.getPost(anyString())).thenReturn(absent())
41         }
42
43         protected fun requestWithoutAnyParameterResultsInFcpException() {
44                 expectedException.expect(FcpException::class.java)
45                 command.execute(parameters, null, null)
46         }
47
48         protected fun requestWithEmptySoneParameterResultsInFcpException() {
49                 parameters.putSingle("Sone", null)
50                 expectedException.expect(FcpException::class.java)
51                 command.execute(parameters, null, null)
52         }
53
54         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
55                 parameters.putSingle("Sone", "InvalidSoneId")
56                 expectedException.expect(FcpException::class.java)
57                 command.execute(parameters, null, null)
58         }
59
60         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
61                 parameters.putSingle("Sone", "RemoteSoneId")
62                 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
63                 expectedException.expect(FcpException::class.java)
64                 command.execute(parameters, null, null)
65         }
66
67 }