1 package net.pterodactylus.sone.core
3 import com.codahale.metrics.*
4 import com.google.common.collect.*
5 import com.google.common.eventbus.*
6 import net.pterodactylus.sone.core.event.*
7 import net.pterodactylus.sone.data.*
8 import net.pterodactylus.sone.database.*
9 import net.pterodactylus.sone.freenet.wot.*
10 import net.pterodactylus.sone.freenet.wot.event.*
11 import net.pterodactylus.sone.test.*
12 import net.pterodactylus.util.config.*
14 import org.hamcrest.MatcherAssert.*
15 import org.hamcrest.Matchers.*
16 import org.mockito.Mockito.inOrder
17 import org.mockito.Mockito.verify
18 import org.mockito.hamcrest.MockitoHamcrest.*
22 * Unit test for [Core] and its subclasses.
27 fun `mark post known marks post as known`() {
28 val core = mock<Core>()
29 val post = mock<Post>()
30 core.MarkPostKnown(post).run()
31 verify(core).markPostKnown(eq(post))
35 fun `mark reply known marks reply as known`() {
36 val core = mock<Core>()
37 val postReply = mock<PostReply>()
38 core.MarkReplyKnown(postReply).run()
39 verify(core).markReplyKnown(eq(postReply))
43 fun `removing an identity sends removal events for all sone elements`() {
45 val configuration = mock<Configuration>()
46 val freenetInterface = mock<FreenetInterface>()
47 val identityManager = mock<IdentityManager>()
48 val soneDownloader = mock<SoneDownloader>()
49 val imageInserter = mock<ImageInserter>()
50 val updateChecker = mock<UpdateChecker>()
51 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
52 val eventBus = mock<EventBus>()
53 val database = mock<Database>()
54 val metricRegistry = MetricRegistry()
55 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry)
56 val ownIdentity = mock<OwnIdentity>()
57 val identity = mock<Identity>()
58 whenever(identity.id).thenReturn("sone-id")
59 val sone = mock<Sone>()
60 whenever(database.getSone("sone-id")).thenReturn(sone)
61 val postReply1 = mock<PostReply>()
62 val postReply2 = mock<PostReply>()
63 whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2))
64 val post1 = mock<Post>()
65 val post2 = mock<Post>()
66 whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2))
69 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
72 val inOrder = inOrder(eventBus, database)
73 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)))
74 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)))
75 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)))
76 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)))
77 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)))
78 inOrder.verify(database).removeSone(sone)
81 private fun isPostRemoved(post: Post): Matcher<Any> {
82 return object : TypeSafeDiagnosingMatcher<Any>() {
83 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
84 if (item !is PostRemovedEvent) {
85 mismatchDescription.appendText("is not PostRemovedEvent")
88 if (item.post !== post) {
89 mismatchDescription.appendText("post is ").appendValue(item.post)
95 override fun describeTo(description: Description) {
96 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
101 private fun isPostReplyRemoved(postReply: PostReply): Matcher<Any> {
102 return object : TypeSafeDiagnosingMatcher<Any>() {
103 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
104 if (item !is PostReplyRemovedEvent) {
105 mismatchDescription.appendText("is not PostReplyRemovedEvent")
108 if (item.postReply !== postReply) {
109 mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
115 override fun describeTo(description: Description) {
116 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
121 private fun isSoneRemoved(sone: Sone): Matcher<Any> {
122 return object : TypeSafeDiagnosingMatcher<Any>() {
123 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
124 if (item !is SoneRemovedEvent) {
125 mismatchDescription.appendText("is not SoneRemovedEvent")
128 if (item.sone !== sone) {
129 mismatchDescription.appendText("sone is ").appendValue(item.sone)
135 override fun describeTo(description: Description) {
136 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
142 fun `core starts with debug set to false`() {
143 val core = createCore()
144 assertThat(core.debug, equalTo(false))
148 fun `debug flag can be set`() {
149 val core = createCore()
151 assertThat(core.debug, equalTo(true))
155 fun `setting debug flag posts event to event bus`() {
156 val eventBus = mock<EventBus>()
157 val core = createCore(eventBus)
159 verify(eventBus).post(argThat(instanceOf(DebugActivatedEvent::class.java)))
162 private fun createCore(eventBus: EventBus = mock()): Core {
163 val configuration = mock<Configuration>()
164 val freenetInterface = mock<FreenetInterface>()
165 val identityManager = mock<IdentityManager>()
166 val soneDownloader = mock<SoneDownloader>()
167 val imageInserter = mock<ImageInserter>()
168 val updateChecker = mock<UpdateChecker>()
169 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
170 val database = mock<Database>()
171 val metricRegistry = MetricRegistry()
172 return Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry)