Add unit test for create reply command
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 21:22:55 +0000 (22:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 7 Jan 2017 21:22:55 +0000 (22:22 +0100)
src/test/kotlin/net/pterodactylus/sone/fcp/CreateReplyCommandTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.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 (file)
index 0000000..6d39083
--- /dev/null
@@ -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<Post>()
+
+       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<PostReply>().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"))
+       }
+
+}
index b2fb4ae..2f2de5c 100644 (file)
@@ -37,6 +37,7 @@ abstract class SoneCommandTest {
        @Before
        fun setupCore() {
                whenever(core.getSone(anyString())).thenReturn(absent())
        @Before
        fun setupCore() {
                whenever(core.getSone(anyString())).thenReturn(absent())
+               whenever(core.getPost(anyString())).thenReturn(absent())
        }
 
        protected fun requestWithoutAnyParameterResultsInFcpException() {
        }
 
        protected fun requestWithoutAnyParameterResultsInFcpException() {