Add unit test for delete post command and actually delete the post
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 21:49:49 +0000 (22:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 21:49:49 +0000 (22:49 +0100)
src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java
src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt [new file with mode: 0644]

index e68cbb5..3cfb102 100644 (file)
@@ -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 (file)
index 0000000..4a23602
--- /dev/null
@@ -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<Post>().apply { whenever(sone).thenReturn(remoteSone) }
+       private val postFromLocalSone = mock<Post>().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)
+       }
+
+}