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.*
13 import org.hamcrest.MatcherAssert.*
14 import org.hamcrest.Matchers.*
15 import org.mockito.ArgumentMatchers.eq
16 import org.mockito.Mockito.*
17 import org.mockito.hamcrest.MockitoHamcrest.*
21 * Unit test for [Core] and its subclasses.
26 fun `mark post known marks post as known`() {
27 val core = mock<Core>()
28 val post = mock<Post>()
29 core.MarkPostKnown(post).run()
30 verify(core).markPostKnown(eq(post))
34 fun `mark reply known marks reply as known`() {
35 val core = mock<Core>()
36 val postReply = mock<PostReply>()
37 core.MarkReplyKnown(postReply).run()
38 verify(core).markReplyKnown(eq(postReply))
42 fun `removing an identity sends removal events for all sone elements`() {
44 val configuration = mock<Configuration>()
45 val freenetInterface = mock<FreenetInterface>()
46 val identityManager = mock<IdentityManager>()
47 val soneDownloader = mock<SoneDownloader>()
48 val imageInserter = mock<ImageInserter>()
49 val updateChecker = mock<UpdateChecker>()
50 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
51 val eventBus = mock<EventBus>()
52 val database = mock<Database>()
53 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
54 val ownIdentity = mock<OwnIdentity>()
55 val identity = mock<Identity>()
56 whenever(identity.id).thenReturn("sone-id")
57 val sone = mock<Sone>()
58 whenever(database.getSone("sone-id")).thenReturn(sone)
59 val postReply1 = mock<PostReply>()
60 val postReply2 = mock<PostReply>()
61 whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2))
62 val post1 = mock<Post>()
63 val post2 = mock<Post>()
64 whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2))
67 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
70 val inOrder = inOrder(eventBus, database)
71 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)))
72 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)))
73 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)))
74 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)))
75 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)))
76 inOrder.verify(database).removeSone(sone)
79 private fun isPostRemoved(post: Post): Matcher<Any> {
80 return object : TypeSafeDiagnosingMatcher<Any>() {
81 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
82 if (item !is PostRemovedEvent) {
83 mismatchDescription.appendText("is not PostRemovedEvent")
86 if (item.post !== post) {
87 mismatchDescription.appendText("post is ").appendValue(item.post)
93 override fun describeTo(description: Description) {
94 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
99 private fun isPostReplyRemoved(postReply: PostReply): Matcher<Any> {
100 return object : TypeSafeDiagnosingMatcher<Any>() {
101 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
102 if (item !is PostReplyRemovedEvent) {
103 mismatchDescription.appendText("is not PostReplyRemovedEvent")
106 if (item.postReply !== postReply) {
107 mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
113 override fun describeTo(description: Description) {
114 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
119 private fun isSoneRemoved(sone: Sone): Matcher<Any> {
120 return object : TypeSafeDiagnosingMatcher<Any>() {
121 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
122 if (item !is SoneRemovedEvent) {
123 mismatchDescription.appendText("is not SoneRemovedEvent")
126 if (item.sone !== sone) {
127 mismatchDescription.appendText("sone is ").appendValue(item.sone)
133 override fun describeTo(description: Description) {
134 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
140 fun `core starts without debug flags`() {
141 val configuration = mock<Configuration>()
142 val freenetInterface = mock<FreenetInterface>()
143 val identityManager = mock<IdentityManager>()
144 val soneDownloader = mock<SoneDownloader>()
145 val imageInserter = mock<ImageInserter>()
146 val updateChecker = mock<UpdateChecker>()
147 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
148 val eventBus = mock<EventBus>()
149 val database = mock<Database>()
150 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
151 assertThat(core.debugInformation.showVersionInformation, equalTo(false))