From 92adeef899fe2798c0952a6585463448cbf56c94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 7 Jan 2017 22:22:55 +0100 Subject: [PATCH] Add unit test for create reply command --- .../sone/fcp/CreateReplyCommandTest.kt | 93 ++++++++++++++++++++++ .../net/pterodactylus/sone/fcp/SoneCommandTest.kt | 1 + 2 files changed, 94 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt new file mode 100644 index 0000000..6d39083 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt @@ -0,0 +1,93 @@ +package net.pterodactylus.sone.fcp + +import com.google.common.base.Optional.of +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +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.junit.Test + +/** + * Unit test for [CreateReplyCommand]. + */ +class CreateReplyCommandTest : SoneCommandTest() { + + private val post = mock() + + override fun createCommand(core: Core) = CreateReplyCommand(core) + + @Test + fun `command requires write access`() { + assertThat(command.requiresWriteAccess(), equalTo(true)) + } + + @Test + fun `request without any parameters results in fcp exception`() { + requestWithoutAnyParameterResultsInFcpException() + } + + @Test + fun `request with empty Sone parameter results in fcp exception`() { + requestWithEmptySoneParameterResultsInFcpException() + } + + @Test + fun `request with invalid Sone parameter results in fcp exception`() { + requestWithInvalidSoneParameterResultsInFcpException() + } + + @Test + fun `request with valid remote Sone parameter results in fcp exception`() { + requestWithValidRemoteSoneParameterResultsInFcpException() + } + + private fun addValidLocalSoneParameter() { + parameters.putSingle("Sone", "LocalSoneId") + whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + } + + @Test + fun `request without post parameter results in fcp exception`() { + addValidLocalSoneParameter() + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + @Test + fun `request with invalid post parameter results in fcp exception`() { + addValidLocalSoneParameter() + parameters.putSingle("Post", "InvalidPostId") + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + private fun addValidPostParameter() { + parameters.putSingle("Post", "ValidPostId") + whenever(core.getPost("ValidPostId")).thenReturn(of(post)) + } + + @Test + fun `request without text results in fcp exception`() { + addValidLocalSoneParameter() + addValidPostParameter() + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + @Test + fun `complete request creates reply`() { + addValidLocalSoneParameter() + addValidPostParameter() + parameters.putSingle("Text", "Test") + val postReply = mock().apply { whenever(id).thenReturn("ReplyId") } + whenever(core.createReply(localSone, post, "Test")).thenReturn(postReply) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("ReplyCreated")) + assertThat(response.replyParameters["Reply"], equalTo("ReplyId")) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index b2fb4ae..2f2de5c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -37,6 +37,7 @@ abstract class SoneCommandTest { @Before fun setupCore() { whenever(core.getSone(anyString())).thenReturn(absent()) + whenever(core.getPost(anyString())).thenReturn(absent()) } protected fun requestWithoutAnyParameterResultsInFcpException() { -- 2.7.4