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 val sonePlugin by lazy { SonePlugin { injector } }
26 private val pluginRespirator = deepMock<PluginRespirator>()
27 private val node = deepMock<Node>()
28 private val clientCore = deepMock<NodeClientCore>()
29 private val uskManager = deepMock<USKManager>()
32 setField(node, "clientCore", clientCore)
33 whenever(pluginRespirator.node).thenReturn(node)
34 setField(clientCore, "uskManager", uskManager)
38 fun `sone plugin can be started`() {
39 sonePlugin.setLanguage(ENGLISH)
40 sonePlugin.runPlugin(pluginRespirator)
44 fun `core can be created`() {
45 val injector: Injector = runSonePluginWithRealInjector()
46 assertThat(injector.getInstance<Core>(), notNullValue())
50 fun `fcp interface can be created`() {
51 val injector: Injector = runSonePluginWithRealInjector()
52 assertThat(injector.getInstance<FcpInterface>(), notNullValue())
56 fun `web interface can be created`() {
57 val injector: Injector = runSonePluginWithRealInjector()
58 assertThat(injector.getInstance<WebInterface>(), notNullValue())
62 fun `web of trust connector can be created`() {
63 val injector: Injector = runSonePluginWithRealInjector()
64 assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
68 fun `notification handler can be created`() {
69 val injector: Injector = runSonePluginWithRealInjector()
70 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
73 private fun runSonePluginWithRealInjector(): Injector {
74 lateinit var injector: Injector
75 val sonePlugin = SonePlugin {
76 Guice.createInjector(*it).also {
80 sonePlugin.setLanguage(ENGLISH)
81 sonePlugin.runPlugin(pluginRespirator)
86 fun `core is being started`() {
87 sonePlugin.runPlugin(pluginRespirator)
88 val core = injector.getInstance<Core>()
93 fun `notification handler is being requested`() {
94 sonePlugin.runPlugin(pluginRespirator)
95 assertThat(getInjected(NotificationHandler::class.java), notNullValue())
98 private fun <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
99 injected[TypeLiteral.get(clazz) to annotation] as? T
101 private val injected =
102 mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
104 private val injector = mock<Injector>().apply {
105 fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz)
106 whenever(getInstance(any<Key<*>>())).then {
107 injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) {
108 it.getArgument<Key<*>>(0).typeLiteral.type.typeName.toClass().let(::mockValue)
111 whenever(getInstance(any<Class<*>>())).then {
112 injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) {
113 it.getArgument<Class<*>>(0).let(::mockValue)
120 private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)