From 00c0f66598af0a462cb367fbf1ad67512878cf5e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 7 Jan 2017 09:52:24 +0100 Subject: [PATCH] Extract common test methods for all commands --- .../sone/fcp/CreatePostCommandTest.kt | 46 ++++----------- .../net/pterodactylus/sone/fcp/SoneCommandTest.kt | 66 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt index d7eeff8..75e238e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt @@ -1,80 +1,54 @@ package net.pterodactylus.sone.fcp +import com.google.common.base.Optional import com.google.common.base.Optional.absent import com.google.common.base.Optional.of -import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Post -import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue -import org.junit.Before -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException -import org.mockito.ArgumentMatchers.anyString /** * Unit test for [CreatePostCommand]. */ -class CreatePostCommandTest { +class CreatePostCommandTest : SoneCommandTest() { - @Rule @JvmField val expectedException = ExpectedException.none() - private val core = mock() - private val command = CreatePostCommand(core) - - private val parameters = SimpleFieldSet(true) - private val localSone = mock().apply { - whenever(isLocal).thenReturn(true) - } - private val remoteSone = mock() - - @Before - fun setupCore() { - whenever(core.getSone(anyString())).thenReturn(absent()) - } + override fun createCommand(core: Core) = CreatePostCommand(core) @Test fun `command requires write access`() { - assertThat(command.requiresWriteAccess(), equalTo(true)) + assertThat(command.requiresWriteAccess(), equalTo(true)) } @Test fun `request without any parameters results in fcp exception`() { - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithoutAnyParameterResultsInFcpException() } @Test - fun `request with empty sone parameter results in fcp exception`() { - parameters.putSingle("Sone", null) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + fun `request with empty Sone parameter results in fcp exception`() { + requestWithEmptySoneParameterResultsInFcpException() } @Test fun `request with invalid Sone parameter results in fcp exception`() { - parameters.putSingle("Sone", "InvalidSoneId") - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithInvalidSoneParameterResultsInFcpException() } @Test fun `request with valid remote Sone parameter results in fcp exception`() { - parameters.putSingle("Sone", "RemoteSoneId") - whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone)) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithValidRemoteSoneParameterResultsInFcpException() } @Test fun `request without text results in fcp exception`() { parameters.putSingle("Sone", "LocalSoneId") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + whenever(core.getSone("LocalSoneId")).thenReturn(Optional.of(localSone)) expectedException.expect(FcpException::class.java) command.execute(parameters, null, null) } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt new file mode 100644 index 0000000..b2fb4ae --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -0,0 +1,66 @@ +package net.pterodactylus.sone.fcp + +import com.google.common.base.Optional +import com.google.common.base.Optional.absent +import freenet.support.SimpleFieldSet +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.freenet.fcp.Command +import net.pterodactylus.sone.freenet.fcp.FcpException +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.ExpectedException +import org.mockito.ArgumentMatchers +import org.mockito.ArgumentMatchers.anyString + +/** + * TODO + */ +abstract class SoneCommandTest { + + @Rule @JvmField val expectedException = ExpectedException.none()!! + + protected val core = mock() + protected val command: AbstractSoneCommand by lazy { createCommand(core) } + + protected val parameters = SimpleFieldSet(true) + protected val localSone = mock().apply { + whenever(isLocal).thenReturn(true) + } + protected val remoteSone = mock() + + protected abstract fun createCommand(core: Core): AbstractSoneCommand + + @Before + fun setupCore() { + whenever(core.getSone(anyString())).thenReturn(absent()) + } + + protected fun requestWithoutAnyParameterResultsInFcpException() { + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + protected fun requestWithEmptySoneParameterResultsInFcpException() { + parameters.putSingle("Sone", null) + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + protected fun requestWithInvalidSoneParameterResultsInFcpException() { + parameters.putSingle("Sone", "InvalidSoneId") + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + fun requestWithValidRemoteSoneParameterResultsInFcpException() { + parameters.putSingle("Sone", "RemoteSoneId") + whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone)) + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + +} -- 2.7.4