🎨 Replace test for PostVisibilityFilter with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 18 Jun 2020 17:37:02 +0000 (19:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 18 Jun 2020 17:37:02 +0000 (19:37 +0200)
src/test/java/net/pterodactylus/sone/notify/PostVisibilityFilterTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/notify/PostVisibilityFilterTest.java b/src/test/java/net/pterodactylus/sone/notify/PostVisibilityFilterTest.java
deleted file mode 100644 (file)
index 21baa4e..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-package net.pterodactylus.sone.notify;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.sameInstance;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-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 com.google.common.base.Optional;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import org.junit.Test;
-
-/**
- * Unit test for {@link PostVisibilityFilterTest}.
- */
-public class PostVisibilityFilterTest {
-
-       private static final String LOCAL_ID = "local-id";
-       private static final String REMOTE_ID = "remote-id";
-
-       private final PostVisibilityFilter postVisibilityFilter = new PostVisibilityFilter();
-
-       private final Sone localSone = mock(Sone.class);
-       private final OwnIdentity localIdentity = mock(OwnIdentity.class);
-       private final Post post = mock(Post.class);
-       private final Sone remoteSone = mock(Sone.class);
-       private final Identity remoteIdentity = mock(Identity.class);
-
-       public PostVisibilityFilterTest() {
-               when(localSone.getId()).thenReturn(LOCAL_ID);
-               when(localSone.isLocal()).thenReturn(true);
-               when(localSone.getIdentity()).thenReturn(localIdentity);
-               when(localIdentity.getId()).thenReturn(LOCAL_ID);
-               when(remoteSone.getId()).thenReturn(REMOTE_ID);
-               when(remoteSone.getIdentity()).thenReturn(remoteIdentity);
-               when(remoteIdentity.getId()).thenReturn(REMOTE_ID);
-               when(post.getRecipientId()).thenReturn(Optional.<String>absent());
-       }
-
-       @Test
-       public void postVisibilityFilterIsOnlyCreatedOnce() {
-               Injector injector = Guice.createInjector();
-               PostVisibilityFilter firstFilter = injector.getInstance(PostVisibilityFilter.class);
-               PostVisibilityFilter secondFilter = injector.getInstance(PostVisibilityFilter.class);
-               assertThat(firstFilter, sameInstance(secondFilter));
-       }
-
-       @Test
-       public void postIsNotVisibleIfItIsNotLoaded() {
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
-       }
-
-       private static void makePostLoaded(Post post) {
-               when(post.isLoaded()).thenReturn(true);
-       }
-
-       @Test
-       public void loadedPostIsVisibleWithoutSone() {
-               makePostLoaded(post);
-               assertThat(postVisibilityFilter.isPostVisible(null, post), is(true));
-       }
-
-       private void makePostComeFromTheFuture() {
-               when(post.getTime()).thenReturn(System.currentTimeMillis() + 1000);
-       }
-
-       @Test
-       public void loadedPostFromTheFutureIsNotVisible() {
-               makePostLoaded(post);
-               makePostComeFromTheFuture();
-               assertThat(postVisibilityFilter.isPostVisible(null, post), is(false));
-       }
-
-       private void makePostFromRemoteSone() {
-               when(post.getSone()).thenReturn(remoteSone);
-       }
-
-       private void giveRemoteIdentityNegativeExplicitTrust() {
-               when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(-1, null, null));
-       }
-
-       @Test
-       public void loadedPostFromExplicitelyNotTrustedSoneIsNotVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               giveRemoteIdentityNegativeExplicitTrust();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
-       }
-
-       private void giveRemoteIdentityNegativeImplicitTrust() {
-               when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, -1, null));
-       }
-
-       @Test
-       public void loadedPostFromImplicitelyUntrustedSoneIsNotVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               giveRemoteIdentityNegativeImplicitTrust();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
-       }
-
-       private void makeLocalSoneFollowRemoteSone() {
-               when(localSone.hasFriend(REMOTE_ID)).thenReturn(true);
-       }
-
-       private void giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
-               when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(1, -1, null));
-       }
-
-       @Test
-       public void loadedPostFromExplicitelyTrustedButImplicitelyUntrustedSoneIsVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               makeLocalSoneFollowRemoteSone();
-               giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       private void giveTheRemoteIdentityPositiveImplicitTrust() {
-               when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, 1, null));
-       }
-
-       @Test
-       public void loadedPostFromImplicitelyTrustedSoneIsVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               makeLocalSoneFollowRemoteSone();
-               giveTheRemoteIdentityPositiveImplicitTrust();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       private void giveTheRemoteIdentityUnknownTrust() {
-               when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, null, null));
-       }
-
-       @Test
-       public void loadedPostFromSoneWithUnknownTrustIsVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               makeLocalSoneFollowRemoteSone();
-               giveTheRemoteIdentityUnknownTrust();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       @Test
-       public void loadedPostFromUnfollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsNotVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
-       }
-
-       private void makePostFromLocalSone() {
-               makePostLoaded(post);
-               when(post.getSone()).thenReturn(localSone);
-       }
-
-       @Test
-       public void loadedPostFromLocalSoneIsVisible() {
-               makePostFromLocalSone();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       @Test
-       public void loadedPostFromFollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               makeLocalSoneFollowRemoteSone();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       private void makePostDirectedAtLocalId() {
-               when(post.getRecipientId()).thenReturn(Optional.of(LOCAL_ID));
-       }
-
-       @Test
-       public void loadedPostFromRemoteSoneThatIsDirectedAtLocalSoneIsVisible() {
-               makePostLoaded(post);
-               makePostFromRemoteSone();
-               makePostDirectedAtLocalId();
-               assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
-       }
-
-       @Test
-       public void predicateWillCorrectlyRecognizeVisiblePost() {
-               makePostFromLocalSone();
-               assertThat(postVisibilityFilter.isVisible(null).apply(post), is(true));
-       }
-
-       @Test
-       public void predicateWillCorrectlyRecognizeNotVisiblePost() {
-               assertThat(postVisibilityFilter.isVisible(null).apply(post), is(false));
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/PostVisibilityFilterTest.kt
new file mode 100644 (file)
index 0000000..910f968
--- /dev/null
@@ -0,0 +1,195 @@
+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.verifySingletonInstance
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+/**
+ * Unit test for [PostVisibilityFilterTest].
+ */
+class PostVisibilityFilterTest {
+
+       private val postVisibilityFilter = PostVisibilityFilter()
+       private val localSone = mock<Sone>()
+       private val localIdentity = mock<OwnIdentity>()
+       private val post = mock<Post>()
+       private val remoteSone = mock<Sone>()
+       private val remoteIdentity = mock<Identity>()
+
+       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())
+       }
+
+       @Test
+       fun `post visibility filter is only created once`() {
+               val injector = Guice.createInjector()
+               injector.verifySingletonInstance<PostVisibilityFilter>()
+       }
+
+       @Test
+       fun `post is not visible if it is not loaded`() {
+               assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(false))
+       }
+
+       @Test
+       fun `loaded post is visible without sone`() {
+               makePostLoaded(post)
+               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()
+               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()
+               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()
+               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()
+               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()
+               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()
+               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()
+               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()
+               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()
+               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()
+               assertThat(postVisibilityFilter.isPostVisible(localSone, post), equalTo(true))
+       }
+
+       @Test
+       fun `predicate will correctly recognize visible post`() {
+               makePostFromLocalSone()
+               assertThat(postVisibilityFilter.isVisible(null).apply(post), equalTo(true))
+       }
+
+       @Test
+       fun `predicate will correctly recognize not visible post`() {
+               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)
+}