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 soneUriCreator = SoneUriCreator()
56 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry, soneUriCreator)
57 val ownIdentity = mock<OwnIdentity>()
58 val identity = mock<Identity>()
59 whenever(identity.id).thenReturn("sone-id")
60 val sone = mock<Sone>()
61 whenever(database.getSone("sone-id")).thenReturn(sone)
62 val postReply1 = mock<PostReply>()
63 val postReply2 = mock<PostReply>()
64 whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2))
65 val post1 = mock<Post>()
66 val post2 = mock<Post>()
67 whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2))
70 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
73 val inOrder = inOrder(eventBus, database)
74 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)))
75 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)))
76 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)))
77 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)))
78 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)))
79 inOrder.verify(database).removeSone(sone)
82 private fun isPostRemoved(post: Post): Matcher<Any> {
83 return object : TypeSafeDiagnosingMatcher<Any>() {
84 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
85 if (item !is PostRemovedEvent) {
86 mismatchDescription.appendText("is not PostRemovedEvent")
89 if (item.post !== post) {
90 mismatchDescription.appendText("post is ").appendValue(item.post)
96 override fun describeTo(description: Description) {
97 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
102 private fun isPostReplyRemoved(postReply: PostReply): Matcher<Any> {
103 return object : TypeSafeDiagnosingMatcher<Any>() {
104 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
105 if (item !is PostReplyRemovedEvent) {
106 mismatchDescription.appendText("is not PostReplyRemovedEvent")
109 if (item.postReply !== postReply) {
110 mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
116 override fun describeTo(description: Description) {
117 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
122 private fun isSoneRemoved(sone: Sone): Matcher<Any> {
123 return object : TypeSafeDiagnosingMatcher<Any>() {
124 override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
125 if (item !is SoneRemovedEvent) {
126 mismatchDescription.appendText("is not SoneRemovedEvent")
129 if (item.sone !== sone) {
130 mismatchDescription.appendText("sone is ").appendValue(item.sone)
136 override fun describeTo(description: Description) {
137 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
143 fun `core starts with debug set to false`() {
144 val core = createCore()
145 assertThat(core.debug, equalTo(false))
149 fun `debug flag can be set`() {
150 val core = createCore()
152 assertThat(core.debug, equalTo(true))
156 fun `setting debug flag posts event to event bus`() {
157 val eventBus = mock<EventBus>()
158 val core = createCore(eventBus)
160 verify(eventBus).post(argThat(instanceOf(DebugActivatedEvent::class.java)))
163 private fun createCore(eventBus: EventBus = mock()): Core {
164 val configuration = mock<Configuration>()
165 val freenetInterface = mock<FreenetInterface>()
166 val identityManager = mock<IdentityManager>()
167 val soneDownloader = mock<SoneDownloader>()
168 val imageInserter = mock<ImageInserter>()
169 val updateChecker = mock<UpdateChecker>()
170 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
171 val database = mock<Database>()
172 val metricRegistry = MetricRegistry()
173 val soneUriCreator = SoneUriCreator()
174 return Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database, metricRegistry, soneUriCreator)