From eaf2f79d9489b46f6f6ee8c5ed83c4a4b0656a24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 23 Jun 2020 06:31:52 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20reply=20visibility=20fi?= =?utf8?q?lter=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/notify/ReplyVisibilityFilterTest.java | 104 --------------------- .../sone/notify/ReplyVisibilityFilterTest.kt | 101 ++++++++++++++++++++ 2 files changed, 101 insertions(+), 104 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt 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" -- 2.7.4