🎨 Reduce mocking of albums and images
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / SoneAccessorTest.kt
index 2f03dee..2ddab15 100644 (file)
@@ -10,11 +10,15 @@ 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
 import net.pterodactylus.sone.freenet.wot.Trust
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.text.TimeText
+import net.pterodactylus.sone.text.TimeTextConverter
 import net.pterodactylus.util.template.TemplateContext
 import org.hamcrest.Matcher
 import org.hamcrest.MatcherAssert.assertThat
@@ -29,7 +33,8 @@ import org.junit.Test
 class SoneAccessorTest {
 
        private val core = mock<Core>()
-       private val accessor = SoneAccessor(core)
+       private val timeTextConverter = mock<TimeTextConverter>()
+       private val accessor = SoneAccessor(core, timeTextConverter)
        private val templateContext = mock<TemplateContext>()
        private val currentSone = mock<Sone>()
        private val currentIdentity = mock<OwnIdentity>()
@@ -54,6 +59,7 @@ class SoneAccessorTest {
                assertThat(accessor.get(templateContext, sone, member), equalTo(expected))
        }
 
+       @Suppress("UNCHECKED_CAST")
        private fun <T : Any> assertAccessorReturnValueMatches(member: String, matcher: Matcher<in T>) {
                assertThat(accessor.get(templateContext, sone, member) as T, matcher)
        }
@@ -123,7 +129,7 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns that the sone’s status is not unknown if it is not unknown`() {
-               whenever(sone.status).thenReturn(mock<SoneStatus>())
+               whenever(sone.status).thenReturn(mock())
                assertAccessorReturnValue("unknown", false)
        }
 
@@ -135,7 +141,7 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns that the sone’s status is not idle if it is not idle`() {
-               whenever(sone.status).thenReturn(mock<SoneStatus>())
+               whenever(sone.status).thenReturn(mock())
                assertAccessorReturnValue("idle", false)
        }
 
@@ -147,7 +153,7 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns that the sone’s status is not inserting if it is not inserting`() {
-               whenever(sone.status).thenReturn(mock<SoneStatus>())
+               whenever(sone.status).thenReturn(mock())
                assertAccessorReturnValue("inserting", false)
        }
 
@@ -159,7 +165,7 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns that the sone’s status is not downloading if it is not downloading`() {
-               whenever(sone.status).thenReturn(mock<SoneStatus>())
+               whenever(sone.status).thenReturn(mock())
                assertAccessorReturnValue("downloading", false)
        }
 
@@ -186,6 +192,13 @@ class SoneAccessorTest {
        }
 
        @Test
+       fun `accessor returns l10n text for last update time`() {
+               whenever(sone.time).thenReturn(12345)
+               whenever(timeTextConverter.getTimeText(12345L)).thenReturn(TimeText(L10nText("l10n.key", listOf(3L)), 23456))
+               assertAccessorReturnValue("lastUpdatedText", L10nText("l10n.key", listOf(3L)))
+       }
+
+       @Test
        fun `accessor returns null trust if there is no current sone`() {
                whenever(templateContext["currentSone"]).thenReturn(null)
                assertAccessorReturnValue("trust", null)
@@ -205,24 +218,24 @@ class SoneAccessorTest {
 
        @Test
        fun `accessor returns all images in the correct order`() {
-               val images = listOf(mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>())
-               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<Album>(), mock<Album>(), mock<Album>(), mock<Album>())
-               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()))
        }