🎨 Reduce mocking of albums and images
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 20:32:37 +0000 (21:32 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 18 Feb 2020 20:32:37 +0000 (21:32 +0100)
15 files changed:
src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt
src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt
src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt
src/test/kotlin/net/pterodactylus/sone/template/ImageAccessorTest.kt
src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt

index a7b187d..9cbaff0 100644 (file)
@@ -57,7 +57,7 @@ class FreenetInterfaceTest {
        private val uskManager = mock<USKManager>()
        private val sone = mock<Sone>()
        private val callbackCaptor: ArgumentCaptor<USKCallback> = forClass(USKCallback::class.java)
-       private val image = mock<Image>()
+       private val image: Image = ImageImpl()
        private val insertToken: InsertToken
        private val bucket = mock<Bucket>()
        private val clientGetCallback: ArgumentCaptor<ClientGetCallback> = forClass(ClientGetCallback::class.java)
index 60bb7e2..6e2c7c5 100644 (file)
@@ -2,8 +2,8 @@ package net.pterodactylus.sone.core
 
 import net.pterodactylus.sone.core.FreenetInterface.InsertToken
 import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier
-import net.pterodactylus.sone.data.Image
 import net.pterodactylus.sone.data.TemporaryImage
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.getInstance
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
@@ -23,7 +23,7 @@ import org.mockito.Mockito.verify
 class ImageInserterTest {
 
        private val temporaryImage = mock<TemporaryImage>().apply { whenever(id).thenReturn("image-id") }
-       private val image = mock<Image>().apply { whenever(id).thenReturn("image-id") }
+       private val image = ImageImpl("image-id")
        private val freenetInterface = mock<FreenetInterface>()
        private val insertToken = mock<InsertToken>()
        private val insertTokenSupplier: InsertTokenSupplier = mock<InsertTokenSupplier>().apply { whenever(apply(any())).thenReturn(insertToken) }
index cc4778f..150336f 100644 (file)
@@ -57,8 +57,8 @@ class MemoryDatabaseTest {
                assertThat(memoryDatabase.getPostReply("reply2"), isPostReply("reply2", "post2", 4000L, "reply2"))
                assertThat(memoryDatabase.getPostReply("reply3"), isPostReply("reply3", "post1", 5000L, "reply3"))
                assertThat(memoryDatabase.getPostReply("reply4"), nullValue())
-               assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", null, "album1", "album-description1"))
-               assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", null, "album2", "album-description2"))
+               assertThat(memoryDatabase.getAlbum("album1"), isAlbum("album1", "root", "album1", "album-description1"))
+               assertThat(memoryDatabase.getAlbum("album2"), isAlbum("album2", "root", "album2", "album-description2"))
                assertThat(memoryDatabase.getAlbum("album3"), isAlbum("album3", "album1", "album3", "album-description3"))
                assertThat(memoryDatabase.getAlbum("album4"), nullValue())
                assertThat(memoryDatabase.getImage("image1"), isImage("image1", 1000L, "KSK@image1", "image1", "image-description1", 16, 9))
@@ -123,9 +123,10 @@ class MemoryDatabaseTest {
                                .setDescription("album-description3")
                                .update()
                firstAlbum.addAlbum(thirdAlbum)
-               val rootAlbum = mock<Album>()
-               whenever(rootAlbum.id).thenReturn("root")
-               whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
+               val rootAlbum = AlbumImpl(sone, "root").also {
+                       it.addAlbum(firstAlbum)
+                       it.addAlbum(secondAlbum)
+               }
                whenever(sone.rootAlbum).thenReturn(rootAlbum)
                val firstImage = TestImageBuilder().withId("image1")
                                .build()
index f7da592..21a3d00 100644 (file)
@@ -1,13 +1,9 @@
 package net.pterodactylus.sone.template
 
-import net.pterodactylus.sone.data.Album
-import net.pterodactylus.sone.data.Image
-import net.pterodactylus.sone.test.mock
-import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.data.impl.*
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.nullValue
-import org.junit.Before
 import org.junit.Test
 
 /**
@@ -16,16 +12,6 @@ import org.junit.Test
 class ImageAccessorTest {
 
        private val accessor = ImageAccessor()
-       private val album = mock<Album>()
-       private val images = listOf(mock<Image>(), mock())
-
-       @Before
-       fun setupImages() {
-               whenever(album.images).thenReturn(images)
-               images.forEach {
-                       whenever(it.album).thenReturn(album)
-               }
-       }
 
        @Test
        fun `accessor returns next image for first image`() {
@@ -53,3 +39,7 @@ class ImageAccessorTest {
        }
 
 }
+
+private val sone = IdOnlySone("sone")
+private val album = AlbumImpl(sone)
+private val images = listOf(ImageImpl().modify().setSone(sone).update(), ImageImpl().modify().setSone(sone).update()).onEach(album::addImage)
index 87cfce0..2ddab15 100644 (file)
@@ -10,6 +10,7 @@ import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
 import net.pterodactylus.sone.data.Sone.SoneStatus.unknown
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.freenet.L10nText
 import net.pterodactylus.sone.freenet.wot.Identity
 import net.pterodactylus.sone.freenet.wot.OwnIdentity
@@ -217,24 +218,24 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns all images in the correct order`() {
-               val images = listOf(mock<Image>(), mock(), mock(), mock(), mock())
-               val firstAlbum = createAlbum(listOf(), listOf(images[0], images[3]))
-               val secondAlbum = createAlbum(listOf(), listOf(images[1], images[4], images[2]))
-               val rootAlbum = createAlbum(listOf(firstAlbum, secondAlbum), listOf())
+               val images = (0 until 5).map { ImageImpl().modify().setSone(sone).update() }
+               val firstAlbum = createAlbum(emptyList(), listOf(images[0], images[3]))
+               val secondAlbum = createAlbum(emptyList(), listOf(images[1], images[4], images[2]))
+               val rootAlbum = createAlbum(listOf(firstAlbum, secondAlbum), emptyList())
                whenever(sone.rootAlbum).thenReturn(rootAlbum)
                assertAccessorReturnValueMatches("allImages", contains(images[0], images[3], images[1], images[4], images[2]))
        }
 
        private fun createAlbum(albums: List<Album>, images: List<Image>) =
-                       mock<Album>().apply {
-                               whenever(this.albums).thenReturn(albums)
-                               whenever(this.images).thenReturn(images)
+                       AlbumImpl(sone).also {
+                               albums.forEach(it::addAlbum)
+                               images.forEach(it::addImage)
                        }
 
        @Test
        fun `accessor returns all albums in the correct order`() {
-               val albums = listOf(mock<Album>(), mock(), mock(), mock(), mock())
-               val rootAlbum = createAlbum(albums, listOf())
+               val albums = (0 until 5).map { AlbumImpl(sone)  }
+               val rootAlbum = createAlbum(albums, emptyList())
                whenever(sone.rootAlbum).thenReturn(rootAlbum)
                assertAccessorReturnValueMatches("albums", contains(*albums.toTypedArray()))
        }
index 1528d60..850cc9d 100644 (file)
@@ -1,10 +1,7 @@
 package net.pterodactylus.sone.web.ajax
 
-import net.pterodactylus.sone.data.Album
-import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.data.impl.AlbumImpl
-import net.pterodactylus.sone.test.deepMock
 import net.pterodactylus.sone.test.getInstance
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
@@ -20,8 +17,7 @@ import org.junit.Test
 class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::EditAlbumAjaxPage) {
 
        private val sone = mock<Sone>()
-       private val localSone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
-       private val album = mock<Album>().apply { whenever(id).thenReturn("album-id") }
+       private val album = AlbumImpl(sone, "album-id")
 
        @Test
        fun `request without album results in invalid-album-id`() {
@@ -30,7 +26,6 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed
 
        @Test
        fun `request with non-local album results in not-authorized`() {
-               whenever(album.sone).thenReturn(sone)
                addAlbum(album)
                addRequestParameter("album", "album-id")
                assertThatJsonFailed("not-authorized")
@@ -38,11 +33,11 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed
 
        @Test
        fun `request with moveLeft moves album to the left`() {
-               whenever(album.sone).thenReturn(localSone)
-               val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
-               val parentAlbum = mock<Album>()
-               whenever(parentAlbum.moveAlbumUp(album)).thenReturn(swappedAlbum)
-               whenever(album.parent).thenReturn(parentAlbum)
+               setupLocalSone()
+               AlbumImpl(sone).also {
+                       it.addAlbum(AlbumImpl(sone, "swapped"))
+                       it.addAlbum(album)
+               }
                addAlbum(album)
                addRequestParameter("album", "album-id")
                addRequestParameter("moveLeft", "true")
@@ -53,11 +48,11 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed
 
        @Test
        fun `request with moveRight moves album to the right`() {
-               whenever(album.sone).thenReturn(localSone)
-               val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
-               val parentAlbum = mock<Album>()
-               whenever(parentAlbum.moveAlbumDown(album)).thenReturn(swappedAlbum)
-               whenever(album.parent).thenReturn(parentAlbum)
+               setupLocalSone()
+               AlbumImpl(sone).also {
+                       it.addAlbum(album)
+                       it.addAlbum(AlbumImpl(sone, "swapped"))
+               }
                addAlbum(album)
                addRequestParameter("album", "album-id")
                addRequestParameter("moveRight", "true")
@@ -68,9 +63,7 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed
 
        @Test
        fun `request with missing title results in invalid-title`() {
-               whenever(album.sone).thenReturn(localSone)
-               whenever(album.modify()).thenReturn(deepMock())
-               whenever(album.modify().setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
+               setupLocalSone()
                addAlbum(album)
                addRequestParameter("album", "album-id")
                assertThatJsonFailed("invalid-album-title")
@@ -95,4 +88,8 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed
            assertThat(baseInjector.getInstance<EditAlbumAjaxPage>(), notNullValue())
        }
 
+       private fun setupLocalSone() {
+               whenever(sone.isLocal).thenReturn(true)
+       }
+
 }
index 26436a3..e63bb8a 100644 (file)
@@ -3,7 +3,7 @@ package net.pterodactylus.sone.web.ajax
 import net.pterodactylus.sone.data.Album
 import net.pterodactylus.sone.data.Image
 import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.data.impl.ImageImpl
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.template.ParserFilter
 import net.pterodactylus.sone.template.RenderFilter
 import net.pterodactylus.sone.template.ShortenFilter
@@ -38,9 +38,8 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
 
        @Test
        fun `request with non-local image results in not-authorized`() {
-               val image = mock<Image>()
                val sone = mock<Sone>()
-               whenever(image.sone).thenReturn(sone)
+               val image = ImageImpl().modify().setSone(sone).update()
                addImage(image, "image-id")
                addRequestParameter("image", "image-id")
                assertThatJsonFailed("not-authorized")
@@ -48,13 +47,12 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
 
        @Test
        fun `moving an image to the left returns the correct values`() {
-               val image = mock<Image>().apply { whenever(id).thenReturn("image-id") }
                val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
-               whenever(image.sone).thenReturn(sone)
-               val swapped = mock<Image>().apply { whenever(id).thenReturn("swapped") }
-               val album = mock<Album>()
-               whenever(album.moveImageUp(image)).thenReturn(swapped)
-               whenever(image.album).thenReturn(album)
+               val image = ImageImpl("image-id").modify().setSone(sone).update()
+               AlbumImpl(sone).also {
+                       it.addImage(ImageImpl("swapped").modify().setSone(sone).update())
+                       it.addImage(image)
+               }
                addImage(image)
                addRequestParameter("image", "image-id")
                addRequestParameter("moveLeft", "true")
@@ -66,13 +64,12 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
 
        @Test
        fun `moving an image to the right returns the correct values`() {
-               val image = mock<Image>().apply { whenever(id).thenReturn("image-id") }
                val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
-               whenever(image.sone).thenReturn(sone)
-               val swapped = mock<Image>().apply { whenever(id).thenReturn("swapped") }
-               val album = mock<Album>()
-               whenever(album.moveImageDown(image)).thenReturn(swapped)
-               whenever(image.album).thenReturn(album)
+               val image = ImageImpl("image-id").modify().setSone(sone).update()
+               AlbumImpl(sone).also {
+                       it.addImage(image)
+                       it.addImage(ImageImpl("swapped").modify().setSone(sone).update())
+               }
                addImage(image)
                addRequestParameter("image", "image-id")
                addRequestParameter("moveRight", "true")
@@ -84,9 +81,8 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
 
        @Test
        fun `request with empty title results in invalid-image-title`() {
-               val image = mock<Image>().apply { whenever(id).thenReturn("image-id") }
                val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
-               whenever(image.sone).thenReturn(sone)
+               val image = ImageImpl("image-id").modify().setSone(sone).update()
                addImage(image)
                addRequestParameter("image", "image-id")
                assertThatJsonFailed("invalid-image-title")
index 55b3d70..9830ffa 100644 (file)
@@ -1,6 +1,6 @@
 package net.pterodactylus.sone.web.pages
 
-import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.sone.web.page.*
@@ -16,20 +16,14 @@ import org.mockito.Mockito.verify
  */
 class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
 
-       private val sone = mock<Sone>()
-       private val album = mock<Album>()
-       private val parentAlbum = mock<Album>()
+       private val album = AlbumImpl(currentSone, "album-id")
+       private val parentAlbum = AlbumImpl(currentSone, "parent-id").also { it.addAlbum(album) }
 
        @Before
        fun setupAlbums() {
-               whenever(sone.id).thenReturn("sone-id")
-               whenever(sone.isLocal).thenReturn(true)
-               whenever(parentAlbum.id).thenReturn("parent-id")
-               whenever(parentAlbum.isRoot).thenReturn(true)
-               whenever(album.id).thenReturn("album-id")
-               whenever(album.sone).thenReturn(sone)
-               whenever(album.parent).thenReturn(parentAlbum)
-               whenever(sone.rootAlbum).thenReturn(parentAlbum)
+               whenever(currentSone.id).thenReturn("sone-id")
+               whenever(currentSone.isLocal).thenReturn(true)
+               whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
        }
 
        @Test
@@ -50,7 +44,6 @@ class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
 
        @Test
        fun `get request with valid album ID sets album in template context`() {
-               val album = mock<Album>()
                addAlbum("album-id", album)
                addHttpRequestParameter("album", "album-id")
                page.processTemplate(freenetRequest, templateContext)
@@ -66,7 +59,7 @@ class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
        @Test
        fun `post request redirects to no permissions page if album is not local`() {
                setMethod(POST)
-               whenever(sone.isLocal).thenReturn(false)
+               whenever(currentSone.isLocal).thenReturn(false)
                addAlbum("album-id", album)
                addHttpRequestPart("album", "album-id")
                verifyRedirect("noPermission.html")
@@ -94,12 +87,12 @@ class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
        @Test
        fun `album is deleted and page redirects to album if parent album is not root album`() {
                setMethod(POST)
-               whenever(parentAlbum.isRoot).thenReturn(false)
-               whenever(sone.rootAlbum).thenReturn(mock())
-               addAlbum("album-id", album)
-               addHttpRequestPart("album", "album-id")
-               verifyRedirect("imageBrowser.html?album=parent-id") {
-                       verify(core).deleteAlbum(album)
+               val subAlbum = AlbumImpl(currentSone, "sub-album-id")
+               album.addAlbum(subAlbum)
+               addAlbum("sub-album-id", subAlbum)
+               addHttpRequestPart("album", "sub-album-id")
+               verifyRedirect("imageBrowser.html?album=album-id") {
+                       verify(core).deleteAlbum(subAlbum)
                }
        }
 
index cecde50..eccf949 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.sone.web.page.*
@@ -15,16 +16,12 @@ import org.mockito.Mockito.*
  */
 class DeleteImagePageTest : WebPageTest(::DeleteImagePage) {
 
-       private val image = mock<Image>()
        private val sone = mock<Sone>()
+       private val image = ImageImpl("image-id").modify().setSone(sone).update()!!
 
        @Before
        fun setupImage() {
-               val album = mock<Album>()
-               whenever(album.id).thenReturn("album-id")
-               whenever(image.id).thenReturn("image-id")
-               whenever(image.sone).thenReturn(sone)
-               whenever(image.album).thenReturn(album)
+               AlbumImpl(sone, "album-id").also { it.addImage(image) }
                whenever(sone.isLocal).thenReturn(true)
        }
 
index 72e814b..97e8865 100644 (file)
@@ -1,8 +1,6 @@
 package net.pterodactylus.sone.web.pages
 
-import net.pterodactylus.sone.data.*
-import net.pterodactylus.sone.data.Album.*
-import net.pterodactylus.sone.data.Album.Modifier.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.util.web.Method.*
@@ -16,20 +14,16 @@ import org.mockito.Mockito.*
  */
 class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
 
-       private val album = mock<Album>()
-       private val parentAlbum = mock<Album>()
-       private val modifier = mockBuilder<Modifier>()
-       private val sone = mock<Sone>()
+       private val album = AlbumImpl(currentSone, "album-id")
+       private val parentAlbum = AlbumImpl(currentSone, "parent-id").also {
+               it.addAlbum(AlbumImpl(currentSone))
+               it.addAlbum(album)
+               it.addAlbum(AlbumImpl(currentSone))
+       }
 
        @Before
        fun setup() {
-               whenever(album.id).thenReturn("album-id")
-               whenever(album.sone).thenReturn(sone)
-               whenever(album.parent).thenReturn(parentAlbum)
-               whenever(album.modify()).thenReturn(modifier)
-               whenever(modifier.update()).thenReturn(album)
-               whenever(parentAlbum.id).thenReturn("parent-id")
-               whenever(sone.isLocal).thenReturn(true)
+               whenever(currentSone.isLocal).thenReturn(true)
                addHttpRequestHeader("Host", "www.te.st")
        }
 
@@ -63,7 +57,7 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
        @Test
        fun `post request with album of non-local sone redirects to no permissions page`() {
                setMethod(POST)
-               whenever(sone.isLocal).thenReturn(false)
+               whenever(currentSone.isLocal).thenReturn(false)
                addAlbum("album-id", album)
                addHttpRequestPart("album", "album-id")
                verifyRedirect("noPermission.html")
@@ -76,7 +70,7 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
                addHttpRequestPart("album", "album-id")
                addHttpRequestPart("moveLeft", "true")
                verifyRedirect("imageBrowser.html?album=parent-id") {
-                       verify(parentAlbum).moveAlbumUp(album)
+                       assertThat(parentAlbum.albums.indexOf(album), equalTo(0))
                        verify(core).touchConfiguration()
                }
        }
@@ -88,7 +82,7 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
                addHttpRequestPart("album", "album-id")
                addHttpRequestPart("moveRight", "true")
                verifyRedirect("imageBrowser.html?album=parent-id") {
-                       verify(parentAlbum).moveAlbumDown(album)
+                       assertThat(parentAlbum.albums.indexOf(album), equalTo(2))
                        verify(core).touchConfiguration()
                }
        }
@@ -98,7 +92,6 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
                setMethod(POST)
                addAlbum("album-id", album)
                addHttpRequestPart("album", "album-id")
-               whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
                verifyRedirect("emptyAlbumTitle.html")
        }
 
@@ -110,9 +103,8 @@ class EditAlbumPageTest : WebPageTest(::EditAlbumPage) {
                addHttpRequestPart("title", "title")
                addHttpRequestPart("description", "description")
                verifyRedirect("imageBrowser.html?album=album-id") {
-                       verify(modifier).setTitle("title")
-                       verify(modifier).setDescription("description")
-                       verify(modifier).update()
+                       assertThat(album.title, equalTo("title"))
+                       assertThat(album.description, equalTo("description"))
                        verify(core).touchConfiguration()
                }
        }
index e43551c..5f59031 100644 (file)
@@ -1,8 +1,7 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
-import net.pterodactylus.sone.data.Image.*
-import net.pterodactylus.sone.data.Image.Modifier.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.util.web.Method.*
@@ -16,19 +15,17 @@ import org.mockito.Mockito.*
  */
 class EditImagePageTest : WebPageTest(::EditImagePage) {
 
-       private val image = mock<Image>()
-       private val modifier = mockBuilder<Modifier>()
        private val sone = mock<Sone>()
-       private val album = mock<Album>()
+       private val image = ImageImpl("image-id").modify().setSone(sone).update()!!
+       private val album = AlbumImpl(sone, "album-id").also {
+               it.addImage(ImageImpl("1").modify().setSone(sone).update())
+               it.addImage(image)
+               it.addImage(ImageImpl("2").modify().setSone(sone).update())
+       }
 
        @Before
        fun setupImage() {
                whenever(sone.isLocal).thenReturn(true)
-               whenever(album.id).thenReturn("album-id")
-               whenever(modifier.update()).thenReturn(image)
-               whenever(image.sone).thenReturn(sone)
-               whenever(image.album).thenReturn(album)
-               whenever(image.modify()).thenReturn(modifier)
        }
 
        @Test
@@ -75,7 +72,7 @@ class EditImagePageTest : WebPageTest(::EditImagePage) {
                addHttpRequestPart("returnPage", "return.html")
                addHttpRequestPart("moveLeft", "true")
                verifyRedirect("return.html") {
-                       verify(album).moveImageUp(image)
+                       assertThat(album.images.indexOf(image), equalTo(0))
                        verify(core).touchConfiguration()
                }
        }
@@ -88,7 +85,7 @@ class EditImagePageTest : WebPageTest(::EditImagePage) {
                addHttpRequestPart("returnPage", "return.html")
                addHttpRequestPart("moveRight", "true")
                verifyRedirect("return.html") {
-                       verify(album).moveImageDown(image)
+                       assertThat(album.images.indexOf(image), equalTo(2))
                        verify(core).touchConfiguration()
                }
        }
@@ -100,7 +97,6 @@ class EditImagePageTest : WebPageTest(::EditImagePage) {
                addHttpRequestPart("image", "image-id")
                addHttpRequestPart("returnPage", "return.html")
                addHttpRequestPart("title", "   ")
-               whenever(modifier.update()).doThrow<ImageTitleMustNotBeEmpty>()
                verifyRedirect("emptyImageTitle.html") {
                        verify(core, never()).touchConfiguration()
                }
@@ -115,9 +111,8 @@ class EditImagePageTest : WebPageTest(::EditImagePage) {
                addHttpRequestPart("title", "Title")
                addHttpRequestPart("description", "Description")
                verifyRedirect("return.html") {
-                       verify(modifier).setTitle("Title")
-                       verify(modifier).setDescription("Description")
-                       verify(modifier).update()
+                       assertThat(image.title, equalTo("Title"))
+                       assertThat(image.description, equalTo("Description"))
                        verify(core).touchConfiguration()
                }
        }
@@ -132,9 +127,8 @@ class EditImagePageTest : WebPageTest(::EditImagePage) {
                addHttpRequestHeader("Host", "www.te.st")
                addHttpRequestPart("description", "Get http://www.te.st/KSK@GPL.txt")
                verifyRedirect("return.html") {
-                       verify(modifier).setTitle("Title")
-                       verify(modifier).setDescription("Get KSK@GPL.txt")
-                       verify(modifier).update()
+                       assertThat(image.title, equalTo("Title"))
+                       assertThat(image.description, equalTo("Get KSK@GPL.txt"))
                        verify(core).touchConfiguration()
                }
        }
index 74c3851..ac909ea 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.sone.web.page.*
@@ -21,16 +22,13 @@ class EditProfilePageTest : WebPageTest(::EditProfilePage) {
 
        @Before
        fun setupProfile() {
-               val avatar = mock<Image>()
-               whenever(avatar.id).thenReturn("image-id")
-               whenever(avatar.sone).thenReturn(currentSone)
                profile.firstName = "First"
                profile.middleName = "Middle"
                profile.lastName = "Last"
                profile.birthDay = 31
                profile.birthMonth = 12
                profile.birthYear = 1999
-               profile.setAvatar(avatar)
+               profile.setAvatar(ImageImpl("image-id").modify().setSone(currentSone).update())
                whenever(currentSone.profile).thenReturn(profile)
        }
 
@@ -120,9 +118,7 @@ class EditProfilePageTest : WebPageTest(::EditProfilePage) {
 
        @Test
        fun `post request with new avatar ID and save profile saves the profile and redirects back to profile edit page`() {
-               val newAvatar = mock<Image>()
-               whenever(newAvatar.sone).thenReturn(currentSone)
-               whenever(newAvatar.id).thenReturn("avatar-id")
+               val newAvatar = ImageImpl("avatar-id").modify().setSone(currentSone).update()
                addImage("avatar-id", newAvatar)
                verifySingleFieldCanBeChanged("avatarId", "avatar-id") { profile.avatar }
        }
index 38ecce4..4a2304c 100644 (file)
@@ -34,7 +34,7 @@ class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
 
        @Test
        fun `get request with album sets album and page in template context`() {
-               val album = mock<Album>()
+               val album = AlbumImpl(currentSone, "album-id")
                addAlbum("album-id", album)
                addHttpRequestParameter("album", "album-id")
                addHttpRequestParameter("page", "5")
@@ -47,7 +47,7 @@ class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
 
        @Test
        fun `get request with image sets image in template context`() {
-               val image = mock<Image>()
+               val image = ImageImpl()
                addImage("image-id", image)
                addHttpRequestParameter("image", "image-id")
                verifyNoRedirect {
index 14cae74..23987b6 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.freenet.wot.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.utils.*
@@ -37,11 +38,12 @@ class KnownSonesPageTest : WebPageTest(::KnownSonesPage) {
                whenever(this.time).thenReturn(time)
                whenever(this.posts).thenReturn((0..(posts - 1)).map { mock<Post>() })
                whenever(this.replies).thenReturn((0..(replies - 1)).map { mock<PostReply>() }.toSet())
-               val album = mock<Album>()
-               whenever(album.images).thenReturn(((0..(images - 1)).map { mock<Image>() }))
-               val rootAlbum = mock<Album>().apply {
-                       whenever(albums).thenReturn(listOf(album))
+               val album = AlbumImpl(this)
+               repeat(images) {
+                       ImageImpl().modify().setSone(this).update()
+                                       .also(album::addImage)
                }
+               val rootAlbum = AlbumImpl(this).also { it.addAlbum(album) }
                whenever(this.rootAlbum).thenReturn(rootAlbum)
                whenever(this.profile).thenReturn(mock())
                whenever(id).thenReturn(name.toLowerCase())
index 370c0a7..4c99759 100644 (file)
@@ -1,10 +1,9 @@
 package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
-import net.pterodactylus.sone.data.Image.*
+import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.test.getInstance
 import net.pterodactylus.sone.test.mock
-import net.pterodactylus.sone.test.mockBuilder
 import net.pterodactylus.sone.test.whenever
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.sone.web.page.*
@@ -20,10 +19,7 @@ import org.mockito.Mockito.eq
  */
 class UploadImagePageTest : WebPageTest(::UploadImagePage) {
 
-       private val parentAlbum = mock<Album>().apply {
-               whenever(id).thenReturn("parent-id")
-               whenever(sone).thenReturn(currentSone)
-       }
+       private val parentAlbum = AlbumImpl(currentSone, "parent-id")
 
        @Test
        fun `page returns correct path`() {
@@ -58,9 +54,9 @@ class UploadImagePageTest : WebPageTest(::UploadImagePage) {
        @Test
        fun `post request with parent that is not the current sone results in no permission error page`() {
                setMethod(POST)
+               val remoteAlbum = AlbumImpl(mock(), "parent-id")
+               addAlbum("parent-id", remoteAlbum)
                addHttpRequestPart("parent", "parent-id")
-               whenever(parentAlbum.sone).thenReturn(mock())
-               addAlbum("parent-id", parentAlbum)
                verifyRedirect("noPermission.html")
        }
 
@@ -97,19 +93,14 @@ class UploadImagePageTest : WebPageTest(::UploadImagePage) {
                addHttpRequestHeader("Host", "localhost:8888")
                addUploadedFile("image", "upload-image-value-image.png", "image/png", "upload-image-value-image.png")
                val temporaryImage = TemporaryImage("temp-image")
-               val imageModifier = mockBuilder<Modifier>()
-               val image = mock<Image>().apply {
-                       whenever(modify()).thenReturn(imageModifier)
-               }
+               val image = ImageImpl()
                whenever(core.createTemporaryImage(eq("image/png"), any())).thenReturn(temporaryImage)
                whenever(core.createImage(currentSone, parentAlbum, temporaryImage)).thenReturn(image)
                verifyRedirect("imageBrowser.html?album=parent-id") {
-                       verify(image).modify()
-                       verify(imageModifier).setWidth(2)
-                       verify(imageModifier).setHeight(1)
-                       verify(imageModifier).setTitle("Title")
-                       verify(imageModifier).setDescription("Description @ KSK@foo")
-                       verify(imageModifier).update()
+                       assertThat(image.width, equalTo(2))
+                       assertThat(image.height, equalTo(1))
+                       assertThat(image.title, equalTo("Title"))
+                       assertThat(image.description, equalTo("Description @ KSK@foo"))
                }
        }