Return nullable Post instead of Optional
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / main / FreenetModuleTest.kt
1 package net.pterodactylus.sone.main
2
3 import com.google.inject.Guice
4 import freenet.client.HighLevelSimpleClient
5 import freenet.clients.http.SessionManager
6 import freenet.node.Node
7 import freenet.pluginmanager.PluginRespirator
8 import net.pterodactylus.sone.test.deepMock
9 import net.pterodactylus.sone.test.getInstance
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.sameInstance
14 import org.junit.Test
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [FreenetModule].
19  */
20 class FreenetModuleTest {
21
22         private val sessionManager = mock<SessionManager>()
23         private val pluginRespirator = deepMock<PluginRespirator>().apply {
24                 whenever(getSessionManager("Sone")).thenReturn(sessionManager)
25         }
26         private val node = pluginRespirator.node!!
27         private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!!
28         private val module = FreenetModule(pluginRespirator)
29         private val injector = Guice.createInjector(module)
30
31         private inline fun <reified T: Any> verifySingletonInstance() {
32                 val firstInstance = injector.getInstance<T>()
33                 val secondInstance = injector.getInstance<T>()
34                 assertThat(firstInstance, sameInstance(secondInstance))
35         }
36
37         @Test
38         fun `plugin respirator is returned correctly`() {
39                 assertThat(injector.getInstance<PluginRespirator>(), sameInstance(pluginRespirator))
40         }
41
42         @Test
43         fun `plugin respirator is returned as singleton`() {
44                 verifySingletonInstance<PluginRespirator>()
45         }
46
47         @Test
48         fun `node is returned correctly`() {
49                 assertThat(injector.getInstance<Node>(), sameInstance(node))
50         }
51
52         @Test
53         fun `node is returned as singleton`() {
54                 verifySingletonInstance<Node>()
55         }
56
57         @Test
58         fun `high level simply client is returned correctly`() {
59                 assertThat(injector.getInstance<HighLevelSimpleClient>(), sameInstance(highLevelSimpleClient))
60         }
61
62         @Test
63         fun `high level simply client is returned as singleton`() {
64                 verifySingletonInstance<HighLevelSimpleClient>()
65         }
66
67         @Test
68         fun `session manager is returned correctly`() {
69                 assertThat(injector.getInstance<SessionManager>(), sameInstance(sessionManager))
70         }
71
72         @Test
73         fun `session manager is returned as singleton`() {
74                 verifySingletonInstance<SessionManager>()
75                 verify(pluginRespirator).getSessionManager("Sone")
76         }
77
78 }