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.Mockito.inOrder
16 import org.mockito.Mockito.verify
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 with debug set to false`() {
141 val core = createCore()
142 assertThat(core.debug, equalTo(false))
146 fun `debug flag can be set`() {
147 val core = createCore()
149 assertThat(core.debug, equalTo(true))
153 fun `setting debug flag posts event to event bus`() {
154 val eventBus = mock<EventBus>()
155 val core = createCore(eventBus)
157 verify(eventBus).post(argThat(instanceOf(DebugActivatedEvent::class.java)))
160 private fun createCore(eventBus: EventBus = mock()): Core {
161 val configuration = mock<Configuration>()
162 val freenetInterface = mock<FreenetInterface>()
163 val identityManager = mock<IdentityManager>()
164 val soneDownloader = mock<SoneDownloader>()
165 val imageInserter = mock<ImageInserter>()
166 val updateChecker = mock<UpdateChecker>()
167 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
168 val database = mock<Database>()
169 return Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)