Add unit test for get posts command
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / fcp / SoneCommandTest.kt
1 package net.pterodactylus.sone.fcp
2
3 import com.google.common.base.Optional
4 import com.google.common.base.Optional.absent
5 import freenet.support.SimpleFieldSet
6 import net.pterodactylus.sone.OneByOneMatcher
7 import net.pterodactylus.sone.core.Core
8 import net.pterodactylus.sone.data.Post
9 import net.pterodactylus.sone.data.PostReply
10 import net.pterodactylus.sone.data.Profile
11 import net.pterodactylus.sone.data.Sone
12 import net.pterodactylus.sone.freenet.fcp.FcpException
13 import net.pterodactylus.sone.test.asOptional
14 import net.pterodactylus.sone.test.mock
15 import net.pterodactylus.sone.test.whenever
16 import org.junit.Before
17 import org.junit.Rule
18 import org.junit.rules.ExpectedException
19 import org.mockito.ArgumentMatchers.anyString
20
21 /**
22  * Base class for Sone FCP command tests.
23  */
24 abstract class SoneCommandTest {
25
26         @Rule @JvmField val expectedException = ExpectedException.none()!!
27
28         protected val core = mock<Core>()
29         protected val command: AbstractSoneCommand by lazy { createCommand(core) }
30
31         protected val parameters = SimpleFieldSet(true)
32         protected val localSone = mock<Sone>().apply {
33                 whenever(isLocal).thenReturn(true)
34         }
35         protected val remoteSone = mock<Sone>()
36
37         protected abstract fun createCommand(core: Core): AbstractSoneCommand
38
39         @Before
40         fun setupCore() {
41                 whenever(core.getSone(anyString())).thenReturn(absent())
42                 whenever(core.getPost(anyString())).thenReturn(absent())
43                 whenever(core.getPostReply(anyString())).thenReturn(absent())
44         }
45
46         protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock<Sone>().apply {
47                 whenever(this.id).thenReturn(id)
48                 whenever(this.name).thenReturn(name)
49                 whenever(profile).thenReturn(Profile(this).apply {
50                         this.firstName = firstName
51                         this.lastName = lastName
52                 })
53                 whenever(this.time).thenReturn(time)
54         }
55
56         protected fun createPost(id: String, sone: Sone, recipientId: String?, time: Long, text: String) = mock<Post>().apply {
57                 whenever(this.id).thenReturn(id)
58                 whenever(this.sone).thenReturn(sone)
59                 whenever(this.recipientId).thenReturn(recipientId.asOptional())
60                 whenever(this.time).thenReturn(time)
61                 whenever(this.text).thenReturn(text)
62         }
63
64         protected fun createReply(id: String, sone: Sone, post: Post, time: Long, text: String) = mock<PostReply>().apply {
65                 whenever(this.id).thenReturn(id)
66                 whenever(this.sone).thenReturn(sone)
67                 whenever(this.post).thenReturn(post.asOptional())
68                 whenever(this.time).thenReturn(time)
69                 whenever(this.text).thenReturn(text)
70         }
71
72         protected fun executeCommandAndExpectFcpException() {
73                 expectedException.expect(FcpException::class.java)
74                 command.execute(parameters)
75         }
76
77         protected fun requestWithoutAnyParameterResultsInFcpException() {
78                 expectedException.expect(FcpException::class.java)
79                 command.execute(parameters)
80         }
81
82         protected fun requestWithEmptySoneParameterResultsInFcpException() {
83                 parameters.putSingle("Sone", null)
84                 executeCommandAndExpectFcpException()
85         }
86
87         protected fun requestWithInvalidSoneParameterResultsInFcpException() {
88                 parameters.putSingle("Sone", "InvalidSoneId")
89                 executeCommandAndExpectFcpException()
90         }
91
92         fun requestWithValidRemoteSoneParameterResultsInFcpException() {
93                 parameters.putSingle("Sone", "RemoteSoneId")
94                 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
95                 executeCommandAndExpectFcpException()
96         }
97
98         protected operator fun SimpleFieldSet.plusAssign(keyValue: Pair<String, String>) = putSingle(keyValue.first, keyValue.second)
99         protected fun SimpleFieldSet.parsePost(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Recipient", "Time", "Text")
100         protected fun SimpleFieldSet.parseReply(prefix: String) = parseFromSimpleFieldSet(prefix, "ID", "Sone", "Time", "Text")
101
102         private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String) = listOf(*fields)
103                         .map { it to (get(prefix + it) as String?) }
104                         .toMap()
105
106         protected fun matchesPost(post: Post) = OneByOneMatcher<Map<String, String?>>().apply {
107                 expect("ID", post.id) { it["ID"] }
108                 expect("Sone", post.sone.id) { it["Sone"] }
109                 expect("recipient", post.recipientId.orNull()) { it["Recipient"] }
110                 expect("time", post.time.toString()) { it["Time"] }
111                 expect("text", post.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] }
112         }
113
114         protected fun matchesReply(reply: PostReply) = OneByOneMatcher<Map<String, String?>>().apply {
115                 expect("ID", reply.id) { it["ID"] }
116                 expect("Sone", reply.sone.id) { it["Sone"] }
117                 expect("time", reply.time.toString()) { it["Time"] }
118                 expect("text", reply.text.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n")) { it["Text"] }
119         }
120
121 }