X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fplugin%2FFredPluginConnectorTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fplugin%2FFredPluginConnectorTest.kt;h=1eaa91aea2512f529df75c425d30c596668f470d;hp=0000000000000000000000000000000000000000;hb=d50730f6a330439e0e7ef97ca9329dffe72d5640;hpb=97fe04482ebb8a08e43294acde041c2975cbd8ee diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnectorTest.kt new file mode 100644 index 0000000..1eaa91a --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnectorTest.kt @@ -0,0 +1,77 @@ +/** + * Sone - FredPluginConnectorTest.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* Fred-based plugin stuff is mostly deprecated. ¯\_(ツ)_/¯ */ +@file:Suppress("DEPRECATION") + +package net.pterodactylus.sone.freenet.plugin + +import freenet.pluginmanager.* +import freenet.support.* +import freenet.support.api.* +import freenet.support.io.* +import kotlinx.coroutines.* +import net.pterodactylus.sone.freenet.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.junit.rules.* +import kotlin.concurrent.* + +class FredPluginConnectorTest { + + @Rule + @JvmField + val expectedException = ExpectedException.none()!! + + @Test + fun `connector throws exception if plugin can not be found`() = runBlocking { + val pluginConnector = FredPluginConnector(pluginRespiratorFacade) + expectedException.expect(PluginException::class.java) + pluginConnector.sendRequest("wrong.plugin", requestFields, requestData) + Unit + } + + @Test + fun `connector returns correct fields and data`() = runBlocking { + val pluginConnector = FredPluginConnector(pluginRespiratorFacade) + val reply = pluginConnector.sendRequest("test.plugin", requestFields, requestData) + assertThat(reply.fields, equalTo(responseFields)) + assertThat(reply.data, equalTo(responseData)) + } + +} + +private val requestFields = SimpleFieldSetBuilder().put("foo", "bar").get() +private val requestData: Bucket? = ArrayBucket(byteArrayOf(1, 2)) +private val responseFields = SimpleFieldSetBuilder().put("baz", "quo").get() +private val responseData: Bucket? = ArrayBucket(byteArrayOf(3, 4)) + +private val pluginRespiratorFacade = object : PluginRespiratorFacade { + override fun getPluginTalker(pluginTalker: FredPluginTalker, pluginName: String, identifier: String) = + if (pluginName == "test.plugin") { + object : PluginTalkerFacade { + override fun send(pluginParameters: SimpleFieldSet, data: Bucket?) { + if ((pluginParameters == requestFields) && (data == requestData)) { + thread { pluginTalker.onReply(pluginName, identifier, responseFields, responseData) } + } + } + } + } else { + throw PluginNotFoundException() + } +}