🔀 Merge next
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / PageToadletFactoryTest.kt
1 package net.pterodactylus.sone.web.page
2
3 import com.google.inject.*
4 import freenet.client.*
5 import freenet.clients.http.*
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.util.web.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.junit.*
12
13 private val highLevelSimpleClient = mock<HighLevelSimpleClient>()
14 private val sessionManager = mock<SessionManager>()
15 private const val pathPrefix = "/some/prefix/"
16
17 class PageToadletFactoryTest {
18
19         private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, sessionManager, pathPrefix)
20
21         @Test
22         fun `page toadlet without menu name is created without menu name`() {
23                 val page = mock<Page<FreenetRequest>>()
24                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
25                 assertThat(pageToadlet.menuName, nullValue())
26         }
27
28         @Test
29         fun `page toadlet with menu name is created with menu name`() {
30                 val page = mock<Page<FreenetRequest>>()
31                 val pageToadlet = pageToadletFactory.createPageToadlet(page, "testName")
32                 assertThat(pageToadlet.menuName, equalTo("testName"))
33         }
34
35         @Test
36         fun `path prefix is handed down correctly`() {
37                 val page = mock<Page<FreenetRequest>>().apply {
38                         whenever(path).thenReturn("path")
39                 }
40                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
41                 assertThat(pageToadlet.path(), equalTo("/some/prefix/path"))
42         }
43
44         @Test
45         fun `menu name is added from annotation when no menu name is given`() {
46                 val page = TestPageWithMenuName()
47                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
48                 assertThat(pageToadlet.menuName, equalTo("testName"))
49         }
50
51         @Test
52         fun `menu name from annotation is ignored when menu name is given`() {
53                 val page = TestPageWithMenuName()
54                 val pageToadlet = pageToadletFactory.createPageToadlet(page, "foo")
55                 assertThat(pageToadlet.menuName, equalTo("foo"))
56         }
57
58         @Test
59         fun `page toadlet factory can be created by guice`() {
60                 val injector = Guice.createInjector(
61                                 HighLevelSimpleClient::class.isProvidedBy(highLevelSimpleClient),
62                                 SessionManager::class.isProvidedBy(sessionManager),
63                                 String::class.withNameIsProvidedBy("/Sone/", "toadletPathPrefix")
64                 )
65             assertThat(injector.getInstance<PageToadletFactory>(), notNullValue())
66         }
67
68 }
69
70 @MenuName("testName")
71 private class TestPageWithMenuName : Page<FreenetRequest> {
72
73         override fun getPath() = ""
74         override fun isPrefixPage() = false
75         override fun handleRequest(request: FreenetRequest, response: Response) = response
76
77 }