1 package net.pterodactylus.sone.fcp
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
14 import org.junit.rules.ExpectedException
15 import org.mockito.ArgumentMatchers.anyString
18 * Base class for Sone FCP command tests.
20 abstract class SoneCommandTest {
22 @Rule @JvmField val expectedException = ExpectedException.none()!!
24 protected val core = mock<Core>()
25 protected val command: AbstractSoneCommand by lazy { createCommand(core) }
27 protected val parameters = SimpleFieldSet(true)
28 protected val localSone = mock<Sone>().apply {
29 whenever(isLocal).thenReturn(true)
31 protected val remoteSone = mock<Sone>()
33 protected abstract fun createCommand(core: Core): AbstractSoneCommand
37 whenever(core.getSone(anyString())).thenReturn(absent())
38 whenever(core.getPost(anyString())).thenReturn(absent())
39 whenever(core.getPostReply(anyString())).thenReturn(absent())
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
49 whenever(this.time).thenReturn(time)
52 protected fun executeCommandAndExpectFcpException() {
53 expectedException.expect(FcpException::class.java)
54 command.execute(parameters)
57 protected fun requestWithoutAnyParameterResultsInFcpException() {
58 expectedException.expect(FcpException::class.java)
59 command.execute(parameters)
62 protected fun requestWithEmptySoneParameterResultsInFcpException() {
63 parameters.putSingle("Sone", null)
64 executeCommandAndExpectFcpException()
67 protected fun requestWithInvalidSoneParameterResultsInFcpException() {
68 parameters.putSingle("Sone", "InvalidSoneId")
69 executeCommandAndExpectFcpException()
72 fun requestWithValidRemoteSoneParameterResultsInFcpException() {
73 parameters.putSingle("Sone", "RemoteSoneId")
74 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
75 executeCommandAndExpectFcpException()