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.template.SoneAccessor
+import net.pterodactylus.sone.test.OneByOneMatcher
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
import org.junit.Before
import org.junit.Rule
import org.junit.rules.ExpectedException
@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<Sone>().apply {
}
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<String, String?>) = 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) = listOf(*fields)
- .map { it to (get(prefix + it) as String?) }
- .toMap()
+ private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String): Map<String, String?> = fields
+ .associate { it to get(prefix + it) }
protected fun matchesPost(post: Post) = OneByOneMatcher<Map<String, String?>>().apply {
expect("ID", post.id) { it["ID"] }
expect("text", reply.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] }
}
+ protected fun matchesSone(sone: Sone) = OneByOneMatcher<Map<String, String?>>().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}"] }
+ }
+ }
+
}