Move createSone() method to test base class
[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.Profile
8 import net.pterodactylus.sone.data.Sone
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.rules.ExpectedException
15 import org.mockito.ArgumentMatchers.anyString
16
17 /**
18  * Base class for Sone FCP command tests.
19  */
20 abstract class SoneCommandTest {
21
22         @Rule @JvmField val expectedException = ExpectedException.none()!!
23
24         protected val core = mock<Core>()
25         protected val command: AbstractSoneCommand by lazy { createCommand(core) }
26
27         protected val parameters = SimpleFieldSet(true)
28         protected val localSone = mock<Sone>().apply {
29                 whenever(isLocal).thenReturn(true)
30         }
31         protected val remoteSone = mock<Sone>()
32
33         protected abstract fun createCommand(core: Core): AbstractSoneCommand
34
35         @Before
36         fun setupCore() {
37                 whenever(core.getSone(anyString())).thenReturn(absent())
38                 whenever(core.getPost(anyString())).thenReturn(absent())
39                 whenever(core.getPostReply(anyString())).thenReturn(absent())
40         }
41
42         protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock<Sone>().apply {
43                 whenever(this.id).thenReturn(id)
44                 whenever(this.name).thenReturn(name)
45                 whenever(profile).thenReturn(Profile(this).apply {
46                         this.firstName = firstName
47                         this.lastName = lastName
48                 })
49                 whenever(this.time).thenReturn(time)
50         }
51
52         protected fun executeCommandAndExpectFcpException() {
53                 expectedException.expect(FcpException::class.java)
54                 command.execute(parameters)
55         }
56
57         protected fun requestWithoutAnyParameterResultsInFcpException() {
58                 expectedException.expect(FcpException::class.java)
59                 command.execute(parameters)
60         }
61
62         protected fun requestWithEmptySoneParameterResultsInFcpException() {
63                 parameters.putSingle("Sone", null)
64                 executeCommandAndExpectFcpException()
65         }
66
67         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
68                 parameters.putSingle("Sone", "InvalidSoneId")
69                 executeCommandAndExpectFcpException()
70         }
71
72         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
73                 parameters.putSingle("Sone", "RemoteSoneId")
74                 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
75                 executeCommandAndExpectFcpException()
76         }
77
78 }