Add Kotlin version of web page test base, convert first test
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 18 Jun 2017 13:35:47 +0000 (15:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 18 Jun 2017 13:37:09 +0000 (15:37 +0200)
src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt [new file with mode: 0644]

index dcd139b..d3e8dad 100644 (file)
@@ -3,6 +3,7 @@ package net.pterodactylus.sone.test
 import com.google.inject.Module
 import org.mockito.ArgumentCaptor
 import org.mockito.Mockito
+import org.mockito.invocation.InvocationOnMock
 import org.mockito.stubbing.OngoingStubbing
 
 inline fun <reified T : Any> mock(): T = Mockito.mock<T>(T::class.java)!!
@@ -22,3 +23,5 @@ inline fun <reified T : Any> bindMock(): Module =
 inline fun <reified T: Any?> whenever(methodCall: T) = Mockito.`when`(methodCall)!!
 
 inline fun <reified T : Any> OngoingStubbing<T>.thenReturnMock(): OngoingStubbing<T> = this.thenReturn(mock<T>())
+
+operator fun <T> InvocationOnMock.get(index: Int): T = getArgument(index)
index 5b3c959..7df6b7e 100644 (file)
@@ -19,13 +19,12 @@ import org.junit.Test
 /**
  * Unit test for [ViewSonePage].
  */
-class ViewSonePageTest: WebPageTest() {
+class ViewSonePageTest: WebPageTest2(::ViewSonePage) {
 
        init {
                whenever(currentSone.id).thenReturn("sone-id")
        }
 
-       private val page = ViewSonePage(template, webInterface)
        private val post1 = createPost("post1", "First Post.", 1000, currentSone)
        private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
        private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
@@ -34,8 +33,6 @@ class ViewSonePageTest: WebPageTest() {
        private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
        private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
 
-       override fun getPage() = page
-
        @Before
        fun setup() {
                whenever(currentSone.posts).thenReturn(mutableListOf(post2, post1))
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt
new file mode 100644 (file)
index 0000000..6981f9f
--- /dev/null
@@ -0,0 +1,102 @@
+package net.pterodactylus.sone.web.pages
+
+import com.google.common.eventbus.EventBus
+import freenet.clients.http.ToadletContext
+import freenet.support.api.HTTPRequest
+import net.pterodactylus.sone.core.Preferences
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.deepMock
+import net.pterodactylus.sone.test.get
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+import net.pterodactylus.util.web.Method.GET
+import org.junit.Before
+import org.mockito.ArgumentMatchers.anyInt
+import org.mockito.ArgumentMatchers.anyLong
+import org.mockito.ArgumentMatchers.anyString
+
+/**
+ * Base class for web page tests.
+ */
+abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
+
+       protected val currentSone = mock<Sone>()
+       private val template = mock<Template>()
+       private val webInterface = deepMock<WebInterface>()
+       protected val core = webInterface.core!!
+       private val eventBus = mock<EventBus>()
+       private val preferences = Preferences(eventBus)
+       private val l10n = webInterface.l10n!!
+
+       protected val page by lazy { pageSupplier(template, webInterface) }
+       private val httpRequest = mock<HTTPRequest>()
+       protected val freenetRequest = mock<FreenetRequest>()
+       protected val templateContext = TemplateContext()
+
+       private val toadletContext = deepMock<ToadletContext>()
+       private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
+       private val allSones = mutableMapOf<String, Sone>()
+       private val translations = mutableMapOf<String, String>()
+
+       @Before
+       fun setupCore() {
+               whenever(core.preferences).thenReturn(preferences)
+               whenever(core.sones).then { allSones.values }
+               whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
+       }
+
+       @Before
+       fun setupHttpRequest() {
+               whenever(httpRequest.method).thenReturn("GET")
+               whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
+               whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
+               whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
+               whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
+               whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
+               whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
+               whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
+               whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
+               whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
+               whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
+       }
+
+       @Before
+       fun setupFreenetRequest() {
+               whenever(freenetRequest.method).thenReturn(GET)
+               whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
+               whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
+       }
+
+       @Before
+       fun setupTranslations() {
+               whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
+       }
+
+       fun addHttpRequestParameter(name: String, value: String) {
+               getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
+       }
+
+       fun addSone(id: String, sone: Sone) {
+               allSones[id] = sone
+       }
+
+       fun addTranslation(key: String, value: String) {
+               translations[key] = value
+       }
+
+       fun verifyNoRedirect(assertions: () -> Unit) {
+               var caughtException: Exception? = null
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } catch (e: Exception) {
+                       caughtException = e
+               }
+               caughtException?.run { throw this } ?: assertions()
+       }
+
+}