X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FSoneCommandTest.kt;h=e6ba1ea6856337960d2d5fdb16e1345df24fb58f;hb=290f54ab871fd8267cdecab78cffd0f79cedd39a;hp=b02aa9e53015fcb2875d7f029e23476cc3fcd5b9;hpb=6cdcdf75cbc0b5341705f55f7b6a33073405732b;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index b02aa9e..e6ba1ea 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -3,9 +3,14 @@ package net.pterodactylus.sone.fcp import com.google.common.base.Optional import com.google.common.base.Optional.absent import freenet.support.SimpleFieldSet +import net.pterodactylus.sone.OneByOneMatcher import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.Profile import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException +import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import org.junit.Before @@ -38,14 +43,40 @@ abstract class SoneCommandTest { whenever(core.getPostReply(anyString())).thenReturn(absent()) } + protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock().apply { + whenever(this.id).thenReturn(id) + whenever(this.name).thenReturn(name) + whenever(profile).thenReturn(Profile(this).apply { + this.firstName = firstName + this.lastName = lastName + }) + whenever(this.time).thenReturn(time) + } + + protected fun createPost(id: String, sone: Sone, recipientId: String?, time: Long, text: String) = mock().apply { + whenever(this.id).thenReturn(id) + whenever(this.sone).thenReturn(sone) + whenever(this.recipientId).thenReturn(recipientId.asOptional()) + whenever(this.time).thenReturn(time) + whenever(this.text).thenReturn(text) + } + + protected fun createReply(id: String, sone: Sone, post: Post, time: Long, text: String) = mock().apply { + whenever(this.id).thenReturn(id) + whenever(this.sone).thenReturn(sone) + whenever(this.post).thenReturn(post.asOptional()) + whenever(this.time).thenReturn(time) + whenever(this.text).thenReturn(text) + } + protected fun executeCommandAndExpectFcpException() { expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + command.execute(parameters) } protected fun requestWithoutAnyParameterResultsInFcpException() { expectedException.expect(FcpException::class.java) - command.execute(parameters, null, null) + command.execute(parameters) } protected fun requestWithEmptySoneParameterResultsInFcpException() { @@ -64,4 +95,26 @@ abstract class SoneCommandTest { executeCommandAndExpectFcpException() } + protected fun SimpleFieldSet.parsePost(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Recipient", "Time", "Text") + protected fun SimpleFieldSet.parseReply(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Time", "Text") + + private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String) = listOf(*fields) + .map { it to (get(prefix + it) as String?) } + .toMap() + + protected fun matchesPost(post: Post) = OneByOneMatcher>().apply { + expect("ID", post.id) { it["ID"] } + expect("Sone", post.sone.id) { it["Sone"] } + expect("recipient", post.recipientId.orNull()) { it["Recipient"] } + expect("time", post.time.toString()) { it["Time"] } + expect("text", post.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] } + } + + protected fun matchesReply(reply: PostReply) = OneByOneMatcher>().apply { + expect("ID", reply.id) { it["ID"] } + expect("Sone", reply.sone.id) { it["Sone"] } + expect("time", reply.time.toString()) { it["Time"] } + expect("text", reply.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] } + } + }