1eaa91aea2512f529df75c425d30c596668f470d
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / plugin / FredPluginConnectorTest.kt
1 /**
2  * Sone - FredPluginConnectorTest.kt - Copyright © 2019 David ‘Bombe’ Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /* Fred-based plugin stuff is mostly deprecated. ¯\_(ツ)_/¯ */
19 @file:Suppress("DEPRECATION")
20
21 package net.pterodactylus.sone.freenet.plugin
22
23 import freenet.pluginmanager.*
24 import freenet.support.*
25 import freenet.support.api.*
26 import freenet.support.io.*
27 import kotlinx.coroutines.*
28 import net.pterodactylus.sone.freenet.*
29 import org.hamcrest.MatcherAssert.*
30 import org.hamcrest.Matchers.*
31 import org.junit.*
32 import org.junit.rules.*
33 import kotlin.concurrent.*
34
35 class FredPluginConnectorTest {
36
37         @Rule
38         @JvmField
39         val expectedException = ExpectedException.none()!!
40
41         @Test
42         fun `connector throws exception if plugin can not be found`() = runBlocking {
43                 val pluginConnector = FredPluginConnector(pluginRespiratorFacade)
44                 expectedException.expect(PluginException::class.java)
45                 pluginConnector.sendRequest("wrong.plugin", requestFields, requestData)
46                 Unit
47         }
48
49         @Test
50         fun `connector returns correct fields and data`() = runBlocking {
51                 val pluginConnector = FredPluginConnector(pluginRespiratorFacade)
52                 val reply = pluginConnector.sendRequest("test.plugin", requestFields, requestData)
53                 assertThat(reply.fields, equalTo(responseFields))
54                 assertThat(reply.data, equalTo(responseData))
55         }
56
57 }
58
59 private val requestFields = SimpleFieldSetBuilder().put("foo", "bar").get()
60 private val requestData: Bucket? = ArrayBucket(byteArrayOf(1, 2))
61 private val responseFields = SimpleFieldSetBuilder().put("baz", "quo").get()
62 private val responseData: Bucket? = ArrayBucket(byteArrayOf(3, 4))
63
64 private val pluginRespiratorFacade = object : PluginRespiratorFacade {
65         override fun getPluginTalker(pluginTalker: FredPluginTalker, pluginName: String, identifier: String) =
66                         if (pluginName == "test.plugin") {
67                                 object : PluginTalkerFacade {
68                                         override fun send(pluginParameters: SimpleFieldSet, data: Bucket?) {
69                                                 if ((pluginParameters == requestFields) && (data == requestData)) {
70                                                         thread { pluginTalker.onReply(pluginName, identifier, responseFields, responseData) }
71                                                 }
72                                         }
73                                 }
74                         } else {
75                                 throw PluginNotFoundException()
76                         }
77 }