2 * Sone - FredPluginConnectorTest.kt - Copyright © 2019 David ‘Bombe’ Roden
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.
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.
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/>.
18 /* Fred-based plugin stuff is mostly deprecated. ¯\_(ツ)_/¯ */
19 @file:Suppress("DEPRECATION")
21 package net.pterodactylus.sone.freenet.plugin
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.*
32 import org.junit.rules.*
33 import kotlin.concurrent.*
35 class FredPluginConnectorTest {
39 val expectedException = ExpectedException.none()!!
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)
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))
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))
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) }
75 throw PluginNotFoundException()