From: David ‘Bombe’ Roden Date: Thu, 18 Jun 2020 21:18:37 +0000 (+0200) Subject: ♻️ Rewrite test to avoid mocks X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=7d0f3c2559da8161f79b06280ed91ceda226ee16 ♻️ Rewrite test to avoid mocks --- diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt index 910f968..549b06e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt @@ -1,15 +1,12 @@ package net.pterodactylus.sone.notify -import com.google.common.base.Optional import com.google.inject.Guice -import net.pterodactylus.sone.data.Post -import net.pterodactylus.sone.data.Sone -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.createLocalSone +import net.pterodactylus.sone.test.createPost +import net.pterodactylus.sone.test.createRemoteSone import net.pterodactylus.sone.test.verifySingletonInstance -import net.pterodactylus.sone.test.whenever import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Test @@ -20,22 +17,8 @@ import org.junit.Test class PostVisibilityFilterTest { private val postVisibilityFilter = PostVisibilityFilter() - private val localSone = mock() - private val localIdentity = mock() - private val post = mock() - private val remoteSone = mock() - private val remoteIdentity = mock() - - init { - whenever(localSone.id).thenReturn(LOCAL_ID) - whenever(localSone.isLocal).thenReturn(true) - whenever(localSone.identity).thenReturn(localIdentity) - whenever(localIdentity.id).thenReturn(LOCAL_ID) - whenever(remoteSone.id).thenReturn(REMOTE_ID) - whenever(remoteSone.identity).thenReturn(remoteIdentity) - whenever(remoteIdentity.id).thenReturn(REMOTE_ID) - whenever(post.recipientId).thenReturn(Optional.absent()) - } + private val localSone = createLocalSone() + private val remoteSone = createRemoteSone() @Test fun `post visibility filter is only created once`() { @@ -45,151 +28,96 @@ class PostVisibilityFilterTest { @Test fun `post is not visible if it is not loaded`() { - assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false)) + val post = createPost(loaded = false) + assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(false)) } @Test - fun `loaded post is visible without sone`() { - makePostLoaded(post) + fun `loaded post is visible without sone and in the past`() { + val post = createPost(sone = null) assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(true)) } - private fun makePostComeFromTheFuture() { - whenever(post.time).thenReturn(System.currentTimeMillis() + 1000) - } - @Test fun `loaded post from the future is not visible`() { - makePostLoaded(post) - makePostComeFromTheFuture() + // the offset for the future must be large enough to survive loading freenet.crypt.Util. + val post = createPost(time = System.currentTimeMillis() + 100000, sone = null) assertThat(postVisibilityFilter.isPostVisible(null, post), equalTo(false)) } - private fun makePostFromRemoteSone() { - whenever(post.sone).thenReturn(remoteSone) - } - - private fun giveRemoteIdentityNegativeExplicitTrust() { - whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(-1, null, null)) - } - @Test fun `loaded post from explicitely not trusted sone is not visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - giveRemoteIdentityNegativeExplicitTrust() + remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(-1, null, null)) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false)) } - private fun giveRemoteIdentityNegativeImplicitTrust() { - whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, -1, null)) - } - @Test fun `loaded post from implicitely untrusted sone is not visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - giveRemoteIdentityNegativeImplicitTrust() + remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, -1, null)) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false)) } - private fun makeLocalSoneFollowRemoteSone() { - whenever(localSone.hasFriend(REMOTE_ID)).thenReturn(true) - } - - private fun giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() { - whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(1, -1, null)) - } - @Test - fun `loaded post from explicitely trusted but implicitely untrusted sone is visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - makeLocalSoneFollowRemoteSone() - giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() + fun `loaded post from implicitely untrusted but followed sone is visible`() { + localSone.friends.add(remoteSone.id) + remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(1, -1, null)) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } - private fun giveTheRemoteIdentityPositiveImplicitTrust() { - whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, 1, null)) - } - @Test - fun `loaded post from implicitely trusted sone is visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - makeLocalSoneFollowRemoteSone() - giveTheRemoteIdentityPositiveImplicitTrust() + fun `loaded post from implicitely trusted and followed sone is visible`() { + localSone.friends.add(remoteSone.id) + remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, 1, null)) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } - private fun giveTheRemoteIdentityUnknownTrust() { - whenever(remoteIdentity.getTrust(localIdentity)).thenReturn(Trust(null, null, null)) - } - @Test - fun `loaded post from sone with unknown trust is visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - makeLocalSoneFollowRemoteSone() - giveTheRemoteIdentityUnknownTrust() + fun `loaded post from followed sone with unknown trust is visible`() { + localSone.friends.add(remoteSone.id) + remoteSone.identity.setTrust(localSone.identity as OwnIdentity, Trust(null, null, null)) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } @Test fun `loaded post from unfollowed remote sone that is not directed at local sone is not visible`() { - makePostLoaded(post) - makePostFromRemoteSone() + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false)) } - private fun makePostFromLocalSone() { - makePostLoaded(post) - whenever(post.sone).thenReturn(localSone) - } - @Test fun `loaded post from local sone is visible`() { - makePostFromLocalSone() + val post = createPost(sone = localSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } @Test fun `loaded post from followed remote sone that is not directed at local sone is visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - makeLocalSoneFollowRemoteSone() + localSone.friends.add(remoteSone.id) + val post = createPost(sone = remoteSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } - private fun makePostDirectedAtLocalId() { - whenever(post.recipientId).thenReturn(Optional.of(LOCAL_ID)) - } - @Test fun `loaded post from remote sone that is directed at local sone is visible`() { - makePostLoaded(post) - makePostFromRemoteSone() - makePostDirectedAtLocalId() + val post = createPost(sone = remoteSone, recipient = localSone) assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true)) } @Test fun `predicate will correctly recognize visible post`() { - makePostFromLocalSone() + val post = createPost(sone = localSone) assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(true)) } @Test fun `predicate will correctly recognize not visible post`() { + val post = createPost(loaded = false) assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(false)) } } - -private const val LOCAL_ID = "local-id" -private const val REMOTE_ID = "remote-id" - -private fun makePostLoaded(post: Post) { - whenever(post.isLoaded).thenReturn(true) -}