Replace create reply ajax page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / JsonPageTest.kt
index b4d897c..87b2939 100644 (file)
@@ -21,8 +21,10 @@ import net.pterodactylus.sone.web.page.FreenetRequest
 import net.pterodactylus.util.notify.Notification
 import net.pterodactylus.util.web.Method.GET
 import org.junit.Before
+import org.mockito.ArgumentMatchers.anyBoolean
 import org.mockito.ArgumentMatchers.anyInt
 import org.mockito.ArgumentMatchers.anyString
+import org.mockito.ArgumentMatchers.eq
 import org.mockito.ArgumentMatchers.isNull
 import java.util.NoSuchElementException
 import javax.naming.SizeLimitExceededException
@@ -43,10 +45,12 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        protected val httpRequest = mock<HTTPRequest>()
        protected val currentSone = deepMock<Sone>()
 
+       private val requestHeaders = mutableMapOf<String, String>()
        private val requestParameters = mutableMapOf<String, String>()
        private val requestParts = mutableMapOf<String, String>()
        private val localSones = mutableMapOf<String, Sone>()
        private val remoteSones = mutableMapOf<String, Sone>()
+       private val posts = mutableMapOf<String, Post>()
        private val newPosts = mutableMapOf<String, Post>()
        private val newReplies = mutableMapOf<String, PostReply>()
        private val linkedElements = mutableMapOf<String, LinkedElement>()
@@ -54,6 +58,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
 
        @Before
        fun setupWebInterface() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.core).thenReturn(core)
@@ -65,7 +70,8 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        @Before
        fun setupCore() {
                whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
-               whenever(core.getPost(anyString())).thenAnswer { newPosts[it[0]].asOptional() }
+               whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
+               whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
        }
 
        @Before
@@ -90,6 +96,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        @Before
        fun setupHttpRequest() {
                whenever(httpRequest.method).thenReturn("GET")
+               whenever(httpRequest.getHeader(anyString())).thenAnswer { requestHeaders[it.get<String>(0).toLowerCase()] }
                whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
                whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) }
                whenever(httpRequest.getParam(anyString(), isNull())).thenAnswer { requestParameters[it.getArgument(0)] }
@@ -102,6 +109,8 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
                whenever(httpRequest.isPartSet(anyString())).thenAnswer { it.getArgument(0) in requestParts }
        }
 
+       protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+
        protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
                whenever(this.id).thenReturn(id)
                whenever(this.name).thenReturn(name)
@@ -111,10 +120,15 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        }
 
        protected fun unsetCurrentSone() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
        }
 
+       protected fun addRequestHeader(key: String, value: String) {
+               requestHeaders += key.toLowerCase() to value
+       }
+
        protected fun addRequestParameter(key: String, value: String) {
                requestParameters += key to value
        }
@@ -131,6 +145,14 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
                remoteSones += sone.id to sone
        }
 
+       protected fun addLocalSone(id: String, sone: Sone) {
+               localSones += id to sone
+       }
+
+       protected fun addPost(id: String, post: Post) {
+               posts[id] = post
+       }
+
        protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) =
                        mock<Post>().apply {
                                whenever(this.id).thenReturn(id)