🔖 Set version to 81
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / CoreTest.kt
1 package net.pterodactylus.sone.core
2
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.*
13 import org.hamcrest.*
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.*
19 import kotlin.test.*
20
21 /**
22  * Unit test for [Core] and its subclasses.
23  */
24 class CoreTest {
25
26         @Test
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))
32         }
33
34         @Test
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))
40         }
41
42         @Test
43         fun `removing an identity sends removal events for all sone elements`() {
44                 // given
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))
67
68                 // when
69                 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
70
71                 // then
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)
79         }
80
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")
86                                         return false
87                                 }
88                                 if (item.post !== post) {
89                                         mismatchDescription.appendText("post is ").appendValue(item.post)
90                                         return false
91                                 }
92                                 return true
93                         }
94
95                         override fun describeTo(description: Description) {
96                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
97                         }
98                 }
99         }
100
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")
106                                         return false
107                                 }
108                                 if (item.postReply !== postReply) {
109                                         mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
110                                         return false
111                                 }
112                                 return true
113                         }
114
115                         override fun describeTo(description: Description) {
116                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
117                         }
118                 }
119         }
120
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")
126                                         return false
127                                 }
128                                 if (item.sone !== sone) {
129                                         mismatchDescription.appendText("sone is ").appendValue(item.sone)
130                                         return false
131                                 }
132                                 return true
133                         }
134
135                         override fun describeTo(description: Description) {
136                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
137                         }
138                 }
139         }
140
141         @Test
142         fun `core starts with debug set to false`() {
143                 val core = createCore()
144                 assertThat(core.debug, equalTo(false))
145         }
146
147         @Test
148         fun `debug flag can be set`() {
149                 val core = createCore()
150                 core.setDebug()
151                 assertThat(core.debug, equalTo(true))
152         }
153
154         @Test
155         fun `setting debug flag posts event to event bus`() {
156                 val eventBus = mock<EventBus>()
157                 val core = createCore(eventBus)
158                 core.setDebug()
159                 verify(eventBus).post(argThat(instanceOf(DebugActivatedEvent::class.java)))
160         }
161
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)
173         }
174
175 }