package net.pterodactylus.sone.main
-import freenet.client.async.USKManager
-import freenet.l10n.BaseL10n.LANGUAGE.ENGLISH
-import freenet.node.Node
-import freenet.node.NodeClientCore
-import freenet.pluginmanager.PluginRespirator
+import com.google.inject.*
+import freenet.client.async.*
+import freenet.l10n.BaseL10n.LANGUAGE.*
+import freenet.node.*
+import freenet.pluginmanager.*
+import net.pterodactylus.sone.core.*
+import net.pterodactylus.sone.fcp.*
+import net.pterodactylus.sone.freenet.wot.*
import net.pterodactylus.sone.test.*
-import org.junit.Test
+import net.pterodactylus.sone.web.*
+import net.pterodactylus.sone.web.notification.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.mockito.Mockito.*
+import kotlin.test.*
/**
* Unit test for [SonePlugin].
*/
+@Dirty
class SonePluginTest {
- private val sonePlugin = SonePlugin()
+ private val sonePlugin by lazy { SonePlugin { injector } }
private val pluginRespirator = deepMock<PluginRespirator>()
private val node = deepMock<Node>()
private val clientCore = deepMock<NodeClientCore>()
sonePlugin.runPlugin(pluginRespirator)
}
+ @Test
+ fun `core can be created`() {
+ val injector: Injector = runSonePluginWithRealInjector()
+ assertThat(injector.getInstance<Core>(), notNullValue())
+ }
+
+ @Test
+ fun `fcp interface can be created`() {
+ val injector: Injector = runSonePluginWithRealInjector()
+ assertThat(injector.getInstance<FcpInterface>(), notNullValue())
+ }
+
+ @Test
+ fun `web interface can be created`() {
+ val injector: Injector = runSonePluginWithRealInjector()
+ assertThat(injector.getInstance<WebInterface>(), notNullValue())
+ }
+
+ @Test
+ fun `web of trust connector can be created`() {
+ val injector: Injector = runSonePluginWithRealInjector()
+ assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
+ }
+
+ @Test
+ fun `notification handler can be created`() {
+ val injector: Injector = runSonePluginWithRealInjector()
+ assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
+ }
+
+ private fun runSonePluginWithRealInjector(): Injector {
+ lateinit var injector: Injector
+ val sonePlugin = SonePlugin {
+ Guice.createInjector(*it).also {
+ injector = it
+ }
+ }
+ sonePlugin.setLanguage(ENGLISH)
+ sonePlugin.runPlugin(pluginRespirator)
+ return injector
+ }
+
+ @Test
+ fun `core is being started`() {
+ sonePlugin.runPlugin(pluginRespirator)
+ val core = injector.getInstance<Core>()
+ verify(core).start()
+ }
+
+ @Test
+ fun `notification handler is being requested`() {
+ sonePlugin.runPlugin(pluginRespirator)
+ assertThat(getInjected(NotificationHandler::class.java), notNullValue())
+ }
+
+ private fun <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
+ injected[TypeLiteral.get(clazz) to annotation] as? T
+
+ private val injected =
+ mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
+
+ private val injector = mock<Injector>().apply {
+ fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz)
+ whenever(getInstance(any<Key<*>>())).then {
+ injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) {
+ it.getArgument<Key<*>>(0).typeLiteral.type.typeName.toClass().let(::mockValue)
+ }
+ }
+ whenever(getInstance(any<Class<*>>())).then {
+ injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) {
+ it.getArgument<Class<*>>(0).let(::mockValue)
+ }
+ }
+ }
+
}
+
+private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)