Optimize some imports
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / SoneCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
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.*
10 import org.junit.*
11 import org.junit.rules.*
12 import org.mockito.ArgumentMatchers.*
13
14 /**
15  * Base class for Sone FCP command tests.
16  */
17 abstract class SoneCommandTest {
18
19         @Rule @JvmField val expectedException = ExpectedException.none()!!
20
21         protected val core = mock<Core>()
22         protected val command: AbstractSoneCommand by lazy { createCommand(core) }
23
24         protected val parameters = SimpleFieldSet(true)
25         protected val localSone = mock<Sone>().apply {
26                 whenever(isLocal).thenReturn(true)
27         }
28         protected val remoteSone = mock<Sone>()
29
30         protected abstract fun createCommand(core: Core): AbstractSoneCommand
31
32         @Before
33         fun setupCore() {
34                 whenever(core.getSone(anyString())).thenReturn(null)
35                 whenever(core.getPost(anyString())).thenReturn(null)
36                 whenever(core.getPostReply(anyString())).thenReturn(null)
37         }
38
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
45                 })
46                 whenever(this.time).thenReturn(time)
47         }
48
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)
55         }
56
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)
63         }
64
65         protected fun executeCommandAndExpectFcpException() {
66                 expectedException.expect(FcpException::class.java)
67                 command.execute(parameters)
68         }
69
70         protected fun requestWithoutAnyParameterResultsInFcpException() {
71                 expectedException.expect(FcpException::class.java)
72                 command.execute(parameters)
73         }
74
75         protected fun requestWithEmptySoneParameterResultsInFcpException() {
76                 parameters += "Sone" to null
77                 executeCommandAndExpectFcpException()
78         }
79
80         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
81                 parameters += "Sone" to "InvalidSoneId"
82                 executeCommandAndExpectFcpException()
83         }
84
85         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
86                 parameters += "Sone" to "RemoteSoneId"
87                 whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
88                 executeCommandAndExpectFcpException()
89         }
90
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"]
97                         }
98
99         private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String): Map<String, String?> = fields
100                         .associate { it to get(prefix + it) }
101
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"] }
108         }
109
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"] }
115         }
116
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}"] }
124                 }
125         }
126
127 }