1 package net.pterodactylus.sone.fcp
3 import freenet.support.*
4 import net.pterodactylus.sone.core.*
5 import net.pterodactylus.sone.data.*
6 import net.pterodactylus.sone.freenet.fcp.*
7 import net.pterodactylus.sone.template.*
8 import net.pterodactylus.sone.test.*
9 import net.pterodactylus.sone.utils.*
11 import org.junit.rules.*
12 import org.mockito.ArgumentMatchers.*
15 * Base class for Sone FCP command tests.
17 abstract class SoneCommandTest {
19 @Rule @JvmField val expectedException = ExpectedException.none()!!
21 protected val core = mock<Core>()
22 protected val command: AbstractSoneCommand by lazy { createCommand(core) }
24 protected val parameters = SimpleFieldSet(true)
25 protected val localSone = mock<Sone>().apply {
26 whenever(isLocal).thenReturn(true)
28 protected val remoteSone = mock<Sone>()
30 protected abstract fun createCommand(core: Core): AbstractSoneCommand
34 whenever(core.getSone(anyString())).thenReturn(null)
35 whenever(core.getPost(anyString())).thenReturn(null)
36 whenever(core.getPostReply(anyString())).thenReturn(null)
39 protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock<Sone>().apply {
40 whenever(this.id).thenReturn(id)
41 whenever(this.name).thenReturn(name)
42 whenever(profile).thenReturn(Profile(this).apply {
43 this.firstName = firstName
44 this.lastName = lastName
46 whenever(this.time).thenReturn(time)
49 protected fun createPost(id: String, sone: Sone, recipientId: String?, time: Long, text: String) = mock<Post>().apply {
50 whenever(this.id).thenReturn(id)
51 whenever(this.sone).thenReturn(sone)
52 whenever(this.recipientId).thenReturn(recipientId.asOptional())
53 whenever(this.time).thenReturn(time)
54 whenever(this.text).thenReturn(text)
57 protected fun createReply(id: String, sone: Sone, post: Post, time: Long, text: String) = mock<PostReply>().apply {
58 whenever(this.id).thenReturn(id)
59 whenever(this.sone).thenReturn(sone)
60 whenever(this.post).thenReturn(post.asOptional())
61 whenever(this.time).thenReturn(time)
62 whenever(this.text).thenReturn(text)
65 protected fun executeCommandAndExpectFcpException() {
66 expectedException.expect(FcpException::class.java)
67 command.execute(parameters)
70 protected fun requestWithoutAnyParameterResultsInFcpException() {
71 expectedException.expect(FcpException::class.java)
72 command.execute(parameters)
75 protected fun requestWithEmptySoneParameterResultsInFcpException() {
76 parameters += "Sone" to null
77 executeCommandAndExpectFcpException()
80 protected fun requestWithInvalidSoneParameterResultsInFcpException() {
81 parameters += "Sone" to "InvalidSoneId"
82 executeCommandAndExpectFcpException()
85 fun requestWithValidRemoteSoneParameterResultsInFcpException() {
86 parameters += "Sone" to "RemoteSoneId"
87 whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
88 executeCommandAndExpectFcpException()
91 protected operator fun SimpleFieldSet.plusAssign(keyValue: Pair<String, String?>) = putSingle(keyValue.first, keyValue.second)
92 protected fun SimpleFieldSet.parsePost(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Recipient", "Time", "Text")
93 protected fun SimpleFieldSet.parseReply(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Time", "Text")
94 protected fun SimpleFieldSet.parseSone(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Name", "NiceName", "LastUpdated", "Followed") +
95 (0 until this["${prefix}Field.Count"].toInt()).map {
96 ("Field." + this["${prefix}Field.$it.Name"]) to this["${prefix}Field.$it.Value"]
99 private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String): Map<String, String?> = fields
100 .associate { it to get(prefix + it) }
102 protected fun matchesPost(post: Post) = OneByOneMatcher<Map<String, String?>>().apply {
103 expect("ID", post.id) { it["ID"] }
104 expect("Sone", post.sone.id) { it["Sone"] }
105 expect("recipient", post.recipientId.orNull()) { it["Recipient"] }
106 expect("time", post.time.toString()) { it["Time"] }
107 expect("text", post.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] }
110 protected fun matchesReply(reply: PostReply) = OneByOneMatcher<Map<String, String?>>().apply {
111 expect("ID", reply.id) { it["ID"] }
112 expect("Sone", reply.sone.id) { it["Sone"] }
113 expect("time", reply.time.toString()) { it["Time"] }
114 expect("text", reply.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] }
117 protected fun matchesSone(sone: Sone) = OneByOneMatcher<Map<String, String?>>().apply {
118 expect("ID", sone.id) { it["ID"] }
119 expect("name", sone.name) { it["Name"] }
120 expect("last updated", sone.time.toString()) { it["LastUpdated"] }
121 expect("nice name", SoneAccessor.getNiceName(sone)) { it["NiceName"] }
122 sone.profile.fields.forEach { field ->
123 expect("field: ${field.name}", field.value) { it["Field.${field.name}"] }