From 2ab560633c24940fa49bdaf569635dd673ad7b19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 18 Feb 2020 21:32:37 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Reduce=20mocking=20of=20albums=20?= =?utf8?q?and=20images?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/core/FreenetInterfaceTest.kt | 2 +- .../pterodactylus/sone/core/ImageInserterTest.kt | 4 +-- .../sone/database/memory/MemoryDatabaseTest.kt | 11 +++---- .../sone/template/ImageAccessorTest.kt | 20 ++++--------- .../sone/template/SoneAccessorTest.kt | 19 ++++++------ .../sone/web/ajax/EditAlbumAjaxPageTest.kt | 35 ++++++++++------------ .../sone/web/ajax/EditImageAjaxPageTest.kt | 30 ++++++++----------- .../sone/web/pages/DeleteAlbumPageTest.kt | 33 ++++++++------------ .../sone/web/pages/DeleteImagePageTest.kt | 9 ++---- .../sone/web/pages/EditAlbumPageTest.kt | 34 ++++++++------------- .../sone/web/pages/EditImagePageTest.kt | 32 ++++++++------------ .../sone/web/pages/EditProfilePageTest.kt | 10 ++----- .../sone/web/pages/ImageBrowserPageTest.kt | 4 +-- .../sone/web/pages/KnownSonesPageTest.kt | 10 ++++--- .../sone/web/pages/UploadImagePageTest.kt | 27 ++++++----------- 15 files changed, 115 insertions(+), 165 deletions(-) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt index a7b187d..9cbaff0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt @@ -57,7 +57,7 @@ class FreenetInterfaceTest { private val uskManager = mock() private val sone = mock() private val callbackCaptor: ArgumentCaptor = forClass(USKCallback::class.java) - private val image = mock() + private val image: Image = ImageImpl() private val insertToken: InsertToken private val bucket = mock() private val clientGetCallback: ArgumentCaptor = forClass(ClientGetCallback::class.java) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt index 60bb7e2..6e2c7c5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt @@ -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().apply { whenever(id).thenReturn("image-id") } - private val image = mock().apply { whenever(id).thenReturn("image-id") } + private val image = ImageImpl("image-id") private val freenetInterface = mock() private val insertToken = mock() private val insertTokenSupplier: InsertTokenSupplier = mock().apply { whenever(apply(any())).thenReturn(insertToken) } diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt index cc4778f..150336f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt @@ -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() - 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() diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ImageAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ImageAccessorTest.kt index f7da592..21a3d00 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/ImageAccessorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/ImageAccessorTest.kt @@ -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() - private val images = listOf(mock(), 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) diff --git a/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt index 87cfce0..2ddab15 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt @@ -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(), 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, images: List) = - mock().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(), 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())) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt index 1528d60..850cc9d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt @@ -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() - private val localSone = mock().apply { whenever(isLocal).thenReturn(true) } - private val album = mock().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().apply { whenever(id).thenReturn("swapped") } - val parentAlbum = mock() - 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().apply { whenever(id).thenReturn("swapped") } - val parentAlbum = mock() - 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(), notNullValue()) } + private fun setupLocalSone() { + whenever(sone.isLocal).thenReturn(true) + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt index 26436a3..e63bb8a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt @@ -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() val sone = mock() - 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().apply { whenever(id).thenReturn("image-id") } val sone = mock().apply { whenever(isLocal).thenReturn(true) } - whenever(image.sone).thenReturn(sone) - val swapped = mock().apply { whenever(id).thenReturn("swapped") } - val album = mock() - 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().apply { whenever(id).thenReturn("image-id") } val sone = mock().apply { whenever(isLocal).thenReturn(true) } - whenever(image.sone).thenReturn(sone) - val swapped = mock().apply { whenever(id).thenReturn("swapped") } - val album = mock() - 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().apply { whenever(id).thenReturn("image-id") } val sone = mock().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") diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt index 55b3d70..9830ffa 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteAlbumPageTest.kt @@ -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() - private val album = mock() - private val parentAlbum = mock() + 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() 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) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt index cecde50..eccf949 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DeleteImagePageTest.kt @@ -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() private val sone = mock() + private val image = ImageImpl("image-id").modify().setSone(sone).update()!! @Before fun setupImage() { - val album = mock() - 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) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt index 72e814b..97e8865 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditAlbumPageTest.kt @@ -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() - private val parentAlbum = mock() - private val modifier = mockBuilder() - private val sone = mock() + 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() } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt index e43551c..5f59031 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditImagePageTest.kt @@ -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() - private val modifier = mockBuilder() private val sone = mock() - private val album = mock() + 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() 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() } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt index 74c3851..ac909ea 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/EditProfilePageTest.kt @@ -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() - 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() - 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 } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt index 38ecce4..4a2304c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt @@ -34,7 +34,7 @@ class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) { @Test fun `get request with album sets album and page in template context`() { - val album = mock() + 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() + val image = ImageImpl() addImage("image-id", image) addHttpRequestParameter("image", "image-id") verifyNoRedirect { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt index 14cae74..23987b6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt @@ -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() }) whenever(this.replies).thenReturn((0..(replies - 1)).map { mock() }.toSet()) - val album = mock() - whenever(album.images).thenReturn(((0..(images - 1)).map { mock() })) - val rootAlbum = mock().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()) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt index 370c0a7..4c99759 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt @@ -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().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() - val image = mock().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")) } } -- 2.7.4