From 1a2e2a402b62923a68b09eea463ca3952c5505b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 19 Nov 2016 00:00:59 +0100 Subject: [PATCH] Add unit test for Sone accessor --- .../sone/template/SoneAccessorTest.kt | 235 +++++++++++++++++++++ .../org.mockito.plugins.MockMaker | 1 + 2 files changed, 236 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt create mode 100644 src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt new file mode 100644 index 0000000..2f03dee --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/SoneAccessorTest.kt @@ -0,0 +1,235 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image +import net.pterodactylus.sone.data.Profile +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.data.Sone.SoneStatus +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.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.util.template.TemplateContext +import org.hamcrest.Matcher +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test + +/** + * Unit test for [SoneAccessor]. + */ +class SoneAccessorTest { + + private val core = mock() + private val accessor = SoneAccessor(core) + private val templateContext = mock() + private val currentSone = mock() + private val currentIdentity = mock() + private val sone = mock() + private val remoteIdentity = mock() + + @Before + fun setupSone() { + whenever(sone.id).thenReturn("sone-id") + whenever(sone.name).thenReturn("sone-name") + whenever(sone.profile).thenReturn(Profile(sone)) + whenever(sone.identity).thenReturn(remoteIdentity) + whenever(currentSone.identity).thenReturn(currentIdentity) + } + + @Before + fun setupTemplateContext() { + whenever(templateContext["currentSone"]).thenReturn(currentSone) + } + + private fun assertAccessorReturnValue(member: String, expected: Any?) { + assertThat(accessor.get(templateContext, sone, member), equalTo(expected)) + } + + private fun assertAccessorReturnValueMatches(member: String, matcher: Matcher) { + assertThat(accessor.get(templateContext, sone, member) as T, matcher) + } + + @Test + fun `accessor returns nice name of a sone`() { + assertAccessorReturnValue("niceName", "sone-name") + } + + @Test + fun `accessor returns that given sone is not a friend of the current sone if there is no current sone`() { + whenever(templateContext["currentSone"]).thenReturn(null) + assertAccessorReturnValue("friend", false) + } + + @Test + fun `accessor returns that given sone is not a friend of the current sone if the given sone is not a friend`() { + assertAccessorReturnValue("friend", false) + } + + @Test + fun `accessor returns that given sone is a friend of the current sone if the given sone is a friend`() { + whenever(currentSone.hasFriend("sone-id")).thenReturn(true) + assertAccessorReturnValue("friend", true) + } + + @Test + fun `accessor returns that the given sone is not the current sone if there is no current sone`() { + whenever(templateContext["currentSone"]).thenReturn(null) + assertAccessorReturnValue("current", false) + } + + @Test + fun `accessor returns that the given sone is not the current sone if it is not`() { + assertAccessorReturnValue("current", false) + } + + @Test + fun `accessor returns that the given sone is the current sone if it is `() { + whenever(templateContext["currentSone"]).thenReturn(sone) + assertAccessorReturnValue("current", true) + } + + @Test + fun `accessor returns that a sone was not modified if the sone was not modified`() { + assertAccessorReturnValue("modified", false) + } + + @Test + fun `accessor returns that a sone was modified if the sone was modified`() { + whenever(core.isModifiedSone(sone)).thenReturn(true) + assertAccessorReturnValue("modified", true) + } + + @Test + fun `accessor returns the sone’s status`() { + val soneStatus = mock() + whenever(sone.status).thenReturn(soneStatus) + assertAccessorReturnValue("status", soneStatus) + } + + @Test + fun `accessor returns that the sone’s status is unknown if it is unknown`() { + whenever(sone.status).thenReturn(unknown) + assertAccessorReturnValue("unknown", true) + } + + @Test + fun `accessor returns that the sone’s status is not unknown if it is not unknown`() { + whenever(sone.status).thenReturn(mock()) + assertAccessorReturnValue("unknown", false) + } + + @Test + fun `accessor returns that the sone’s status is idle if it is idle`() { + whenever(sone.status).thenReturn(idle) + assertAccessorReturnValue("idle", true) + } + + @Test + fun `accessor returns that the sone’s status is not idle if it is not idle`() { + whenever(sone.status).thenReturn(mock()) + assertAccessorReturnValue("idle", false) + } + + @Test + fun `accessor returns that the sone’s status is inserting if it is inserting`() { + whenever(sone.status).thenReturn(inserting) + assertAccessorReturnValue("inserting", true) + } + + @Test + fun `accessor returns that the sone’s status is not inserting if it is not inserting`() { + whenever(sone.status).thenReturn(mock()) + assertAccessorReturnValue("inserting", false) + } + + @Test + fun `accessor returns that the sone’s status is downloading if it is downloading`() { + whenever(sone.status).thenReturn(downloading) + assertAccessorReturnValue("downloading", true) + } + + @Test + fun `accessor returns that the sone’s status is not downloading if it is not downloading`() { + whenever(sone.status).thenReturn(mock()) + assertAccessorReturnValue("downloading", false) + } + + @Test + fun `accessor returns that the sone is new if it is not known`() { + assertAccessorReturnValue("new", true) + } + + @Test + fun `accessor returns that the sone is not new if it is known`() { + whenever(sone.isKnown).thenReturn(true) + assertAccessorReturnValue("new", false) + } + + @Test + fun `accessor returns that the sone is not locked if it is not locked`() { + assertAccessorReturnValue("locked", false) + } + + @Test + fun `accessor returns that the sone is locked if it is locked`() { + whenever(core.isLocked(sone)).thenReturn(true) + assertAccessorReturnValue("locked", true) + } + + @Test + fun `accessor returns null trust if there is no current sone`() { + whenever(templateContext["currentSone"]).thenReturn(null) + assertAccessorReturnValue("trust", null) + } + + @Test + fun `accessor returns trust with null values if there is no trust from the current sone`() { + assertAccessorReturnValue("trust", Trust(null, null, null)) + } + + @Test + fun `accessor returns trust if there is trust from the current sone`() { + val trust = mock() + whenever(remoteIdentity.getTrust(currentIdentity)).thenReturn(trust) + assertAccessorReturnValue("trust", trust) + } + + @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()) + 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) + } + + @Test + fun `accessor returns all albums in the correct order`() { + val albums = listOf(mock(), mock(), mock(), mock(), mock()) + val rootAlbum = createAlbum(albums, listOf()) + whenever(sone.rootAlbum).thenReturn(rootAlbum) + assertAccessorReturnValueMatches("albums", contains(*albums.toTypedArray())) + } + + @Test + fun `reflection accessor is used for other members`() { + assertAccessorReturnValue("hashCode", sone.hashCode()) + } + +} diff --git a/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000..1f0955d --- /dev/null +++ b/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline -- 2.7.4