Convert search page test to use new web page test base
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jun 2017 08:27:18 +0000 (10:27 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jun 2017 08:27:18 +0000 (10:27 +0200)
src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt

diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt
new file mode 100644 (file)
index 0000000..36e530c
--- /dev/null
@@ -0,0 +1,3 @@
+package net.pterodactylus.sone.utils
+
+fun <T> T?.asList() = this?.let(::listOf) ?: emptyList<T>()
diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt
new file mode 100644 (file)
index 0000000..1c2d7f5
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.sone.utils
+
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.empty
+import org.junit.Test
+
+/**
+ * Unit test for Object utils.
+ */
+class ObjectsTest {
+
+       @Test
+       fun `non-null value is turned into a list with one element`() {
+               assertThat(5.asList(), contains(5))
+       }
+
+       @Test
+       fun `null value is turned into empty list`() {
+               assertThat(null.asList(), empty())
+       }
+
+}
index 540b8b5..7fab8d6 100644 (file)
@@ -22,12 +22,11 @@ import java.util.concurrent.atomic.AtomicInteger
 /**
  * Unit test for [SearchPage].
  */
-class SearchPageTest : WebPageTest() {
+class SearchPageTest: WebPageTest2({ template, webInterface -> SearchPage(template, webInterface, ticker) }) {
 
-       private val ticker = mock<Ticker>()
-       private val page = SearchPage(template, webInterface, ticker)
-
-       override fun getPage() = page
+       companion object {
+               val ticker = mock<Ticker>()
+       }
 
        @Test
        fun `page returns correct path`() {
index 6c4372d..9e57539 100644 (file)
@@ -4,12 +4,16 @@ 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.Album
+import net.pterodactylus.sone.data.Image
 import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
 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.asList
 import net.pterodactylus.sone.utils.asOptional
 import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.FreenetRequest
@@ -54,6 +58,10 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla
        private val allSones = mutableMapOf<String, Sone>()
        private val localSones = mutableMapOf<String, Sone>()
        private val allPosts = mutableMapOf<String, Post>()
+       private val allPostReplies = mutableMapOf<String, PostReply>()
+       private val perPostReplies = mutableMapOf<String, PostReply>()
+       private val allAlbums = mutableMapOf<String, Album>()
+       private val allImages = mutableMapOf<String, Image>()
        private val translations = mutableMapOf<String, String>()
 
        @Before
@@ -64,6 +72,11 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla
                whenever(core.localSones).then { localSones.values }
                whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
                whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
+               whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]].asOptional() }
+               whenever(core.getReplies(anyString())).then { perPostReplies[it[0]].asList() }
+               whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] }
+               whenever(core.getImage(anyString())).then { allImages[it[0]]}
+               whenever(core.getImage(anyString(), anyBoolean())).then { allImages[it[0]]}
        }
 
        @Before
@@ -145,6 +158,19 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla
                allPosts[id] = post
        }
 
+       fun addPostReply(id: String, postReply: PostReply) {
+               allPostReplies[id] = postReply
+               postReply.postId?.also { perPostReplies[it] = postReply }
+       }
+
+       fun addAlbum(id: String, album: Album) {
+               allAlbums[id] = album
+       }
+
+       fun addImage(id: String, image: Image) {
+               allImages[id] = image
+       }
+
        fun addTranslation(key: String, value: String) {
                translations[key] = value
        }
@@ -159,7 +185,7 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla
                caughtException?.run { throw this } ?: assertions()
        }
 
-       fun verifyRedirect(target: String, assertions: () -> Unit) {
+       fun verifyRedirect(target: String, assertions: () -> Unit = {}) {
                try {
                        page.handleRequest(freenetRequest, templateContext)
                        fail()