From: David ‘Bombe’ Roden Date: Thu, 11 Jul 2019 09:25:51 +0000 (+0200) Subject: 🎨 Replace CoreTest with Kotlin version X-Git-Tag: v81^2~194 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6a3e5c2e9e69ff442a4a64c505345087317e0726;p=Sone.git 🎨 Replace CoreTest with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/core/CoreTest.java b/src/test/java/net/pterodactylus/sone/core/CoreTest.java deleted file mode 100644 index 85f981c..0000000 --- a/src/test/java/net/pterodactylus/sone/core/CoreTest.java +++ /dev/null @@ -1,161 +0,0 @@ -package net.pterodactylus.sone.core; - -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.hamcrest.MockitoHamcrest.argThat; - -import net.pterodactylus.sone.core.Core.MarkPostKnown; -import net.pterodactylus.sone.core.Core.MarkReplyKnown; -import net.pterodactylus.sone.core.event.PostRemovedEvent; -import net.pterodactylus.sone.core.event.PostReplyRemovedEvent; -import net.pterodactylus.sone.core.event.SoneRemovedEvent; -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.database.Database; -import net.pterodactylus.sone.freenet.wot.Identity; -import net.pterodactylus.sone.freenet.wot.IdentityManager; -import net.pterodactylus.sone.freenet.wot.OwnIdentity; -import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; -import net.pterodactylus.util.config.Configuration; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.eventbus.EventBus; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; -import org.junit.Test; -import org.mockito.InOrder; - -/** - * Unit test for {@link Core} and its subclasses. - */ -public class CoreTest { - - @Test - public void markPostKnownMarksPostAsKnown() { - Core core = mock(Core.class); - Post post = mock(Post.class); - MarkPostKnown markPostKnown = core.new MarkPostKnown(post); - markPostKnown.run(); - verify(core).markPostKnown(eq(post)); - } - - @Test - public void markReplyKnownMarksReplyAsKnown() { - Core core = mock(Core.class); - PostReply postReply = mock(PostReply.class); - MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply); - markReplyKnown.run(); - verify(core).markReplyKnown(eq(postReply)); - } - - @Test - public void removingAnIdentitySendsRemovalEventsForAllSoneElements() { - // given - Configuration configuration = mock(Configuration.class); - FreenetInterface freenetInterface = mock(FreenetInterface.class); - IdentityManager identityManager = mock(IdentityManager.class); - SoneDownloader soneDownloader = mock(SoneDownloader.class); - ImageInserter imageInserter = mock(ImageInserter.class); - UpdateChecker updateChecker = mock(UpdateChecker.class); - WebOfTrustUpdater webOfTrustUpdater = mock(WebOfTrustUpdater.class); - EventBus eventBus = mock(EventBus.class); - Database database = mock(Database.class); - Core core = new Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database); - OwnIdentity ownIdentity = mock(OwnIdentity.class); - Identity identity = mock(Identity.class); - when(identity.getId()).thenReturn("sone-id"); - Sone sone = mock(Sone.class); - when(database.getSone("sone-id")).thenReturn(sone); - PostReply postReply1 = mock(PostReply.class); - PostReply postReply2 = mock(PostReply.class); - when(sone.getReplies()).thenReturn(ImmutableSet.of(postReply1, postReply2)); - Post post1 = mock(Post.class); - Post post2 = mock(Post.class); - when(sone.getPosts()).thenReturn(ImmutableList.of(post1, post2)); - - // when - core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity)); - - // then - InOrder inOrder = inOrder(eventBus, database); - inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1))); - inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2))); - inOrder.verify(eventBus).post(argThat(isPostRemoved(post1))); - inOrder.verify(eventBus).post(argThat(isPostRemoved(post2))); - inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone))); - inOrder.verify(database).removeSone(sone); - } - - private Matcher isPostRemoved(final Post post) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(Object item, Description mismatchDescription) { - if (!(item instanceof PostRemovedEvent)) { - mismatchDescription.appendText("is not PostRemovedEvent"); - return false; - } - if (((PostRemovedEvent) item).getPost() != post) { - mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).getPost()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is PostRemovedEvent and post is ").appendValue(post); - } - }; - } - - private Matcher isPostReplyRemoved(final PostReply postReply) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(Object item, Description mismatchDescription) { - if (!(item instanceof PostReplyRemovedEvent)) { - mismatchDescription.appendText("is not PostReplyRemovedEvent"); - return false; - } - if (((PostReplyRemovedEvent) item).postReply() != postReply) { - mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply); - } - }; - } - - private Matcher isSoneRemoved(final Sone sone) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(Object item, Description mismatchDescription) { - if (!(item instanceof SoneRemovedEvent)) { - mismatchDescription.appendText("is not SoneRemovedEvent"); - return false; - } - if (((SoneRemovedEvent) item).getSone() != sone) { - mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).getSone()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone); - } - }; - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/core/CoreTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/CoreTest.kt new file mode 100644 index 0000000..ff997aa --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/CoreTest.kt @@ -0,0 +1,138 @@ +package net.pterodactylus.sone.core + +import com.google.common.collect.* +import com.google.common.eventbus.* +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.database.* +import net.pterodactylus.sone.freenet.wot.* +import net.pterodactylus.sone.freenet.wot.event.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.config.* +import org.hamcrest.* +import org.junit.* +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mockito.inOrder +import org.mockito.Mockito.verify +import org.mockito.hamcrest.MockitoHamcrest.* + +/** + * Unit test for [Core] and its subclasses. + */ +class CoreTest { + + @Test + fun `mark post known marks post as known`() { + val core = mock() + val post = mock() + core.MarkPostKnown(post).run() + verify(core).markPostKnown(eq(post)) + } + + @Test + fun `mark reply known marks reply as known`() { + val core = mock() + val postReply = mock() + core.MarkReplyKnown(postReply).run() + verify(core).markReplyKnown(eq(postReply)) + } + + @Test + fun `removing an identity sends removal events for all sone elements`() { + // given + val configuration = mock() + val freenetInterface = mock() + val identityManager = mock() + val soneDownloader = mock() + val imageInserter = mock() + val updateChecker = mock() + val webOfTrustUpdater = mock() + val eventBus = mock() + val database = mock() + val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database) + val ownIdentity = mock() + val identity = mock() + whenever(identity.id).thenReturn("sone-id") + val sone = mock() + whenever(database.getSone("sone-id")).thenReturn(sone) + val postReply1 = mock() + val postReply2 = mock() + whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2)) + val post1 = mock() + val post2 = mock() + whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2)) + + // when + core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity)) + + // then + val inOrder = inOrder(eventBus, database) + inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1))) + inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2))) + inOrder.verify(eventBus).post(argThat(isPostRemoved(post1))) + inOrder.verify(eventBus).post(argThat(isPostRemoved(post2))) + inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone))) + inOrder.verify(database).removeSone(sone) + } + + private fun isPostRemoved(post: Post): Matcher { + return object : TypeSafeDiagnosingMatcher() { + override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean { + if (item !is PostRemovedEvent) { + mismatchDescription.appendText("is not PostRemovedEvent") + return false + } + if (item.post !== post) { + mismatchDescription.appendText("post is ").appendValue(item.post) + return false + } + return true + } + + override fun describeTo(description: Description) { + description.appendText("is PostRemovedEvent and post is ").appendValue(post) + } + } + } + + private fun isPostReplyRemoved(postReply: PostReply): Matcher { + return object : TypeSafeDiagnosingMatcher() { + override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean { + if (item !is PostReplyRemovedEvent) { + mismatchDescription.appendText("is not PostReplyRemovedEvent") + return false + } + if (item.postReply !== postReply) { + mismatchDescription.appendText("post reply is ").appendValue(item.postReply) + return false + } + return true + } + + override fun describeTo(description: Description) { + description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply) + } + } + } + + private fun isSoneRemoved(sone: Sone): Matcher { + return object : TypeSafeDiagnosingMatcher() { + override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean { + if (item !is SoneRemovedEvent) { + mismatchDescription.appendText("is not SoneRemovedEvent") + return false + } + if (item.sone !== sone) { + mismatchDescription.appendText("sone is ").appendValue(item.sone) + return false + } + return true + } + + override fun describeTo(description: Description) { + description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone) + } + } + } + +}