1 package net.pterodactylus.sone.core
3 import com.google.common.collect.*
4 import com.google.common.eventbus.*
5 import net.pterodactylus.sone.core.event.*
6 import net.pterodactylus.sone.data.*
7 import net.pterodactylus.sone.database.*
8 import net.pterodactylus.sone.freenet.wot.*
9 import net.pterodactylus.sone.freenet.wot.event.*
10 import net.pterodactylus.sone.test.*
11 import net.pterodactylus.util.config.*
14 import org.mockito.ArgumentMatchers.eq
15 import org.mockito.Mockito.inOrder
16 import org.mockito.Mockito.verify
17 import org.mockito.hamcrest.MockitoHamcrest.*
20 * Unit test for [Core] and its subclasses.
25 fun `mark post known marks post as known`() {
26 val core = mock<Core>()
27 val post = mock<Post>()
28 core.MarkPostKnown(post).run()
29 verify(core).markPostKnown(eq(post))
33 fun `mark reply known marks reply as known`() {
34 val core = mock<Core>()
35 val postReply = mock<PostReply>()
36 core.MarkReplyKnown(postReply).run()
37 verify(core).markReplyKnown(eq(postReply))
41 fun `removing an identity sends removal events for all sone elements`() {
43 val configuration = mock<Configuration>()
44 val freenetInterface = mock<FreenetInterface>()
45 val identityManager = mock<IdentityManager>()
46 val soneDownloader = mock<SoneDownloader>()
47 val imageInserter = mock<ImageInserter>()
48 val updateChecker = mock<UpdateChecker>()
49 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
50 val eventBus = mock<EventBus>()
51 val database = mock<Database>()
52 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
53 val ownIdentity = mock<OwnIdentity>()
54 val identity = mock<Identity>()
55 whenever(identity.id).thenReturn("sone-id")
56 val sone = mock<Sone>()
57 whenever(database.getSone("sone-id")).thenReturn(sone)
58 val postReply1 = mock<PostReply>()
59 val postReply2 = mock<PostReply>()
60 whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2))
61 val post1 = mock<Post>()
62 val post2 = mock<Post>()
63 whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2))
66 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
69 val inOrder = inOrder(eventBus, database)
70 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)))
71 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)))
72 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)))
73 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)))
74 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)))
75 inOrder.verify(database).removeSone(sone)
78 private fun isPostRemoved(post: Post): Matcher<Any> {
79 return object : TypeSafeDiagnosingMatcher<Any>() {
80 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
81 if (item !is PostRemovedEvent) {
82 mismatchDescription.appendText("is not PostRemovedEvent")
85 if (item.post !== post) {
86 mismatchDescription.appendText("post is ").appendValue(item.post)
92 override fun describeTo(description: Description) {
93 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
98 private fun isPostReplyRemoved(postReply: PostReply): Matcher<Any> {
99 return object : TypeSafeDiagnosingMatcher<Any>() {
100 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
101 if (item !is PostReplyRemovedEvent) {
102 mismatchDescription.appendText("is not PostReplyRemovedEvent")
105 if (item.postReply !== postReply) {
106 mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
112 override fun describeTo(description: Description) {
113 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
118 private fun isSoneRemoved(sone: Sone): Matcher<Any> {
119 return object : TypeSafeDiagnosingMatcher<Any>() {
120 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
121 if (item !is SoneRemovedEvent) {
122 mismatchDescription.appendText("is not SoneRemovedEvent")
125 if (item.sone !== sone) {
126 mismatchDescription.appendText("sone is ").appendValue(item.sone)
132 override fun describeTo(description: Description) {
133 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)