🔀 Merge 'feature/sone-locked-notification' into 'next'
[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 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>()
31
32         init {
33                 setField(node, "clientCore", clientCore)
34                 whenever(pluginRespirator.node).thenReturn(node)
35                 setField(clientCore, "uskManager", uskManager)
36         }
37
38         @Test
39         fun `sone plugin can be started`() {
40                 sonePlugin.setLanguage(ENGLISH)
41                 sonePlugin.runPlugin(pluginRespirator)
42         }
43
44         @Test
45         fun `core can be created`() {
46                 val injector: Injector = runSonePluginWithRealInjector()
47                 assertThat(injector.getInstance<Core>(), notNullValue())
48         }
49
50         @Test
51         fun `fcp interface can be created`() {
52                 val injector: Injector = runSonePluginWithRealInjector()
53                 assertThat(injector.getInstance<FcpInterface>(), notNullValue())
54         }
55
56         @Test
57         fun `web interface can be created`() {
58                 val injector: Injector = runSonePluginWithRealInjector()
59                 assertThat(injector.getInstance<WebInterface>(), notNullValue())
60         }
61
62         @Test
63         fun `web of trust connector can be created`() {
64                 val injector: Injector = runSonePluginWithRealInjector()
65                 assertThat(injector.getInstance<WebOfTrustConnector>(), notNullValue())
66         }
67
68         @Test
69         fun `notification handler can be created`() {
70                 val injector: Injector = runSonePluginWithRealInjector()
71                 assertThat(injector.getInstance<NotificationHandler>(), notNullValue())
72         }
73
74         private fun runSonePluginWithRealInjector(): Injector {
75                 lateinit var injector: Injector
76                 val sonePlugin = SonePlugin {
77                         Guice.createInjector(*it).also {
78                                 injector = it
79                         }
80                 }
81                 sonePlugin.setLanguage(ENGLISH)
82                 sonePlugin.runPlugin(pluginRespirator)
83                 return injector
84         }
85
86         @Test
87         fun `core is being started`() {
88                 sonePlugin.runPlugin(pluginRespirator)
89                 val core = injector.getInstance<Core>()
90                 verify(core).start()
91         }
92
93         @Test
94         fun `notification handler is being started`() {
95                 sonePlugin.runPlugin(pluginRespirator)
96                 val notificationHandler = injector.getInstance<NotificationHandler>()
97                 verify(notificationHandler).start()
98         }
99
100 }
101
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)
108                 }
109         }
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)
113                 }
114         }
115 }
116
117 private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this)