1 package net.pterodactylus.sone.fcp
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
18 import org.junit.rules.ExpectedException
19 import org.mockito.ArgumentMatchers.anyString
22 * Base class for Sone FCP command tests.
24 abstract class SoneCommandTest {
26 @Rule @JvmField val expectedException = ExpectedException.none()!!
28 protected val core = mock<Core>()
29 protected val command: AbstractSoneCommand by lazy { createCommand(core) }
31 protected val parameters = SimpleFieldSet(true)
32 protected val localSone = mock<Sone>().apply {
33 whenever(isLocal).thenReturn(true)
35 protected val remoteSone = mock<Sone>()
37 protected abstract fun createCommand(core: Core): AbstractSoneCommand
41 whenever(core.getSone(anyString())).thenReturn(absent())
42 whenever(core.getPost(anyString())).thenReturn(absent())
43 whenever(core.getPostReply(anyString())).thenReturn(absent())
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
53 whenever(this.time).thenReturn(time)
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)
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)
72 protected fun executeCommandAndExpectFcpException() {
73 expectedException.expect(FcpException::class.java)
74 command.execute(parameters)
77 protected fun requestWithoutAnyParameterResultsInFcpException() {
78 expectedException.expect(FcpException::class.java)
79 command.execute(parameters)
82 protected fun requestWithEmptySoneParameterResultsInFcpException() {
83 parameters += "Sone" to null
84 executeCommandAndExpectFcpException()
87 protected fun requestWithInvalidSoneParameterResultsInFcpException() {
88 parameters += "Sone" to "InvalidSoneId"
89 executeCommandAndExpectFcpException()
92 fun requestWithValidRemoteSoneParameterResultsInFcpException() {
93 parameters += "Sone" to "RemoteSoneId"
94 whenever(core.getSone("RemoteSoneId")).thenReturn(Optional.of(remoteSone))
95 executeCommandAndExpectFcpException()
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")
102 private fun SimpleFieldSet.parseFromSimpleFieldSet(prefix: String, vararg fields: String) = listOf(*fields)
103 .map { it to (get(prefix + it) as String?) }
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"] }
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"] }