🚚 Move notification handler usage into Sone plugin
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / main / SonePluginTest.kt
1 package net.pterodactylus.sone.main
2
3 import com.google.inject.*
4 import freenet.client.async.*
5 import freenet.l10n.BaseL10n.LANGUAGE.*
6 import freenet.node.*
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.*
17 import kotlin.test.*
18
19 /**
20  * Unit test for [SonePlugin].
21  */
22 @Dirty
23 class SonePluginTest {
24
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>()
30
31         init {
32                 setField(node, "clientCore", clientCore)
33                 whenever(pluginRespirator.node).thenReturn(node)
34                 setField(clientCore, "uskManager", uskManager)
35         }
36
37         @Test
38         fun `sone plugin can be started`() {
39                 sonePlugin.setLanguage(ENGLISH)
40                 sonePlugin.runPlugin(pluginRespirator)
41         }
42
43         @Test
44         fun `core can be created`() {
45                 val injector: Injector = runSonePluginWithRealInjector()
46                 assertThat(injector.getInstance<Core>(), notNullValue())
47         }
48
49         @Test
50         fun `fcp interface can be created`() {
51                 val injector: Injector = runSonePluginWithRealInjector()
52                 assertThat(injector.getInstance<FcpInterface>(), notNullValue())
53         }
54
55         @Test
56         fun `web interface can be created`() {
57                 val injector: Injector = runSonePluginWithRealInjector()
58                 assertThat(injector.getInstance<WebInterface>(), notNullValue())
59         }
60
61         @Test
62         fun `web of trust connector can be created`() {
63                 val injector: Injector = runSonePluginWithRealInjector()
64                 assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
65         }
66
67         @Test
68         fun `notification handler can be created`() {
69                 val injector: Injector = runSonePluginWithRealInjector()
70                 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
71         }
72
73         private fun runSonePluginWithRealInjector(): Injector {
74                 lateinit var injector: Injector
75                 val sonePlugin = SonePlugin {
76                         Guice.createInjector(*it).also {
77                                 injector = it
78                         }
79                 }
80                 sonePlugin.setLanguage(ENGLISH)
81                 sonePlugin.runPlugin(pluginRespirator)
82                 return injector
83         }
84
85         @Test
86         fun `core is being started`() {
87                 sonePlugin.runPlugin(pluginRespirator)
88                 val core = injector.getInstance<Core>()
89                 verify(core).start()
90         }
91
92         @Test
93         fun `notification handler is being requested`() {
94                 sonePlugin.runPlugin(pluginRespirator)
95                 assertThat(getInjected(NotificationHandler::class.java), notNullValue())
96         }
97
98         private fun <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
99                         injected[TypeLiteral.get(clazz) to annotation] as? T
100
101         private val injected =
102                         mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
103
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)
109                         }
110                 }
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)
114                         }
115                 }
116         }
117
118 }
119
120 private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)