X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FSoneCommandTest.kt;h=91eabf54841d8bf9ca29a83738192d1530b978a2;hp=8344531b587a6f4320083f96bff69b57293fde4f;hb=d14188d87056cfd63490ef9f16f4aae0c1864864;hpb=5684bc4813ffdbe26af73cb3cf6a1f62abbadedb diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index 8344531..91eabf5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -1,18 +1,15 @@ 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.core.Core -import net.pterodactylus.sone.data.Profile -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.junit.Before -import org.junit.Rule -import org.junit.rules.ExpectedException -import org.mockito.ArgumentMatchers.anyString +import freenet.support.* +import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.freenet.fcp.* +import net.pterodactylus.sone.template.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.utils.* +import org.junit.* +import org.junit.rules.* +import org.mockito.ArgumentMatchers.* /** * Base class for Sone FCP command tests. @@ -34,9 +31,9 @@ abstract class SoneCommandTest { @Before fun setupCore() { - whenever(core.getSone(anyString())).thenReturn(absent()) - whenever(core.getPost(anyString())).thenReturn(absent()) - whenever(core.getPostReply(anyString())).thenReturn(absent()) + whenever(core.getSone(anyString())).thenReturn(null) + whenever(core.getPost(anyString())).thenReturn(null) + whenever(core.getPostReply(anyString())).thenReturn(null) } protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock().apply { @@ -49,6 +46,22 @@ abstract class SoneCommandTest { 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) @@ -60,19 +73,55 @@ abstract class SoneCommandTest { } protected fun requestWithEmptySoneParameterResultsInFcpException() { - parameters.putSingle("Sone", null) + parameters += "Sone" to null executeCommandAndExpectFcpException() } protected fun requestWithInvalidSoneParameterResultsInFcpException() { - parameters.putSingle("Sone", "InvalidSoneId") + parameters += "Sone" to "InvalidSoneId" executeCommandAndExpectFcpException() } fun requestWithValidRemoteSoneParameterResultsInFcpException() { - parameters.putSingle("Sone", "RemoteSoneId") - whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone)) + parameters += "Sone" to "RemoteSoneId" + whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) executeCommandAndExpectFcpException() } + protected operator fun SimpleFieldSet.plusAssign(keyValue: Pair) = putSingle(keyValue.first, keyValue.second) + 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") + protected fun SimpleFieldSet.parseSone(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Name", "NiceName", "LastUpdated", "Followed") + + (0 until this["${prefix}Field.Count"].toInt()).map { + ("Field." + this["${prefix}Field.$it.Name"]) to this["${prefix}Field.$it.Value"] + } + + private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String): Map = fields + .associate { it to get(prefix + it) } + + 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"] } + } + + protected fun matchesSone(sone: Sone) = OneByOneMatcher>().apply { + expect("ID", sone.id) { it["ID"] } + expect("name", sone.name) { it["Name"] } + expect("last updated", sone.time.toString()) { it["LastUpdated"] } + expect("nice name", SoneAccessor.getNiceName(sone)) { it["NiceName"] } + sone.profile.fields.forEach { field -> + expect("field: ${field.name}", field.value) { it["Field.${field.name}"] } + } + } + }