From 10d9eb5fcea8440b9ea06a3732678b286090934f 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:49:49 +0100 Subject: [PATCH] Add unit test for delete post command and actually delete the post --- .../pterodactylus/sone/fcp/DeletePostCommand.java | 1 + .../sone/fcp/DeletePostCommandTest.kt | 59 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt diff --git a/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java index e68cbb5..3cfb102 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java @@ -51,6 +51,7 @@ public class DeletePostCommand extends AbstractSoneCommand { if (!post.getSone().isLocal()) { return new ErrorResponse(401, "Not allowed."); } + getCore().deletePost(post); return new Response("PostDeleted", new SimpleFieldSetBuilder().get()); } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt new file mode 100644 index 0000000..4a23602 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt @@ -0,0 +1,59 @@ +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.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 +import org.mockito.Mockito +import org.mockito.Mockito.verify + +/** + * Unit test for [DeletePostCommand]. + */ +class DeletePostCommandTest : SoneCommandTest() { + + private val postFromRemoteSone = mock().apply { whenever(sone).thenReturn(remoteSone) } + private val postFromLocalSone = mock().apply { whenever(sone).thenReturn(localSone) } + override fun createCommand(core: Core) = DeletePostCommand(core) + + @Test + fun `command requires write access`() { + assertThat(command.requiresWriteAccess(), equalTo(true)) + } + + @Test + fun `request without any parameter results in fcp exception`() { + requestWithoutAnyParameterResultsInFcpException() + } + + @Test + fun `request with invalid post parameter results in fcp exception`() { + parameters.putSingle("Post", "InvalidPostId") + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + @Test + fun `request with post from remote sone returns error response`() { + parameters.putSingle("Post", "RemotePostId") + whenever(core.getPost("RemotePostId")).thenReturn(of(postFromRemoteSone)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("Error")) + assertThat(response.replyParameters["ErrorCode"], equalTo("401")) + } + + @Test + fun `request with post from local sone deletes posts`() { + parameters.putSingle("Post", "LocalPostId") + whenever(core.getPost("LocalPostId")).thenReturn(of(postFromLocalSone)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("PostDeleted")) + verify(core).deletePost(postFromLocalSone) + } + +} -- 2.7.4