X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FCreatePostCommandTest.kt;h=cfbda94f94cec47a520a98a1922ee22e1e37bf47;hp=d7eeff86e7c085cb3a4f7c12bdcaa76c679f5720;hb=04709e23f38e9d447337682ba27201da5dc19bd9;hpb=b43aa662e0888130d40ce93fcc38df46563d33c3 diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt index d7eeff8..cfbda94 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt @@ -2,126 +2,96 @@ package net.pterodactylus.sone.fcp import com.google.common.base.Optional.absent import com.google.common.base.Optional.of -import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Post -import net.pterodactylus.sone.data.Sone -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.hamcrest.Matchers.notNullValue -import org.junit.Before -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException -import org.mockito.ArgumentMatchers.anyString /** * Unit test for [CreatePostCommand]. */ -class CreatePostCommandTest { +class CreatePostCommandTest : SoneCommandTest() { - @Rule @JvmField val expectedException = ExpectedException.none() - private val core = mock() - private val command = CreatePostCommand(core) - - private val parameters = SimpleFieldSet(true) - private val localSone = mock().apply { - whenever(isLocal).thenReturn(true) - } - private val remoteSone = mock() - - @Before - fun setupCore() { - whenever(core.getSone(anyString())).thenReturn(absent()) - } + override fun createCommand(core: Core) = CreatePostCommand(core) @Test fun `command requires write access`() { - assertThat(command.requiresWriteAccess(), equalTo(true)) + assertThat(command.requiresWriteAccess, equalTo(true)) } @Test fun `request without any parameters results in fcp exception`() { - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithoutAnyParameterResultsInFcpException() } @Test - fun `request with empty sone parameter results in fcp exception`() { - parameters.putSingle("Sone", null) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + fun `request with empty Sone parameter results in fcp exception`() { + requestWithEmptySoneParameterResultsInFcpException() } @Test fun `request with invalid Sone parameter results in fcp exception`() { - parameters.putSingle("Sone", "InvalidSoneId") - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithInvalidSoneParameterResultsInFcpException() } @Test fun `request with valid remote Sone parameter results in fcp exception`() { - parameters.putSingle("Sone", "RemoteSoneId") - whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone)) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + requestWithValidRemoteSoneParameterResultsInFcpException() } @Test fun `request without text results in fcp exception`() { - parameters.putSingle("Sone", "LocalSoneId") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + parameters += "Sone" to "LocalSoneId" + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) + executeCommandAndExpectFcpException() } @Test fun `request with text creates post`() { - parameters.putSingle("Sone", "LocalSoneId") - parameters.putSingle("Text", "Test") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) + parameters += "Sone" to "LocalSoneId" + parameters += "Text" to "Test" + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) val post = mock().apply { whenever(id).thenReturn("PostId") } - whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post) - val response = command.execute(parameters, null, null) + whenever(core.createPost(localSone, null, "Test")).thenReturn(post) + val response = command.execute(parameters) assertThat(response.replyParameters.get("Message"), equalTo("PostCreated")) assertThat(response.replyParameters.get("Post"), equalTo("PostId")) } @Test fun `request with invalid recipient results in fcp exception`() { - parameters.putSingle("Sone", "LocalSoneId") - parameters.putSingle("Text", "Test") - parameters.putSingle("Recipient", "InvalidSoneId") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) - expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + parameters += "Sone" to "LocalSoneId" + parameters += "Text" to "Test" + parameters += "Recipient" to "InvalidSoneId" + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) + executeCommandAndExpectFcpException() } @Test fun `request with recipient the same as the sender returns an error response`() { - parameters.putSingle("Sone", "LocalSoneId") - parameters.putSingle("Text", "Test") - parameters.putSingle("Recipient", "LocalSoneId") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) - val response = command.execute(parameters, null, null) + parameters += "Sone" to "LocalSoneId" + parameters += "Text" to "Test" + parameters += "Recipient" to "LocalSoneId" + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) + val response = command.execute(parameters) assertThat(response.replyParameters["Message"], equalTo("Error")) assertThat(response.replyParameters["ErrorMessage"], notNullValue()) } @Test fun `request with text and recipient creates post`() { - parameters.putSingle("Sone", "LocalSoneId") - parameters.putSingle("Text", "Test") - parameters.putSingle("Recipient", "RemoteSoneId") - whenever(core.getSone("LocalSoneId")).thenReturn(of(localSone)) - whenever(core.getSone("RemoteSoneId")).thenReturn(of(remoteSone)) + parameters += "Sone" to "LocalSoneId" + parameters += "Text" to "Test" + parameters += "Recipient" to "RemoteSoneId" + whenever(core.getSone("LocalSoneId")).thenReturn(localSone) + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) val post = mock().apply { whenever(id).thenReturn("PostId") } - whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post) - val response = command.execute(parameters, null, null) + whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post) + val response = command.execute(parameters) assertThat(response.replyParameters.get("Message"), equalTo("PostCreated")) assertThat(response.replyParameters.get("Post"), equalTo("PostId")) }