Add test for delete reply command and actually delete the reply
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 23:55:56 +0000 (00:55 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 23:55:56 +0000 (00:55 +0100)
src/main/java/net/pterodactylus/sone/fcp/DeleteReplyCommand.java
src/test/kotlin/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.kt [new file with mode: 0644]

index 00a5b97..6a018fa 100644 (file)
@@ -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 (file)
index 0000000..0819a60
--- /dev/null
@@ -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<PostReply>().apply { whenever(sone).thenReturn(remoteSone) }
+       private val localPostReply = mock<PostReply>().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)
+       }
+
+}