🎨 Reduce mocking of albums and images
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / ImageBrowserPageTest.kt
index 063a588..4a2304c 100644 (file)
 package net.pterodactylus.sone.web.pages
 
-import net.pterodactylus.sone.data.Album
-import net.pterodactylus.sone.data.Image
-import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.mock
-import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.util.web.Method.GET
-import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.contains
-import org.hamcrest.Matchers.equalTo
-import org.junit.Test
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.AlbumImpl
+import net.pterodactylus.sone.data.impl.ImageImpl
+import net.pterodactylus.sone.test.*
+import net.pterodactylus.sone.web.*
+import net.pterodactylus.sone.web.page.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import java.net.*
 
 /**
  * Unit test for [ImageBrowserPage].
  */
-class ImageBrowserPageTest : WebPageTest() {
-
-       private val page = ImageBrowserPage(template, webInterface)
+class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
 
        @Test
        fun `page returns correct path`() {
-           assertThat(page.path, equalTo("imageBrowser.html"))
+               assertThat(page.path, equalTo("imageBrowser.html"))
        }
 
        @Test
        fun `page requires login`() {
-           assertThat(page.requiresLogin(), equalTo(true))
+               assertThat(page.requiresLogin(), equalTo(true))
        }
 
        @Test
        fun `page returns correct title`() {
-               whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
-           assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
+               addTranslation("Page.ImageBrowser.Title", "image browser page title")
+               assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
        }
 
        @Test
        fun `get request with album sets album and page in template context`() {
-               request("", GET)
-               val album = mock<Album>()
+               val album = AlbumImpl(currentSone, "album-id")
                addAlbum("album-id", album)
                addHttpRequestParameter("album", "album-id")
                addHttpRequestParameter("page", "5")
-               page.processTemplate(freenetRequest, templateContext)
-               assertThat(templateContext["albumRequested"], equalTo<Any>(true))
-               assertThat(templateContext["album"], equalTo<Any>(album))
-               assertThat(templateContext["page"], equalTo<Any>("5"))
+               verifyNoRedirect {
+                       assertThat(templateContext["albumRequested"], equalTo<Any>(true))
+                       assertThat(templateContext["album"], equalTo<Any>(album))
+                       assertThat(templateContext["page"], equalTo<Any>("5"))
+               }
        }
 
        @Test
        fun `get request with image sets image in template context`() {
-               request("", GET)
-               val image = mock<Image>()
+               val image = ImageImpl()
                addImage("image-id", image)
                addHttpRequestParameter("image", "image-id")
-               page.processTemplate(freenetRequest, templateContext)
-               assertThat(templateContext["imageRequested"], equalTo<Any>(true))
-               assertThat(templateContext["image"], equalTo<Any>(image))
+               verifyNoRedirect {
+                       assertThat(templateContext["imageRequested"], equalTo<Any>(true))
+                       assertThat(templateContext["image"], equalTo<Any>(image))
+               }
        }
 
        @Test
        fun `get request with sone sets sone in template context`() {
-               request("", GET)
                val sone = mock<Sone>()
                addSone("sone-id", sone)
                addHttpRequestParameter("sone", "sone-id")
-               page.processTemplate(freenetRequest, templateContext)
-               assertThat(templateContext["soneRequested"], equalTo<Any>(true))
-               assertThat(templateContext["sone"], equalTo<Any>(sone))
+               verifyNoRedirect {
+                       assertThat(templateContext["soneRequested"], equalTo<Any>(true))
+                       assertThat(templateContext["sone"], equalTo<Any>(sone))
+               }
        }
 
        @Test
        fun `get request with mode of gallery sets albums and page in template context`() {
-               request("", GET)
                val firstSone = createSone("first album", "second album")
                addSone("sone1", firstSone)
                val secondSone = createSone("third album", "fourth album")
                addSone("sone2", secondSone)
                addHttpRequestParameter("mode", "gallery")
-               page.processTemplate(freenetRequest, templateContext)
-               assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
-               @Suppress("UNCHECKED_CAST")
-               assertThat(templateContext["albums"] as Iterable<Album>, contains(
-                               firstSone.rootAlbum.albums[0],
-                               secondSone.rootAlbum.albums[1],
-                               firstSone.rootAlbum.albums[1],
-                               secondSone.rootAlbum.albums[0]
-               ))
+               verifyNoRedirect {
+                       assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
+                       @Suppress("UNCHECKED_CAST")
+                       assertThat(templateContext["albums"] as Iterable<Album>, contains(
+                                       firstSone.rootAlbum.albums[0],
+                                       secondSone.rootAlbum.albums[1],
+                                       firstSone.rootAlbum.albums[1],
+                                       secondSone.rootAlbum.albums[0]
+                       ))
+               }
+       }
+
+       @Test
+       fun `get request for gallery can show second page`() {
+               core.preferences.newImagesPerPage = 2
+               val firstSone = createSone("first album", "second album")
+               addSone("sone1", firstSone)
+               val secondSone = createSone("third album", "fourth album")
+               addSone("sone2", secondSone)
+               addHttpRequestParameter("mode", "gallery")
+               addHttpRequestParameter("page", "1")
+               verifyNoRedirect {
+                       assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
+                       @Suppress("UNCHECKED_CAST")
+                       assertThat(templateContext["albums"] as Iterable<Album>, contains(
+                                       firstSone.rootAlbum.albums[1],
+                                       secondSone.rootAlbum.albums[0]
+                       ))
+               }
        }
 
        private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
                return mock<Sone>().apply {
-                       val rootAlbum = mock<Album>()
-                       val firstAlbum = mock<Album>()
-                       val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
-                       whenever(firstAlbum.images).thenReturn(listOf(firstImage))
-                       val secondAlbum = mock<Album>()
-                       val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
-                       whenever(secondAlbum.images).thenReturn(listOf(secondImage))
-                       whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
-                       whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
-                       whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
+                       val rootAlbum = AlbumImpl(this)
+                       val firstAlbum = AlbumImpl(this).modify().setTitle(firstAlbumTitle).update()
+                       firstAlbum.addImage(ImageImpl("1").modify().setSone(this).setKey("key").update())
+                       val secondAlbum = AlbumImpl(this).modify().setTitle(secondAlbumTitle).update()
+                       secondAlbum.addImage(ImageImpl("2").modify().setSone(this).setKey("key").update())
+                       rootAlbum.addAlbum(firstAlbum)
+                       rootAlbum.addAlbum(secondAlbum)
                        whenever(this.rootAlbum).thenReturn(rootAlbum)
                }
        }
 
        @Test
        fun `requesting nothing will show the albums of the current sone`() {
-               request("", GET)
-               page.processTemplate(freenetRequest, templateContext)
-               assertThat(templateContext["soneRequested"], equalTo<Any>(true))
-               assertThat(templateContext["sone"], equalTo<Any>(currentSone))
+               verifyNoRedirect {
+                       assertThat(templateContext["soneRequested"], equalTo<Any>(true))
+                       assertThat(templateContext["sone"], equalTo<Any>(currentSone))
+               }
        }
 
        @Test
        fun `page is link-excepted`() {
-           assertThat(page.isLinkExcepted(null), equalTo(true))
+               assertThat(page.isLinkExcepted(URI("")), equalTo(true))
+       }
+
+       @Test
+       fun `page can be created by dependency injection`() {
+               assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
+       }
+
+       @Test
+       fun `page is annotated with correct menuname`() {
+               assertThat(page.menuName, equalTo("ImageBrowser"))
+       }
+
+       @Test
+       fun `page is annotated with correct template path`() {
+               assertThat(page.templatePath, equalTo("/templates/imageBrowser.html"))
        }
 
 }