From: David ‘Bombe’ Roden Date: Tue, 23 Jun 2020 04:31:52 +0000 (+0200) Subject: 🎨 Replace reply visibility filter with Kotlin version X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=eaf2f79d9489b46f6f6ee8c5ed83c4a4b0656a24;p=Sone.git 🎨 Replace reply visibility filter with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.java b/src/test/java/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.java deleted file mode 100644 index 1ba723b..0000000 --- a/src/test/java/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.java +++ /dev/null @@ -1,104 +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.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.freenet.wot.OwnIdentity; - -import com.google.common.base.Optional; -import com.google.inject.Guice; -import com.google.inject.Injector; -import org.junit.Test; - -/** - * Unit test for {@link ReplyVisibilityFilterTest}. - */ -public class ReplyVisibilityFilterTest { - - private static final String LOCAL_ID = "local-id"; - - private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class); - private final ReplyVisibilityFilter replyVisibilityFilter = new ReplyVisibilityFilter(postVisibilityFilter); - - private final Sone localSone = mock(Sone.class); - private final OwnIdentity localIdentity = mock(OwnIdentity.class); - private final Post post = mock(Post.class); - private final PostReply postReply = mock(PostReply.class); - - public ReplyVisibilityFilterTest() { - when(localSone.getId()).thenReturn(LOCAL_ID); - when(localSone.isLocal()).thenReturn(true); - when(localSone.getIdentity()).thenReturn(localIdentity); - when(post.getRecipientId()).thenReturn(Optional.absent()); - } - - @Test - public void replyVisibilityFilterIsOnlyCreatedOnce() { - Injector injector = Guice.createInjector(); - ReplyVisibilityFilter firstFilter = injector.getInstance(ReplyVisibilityFilter.class); - ReplyVisibilityFilter secondFilter = injector.getInstance(ReplyVisibilityFilter.class); - assertThat(firstFilter, sameInstance(secondFilter)); - } - - private void makePostPresent() { - when(postReply.getPost()).thenReturn(Optional.of(post)); - } - - @Test - public void replyIsNotVisibleIfPostIsNotVisible() { - makePostPresent(); - assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), is(false)); - } - - private void makePostAbsent() { - when(postReply.getPost()).thenReturn(Optional.absent()); - } - - @Test - public void replyIsNotVisibleIfPostIsNotPresent() { - makePostAbsent(); - assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), is(false)); - } - - private void makePostPresentAndVisible() { - makePostPresent(); - when(postVisibilityFilter.isPostVisible(localSone, post)).thenReturn(true); - } - - private void makeReplyComeFromFuture() { - when(postReply.getTime()).thenReturn(System.currentTimeMillis() + 1000); - } - - @Test - public void replyIsNotVisibleIfItIsFromTheFuture() { - makePostPresentAndVisible(); - makeReplyComeFromFuture(); - assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), is(false)); - } - - @Test - public void replyIsVisibleIfItIsNotFromTheFuture() { - makePostPresentAndVisible(); - assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), is(true)); - } - - @Test - public void predicateCorrectlyRecognizesVisibleReply() { - makePostPresentAndVisible(); - assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), is(true)); - } - - @Test - public void predicateCorrectlyRecognizesNotVisibleReply() { - makePostPresentAndVisible(); - makeReplyComeFromFuture(); - assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), is(false)); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt new file mode 100644 index 0000000..26e6f63 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt @@ -0,0 +1,101 @@ +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.PostReply +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.freenet.wot.OwnIdentity +import net.pterodactylus.sone.test.getInstance +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.sameInstance +import org.junit.Test + +/** + * Unit test for [ReplyVisibilityFilterTest]. + */ +class ReplyVisibilityFilterTest { + + private val postVisibilityFilter = mock() + private val replyVisibilityFilter = ReplyVisibilityFilter(postVisibilityFilter) + private val localSone = mock() + private val localIdentity = mock() + private val post = mock() + private val postReply = mock() + + @Test + fun `reply visibility filter is only created once`() { + val injector = Guice.createInjector() + val firstFilter = injector.getInstance() + val secondFilter = injector.getInstance() + assertThat(firstFilter, sameInstance(secondFilter)) + } + + private fun makePostPresent() { + whenever(postReply.post).thenReturn(Optional.of(post)) + } + + @Test + fun `reply is not visible if post is not visible`() { + makePostPresent() + assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), equalTo(false)) + } + + private fun makePostAbsent() { + whenever(postReply.post).thenReturn(Optional.absent()) + } + + @Test + fun `reply is not visible if post is not present`() { + makePostAbsent() + assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), equalTo(false)) + } + + private fun makePostPresentAndVisible() { + makePostPresent() + whenever(postVisibilityFilter.isPostVisible(localSone, post)).thenReturn(true) + } + + private fun makeReplyComeFromFuture() { + whenever(postReply.time).thenReturn(System.currentTimeMillis() + 1000) + } + + @Test + fun `reply is not visible if it is from the future`() { + makePostPresentAndVisible() + makeReplyComeFromFuture() + assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), equalTo(false)) + } + + @Test + fun `reply is visible if it is not from the future`() { + makePostPresentAndVisible() + assertThat(replyVisibilityFilter.isReplyVisible(localSone, postReply), equalTo(true)) + } + + @Test + fun `predicate correctly recognizes visible reply`() { + makePostPresentAndVisible() + assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), equalTo(true)) + } + + @Test + fun `predicate correctly recognizes not visible reply`() { + makePostPresentAndVisible() + makeReplyComeFromFuture() + assertThat(replyVisibilityFilter.isVisible(localSone).test(postReply), equalTo(false)) + } + + init { + whenever(localSone.id).thenReturn(LOCAL_ID) + whenever(localSone.isLocal).thenReturn(true) + whenever(localSone.identity).thenReturn(localIdentity) + whenever(post.recipientId).thenReturn(Optional.absent()) + } + +} + +private const val LOCAL_ID = "local-id"