Add Kotlin version of web page test base, convert first test
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / WebPageTest2.kt
1 package net.pterodactylus.sone.web.pages
2
3 import com.google.common.eventbus.EventBus
4 import freenet.clients.http.ToadletContext
5 import freenet.support.api.HTTPRequest
6 import net.pterodactylus.sone.core.Preferences
7 import net.pterodactylus.sone.data.Sone
8 import net.pterodactylus.sone.test.deepMock
9 import net.pterodactylus.sone.test.get
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import net.pterodactylus.sone.utils.asOptional
13 import net.pterodactylus.sone.web.WebInterface
14 import net.pterodactylus.sone.web.page.FreenetRequest
15 import net.pterodactylus.util.template.Template
16 import net.pterodactylus.util.template.TemplateContext
17 import net.pterodactylus.util.web.Method.GET
18 import org.junit.Before
19 import org.mockito.ArgumentMatchers.anyInt
20 import org.mockito.ArgumentMatchers.anyLong
21 import org.mockito.ArgumentMatchers.anyString
22
23 /**
24  * Base class for web page tests.
25  */
26 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
27
28         protected val currentSone = mock<Sone>()
29         private val template = mock<Template>()
30         private val webInterface = deepMock<WebInterface>()
31         protected val core = webInterface.core!!
32         private val eventBus = mock<EventBus>()
33         private val preferences = Preferences(eventBus)
34         private val l10n = webInterface.l10n!!
35
36         protected val page by lazy { pageSupplier(template, webInterface) }
37         private val httpRequest = mock<HTTPRequest>()
38         protected val freenetRequest = mock<FreenetRequest>()
39         protected val templateContext = TemplateContext()
40
41         private val toadletContext = deepMock<ToadletContext>()
42         private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
43         private val allSones = mutableMapOf<String, Sone>()
44         private val translations = mutableMapOf<String, String>()
45
46         @Before
47         fun setupCore() {
48                 whenever(core.preferences).thenReturn(preferences)
49                 whenever(core.sones).then { allSones.values }
50                 whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
51         }
52
53         @Before
54         fun setupHttpRequest() {
55                 whenever(httpRequest.method).thenReturn("GET")
56                 whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
57                 whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
58                 whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
59                 whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
60                 whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
61                 whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
62                 whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
63                 whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
64                 whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
65                 whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
66         }
67
68         @Before
69         fun setupFreenetRequest() {
70                 whenever(freenetRequest.method).thenReturn(GET)
71                 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
72                 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
73         }
74
75         @Before
76         fun setupTranslations() {
77                 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
78         }
79
80         fun addHttpRequestParameter(name: String, value: String) {
81                 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
82         }
83
84         fun addSone(id: String, sone: Sone) {
85                 allSones[id] = sone
86         }
87
88         fun addTranslation(key: String, value: String) {
89                 translations[key] = value
90         }
91
92         fun verifyNoRedirect(assertions: () -> Unit) {
93                 var caughtException: Exception? = null
94                 try {
95                         page.handleRequest(freenetRequest, templateContext)
96                 } catch (e: Exception) {
97                         caughtException = e
98                 }
99                 caughtException?.run { throw this } ?: assertions()
100         }
101
102 }