1 package net.pterodactylus.sone.main
3 import com.google.inject.*
4 import freenet.client.async.*
5 import freenet.l10n.BaseL10n.LANGUAGE.*
7 import freenet.pluginmanager.*
8 import net.pterodactylus.sone.core.*
9 import net.pterodactylus.sone.fcp.*
10 import net.pterodactylus.sone.freenet.wot.*
11 import net.pterodactylus.sone.test.*
12 import net.pterodactylus.sone.web.*
13 import net.pterodactylus.sone.web.notification.*
14 import org.hamcrest.MatcherAssert.*
15 import org.hamcrest.Matchers.*
16 import org.mockito.Mockito.*
20 * Unit test for [SonePlugin].
23 class SonePluginTest {
25 private var injector = mockInjector()
26 private val sonePlugin by lazy { SonePlugin { injector } }
27 private val pluginRespirator = deepMock<PluginRespirator>()
28 private val node = deepMock<Node>()
29 private val clientCore = deepMock<NodeClientCore>()
30 private val uskManager = deepMock<USKManager>()
33 setField(node, "clientCore", clientCore)
34 whenever(pluginRespirator.node).thenReturn(node)
35 setField(clientCore, "uskManager", uskManager)
39 fun `sone plugin can be started`() {
40 sonePlugin.setLanguage(ENGLISH)
41 sonePlugin.runPlugin(pluginRespirator)
45 fun `core can be created`() {
46 val injector: Injector = runSonePluginWithRealInjector()
47 assertThat(injector.getInstance<Core>(), notNullValue())
51 fun `fcp interface can be created`() {
52 val injector: Injector = runSonePluginWithRealInjector()
53 assertThat(injector.getInstance<FcpInterface>(), notNullValue())
57 fun `web interface can be created`() {
58 val injector: Injector = runSonePluginWithRealInjector()
59 assertThat(injector.getInstance<WebInterface>(), notNullValue())
63 fun `web of trust connector can be created`() {
64 val injector: Injector = runSonePluginWithRealInjector()
65 assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
69 fun `notification handler can be created`() {
70 val injector: Injector = runSonePluginWithRealInjector()
71 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
74 private fun runSonePluginWithRealInjector(): Injector {
75 lateinit var injector: Injector
76 val sonePlugin = SonePlugin {
77 Guice.createInjector(*it).also {
81 sonePlugin.setLanguage(ENGLISH)
82 sonePlugin.runPlugin(pluginRespirator)
87 fun `core is being started`() {
88 sonePlugin.runPlugin(pluginRespirator)
89 val core = injector.getInstance<Core>()
94 fun `notification handler is being started`() {
95 sonePlugin.runPlugin(pluginRespirator)
96 val notificationHandler = injector.getInstance<NotificationHandler>()
97 verify(notificationHandler).start()
102 private fun mockInjector() = mock<Injector>().apply {
103 val injected = mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
104 fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz)
105 whenever(getInstance(any<Key<*>>())).then {
106 injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) {
107 it.getArgument<Key<*>>(0).typeLiteral.type.typeName.toClass().let(::mockValue)
110 whenever(getInstance(any<Class<*>>())).then {
111 injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) {
112 it.getArgument<Class<*>>(0).let(::mockValue)
117 private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)