From 8775a0be01123cc2b3375cb1f3f61a260d29b1ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 8 Jan 2017 00:55:56 +0100 Subject: [PATCH] Add test for delete reply command and actually delete the reply --- .../pterodactylus/sone/fcp/DeleteReplyCommand.java | 1 + .../sone/fcp/DeleteReplyCommandTest.kt | 57 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.kt diff --git a/src/main/java/net/pterodactylus/sone/fcp/DeleteReplyCommand.java b/src/main/java/net/pterodactylus/sone/fcp/DeleteReplyCommand.java index 00a5b97..6a018fa 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/DeleteReplyCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/DeleteReplyCommand.java @@ -51,6 +51,7 @@ public class DeleteReplyCommand extends AbstractSoneCommand { if (!reply.getSone().isLocal()) { return new ErrorResponse(401, "Not allowed."); } + getCore().deleteReply(reply); return new Response("ReplyDeleted", new SimpleFieldSetBuilder().get()); } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.kt new file mode 100644 index 0000000..0819a60 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.kt @@ -0,0 +1,57 @@ +package net.pterodactylus.sone.fcp + +import com.google.common.base.Optional.of +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.PostReply +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.verify + +/** + * Unit test for [DeleteReplyCommand]. + */ +class DeleteReplyCommandTest : SoneCommandTest() { + + private val remotePostReply = mock().apply { whenever(sone).thenReturn(remoteSone) } + private val localPostReply = mock().apply { whenever(sone).thenReturn(localSone) } + + override fun createCommand(core: Core) = DeleteReplyCommand(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 invalid post reply parameter results in fcp exception`() { + parameters.putSingle("Reply", "InvalidReplyId") + executeCommandAndExpectFcpException() + } + + @Test + fun `request with remote post reply parameter results in error response`() { + parameters.putSingle("Reply", "RemoteReplyId") + whenever(core.getPostReply("RemoteReplyId")).thenReturn(of(remotePostReply)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("Error")) + assertThat(response.replyParameters["ErrorCode"], equalTo("401")) + } + + @Test + fun `request with local post reply parameter deletes reply`() { + parameters.putSingle("Reply", "RemoteReplyId") + whenever(core.getPostReply("RemoteReplyId")).thenReturn(of(localPostReply)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("ReplyDeleted")) + verify(core).deleteReply(localPostReply) + } + +} -- 2.7.4