🔀 Merge branch 'release/v82'
[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 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))
68
69                 // when
70                 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
71
72                 // then
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)
80         }
81
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")
87                                         return false
88                                 }
89                                 if (item.post !== post) {
90                                         mismatchDescription.appendText("post is ").appendValue(item.post)
91                                         return false
92                                 }
93                                 return true
94                         }
95
96                         override fun describeTo(description: Description) {
97                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
98                         }
99                 }
100         }
101
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")
107                                         return false
108                                 }
109                                 if (item.postReply !== postReply) {
110                                         mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
111                                         return false
112                                 }
113                                 return true
114                         }
115
116                         override fun describeTo(description: Description) {
117                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
118                         }
119                 }
120         }
121
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")
127                                         return false
128                                 }
129                                 if (item.sone !== sone) {
130                                         mismatchDescription.appendText("sone is ").appendValue(item.sone)
131                                         return false
132                                 }
133                                 return true
134                         }
135
136                         override fun describeTo(description: Description) {
137                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
138                         }
139                 }
140         }
141
142         @Test
143         fun `core starts with debug set to false`() {
144                 val core = createCore()
145                 assertThat(core.debug, equalTo(false))
146         }
147
148         @Test
149         fun `debug flag can be set`() {
150                 val core = createCore()
151                 core.setDebug()
152                 assertThat(core.debug, equalTo(true))
153         }
154
155         @Test
156         fun `setting debug flag posts event to event bus`() {
157                 val eventBus = mock<EventBus>()
158                 val core = createCore(eventBus)
159                 core.setDebug()
160                 verify(eventBus).post(argThat(instanceOf(DebugActivatedEvent::class.java)))
161         }
162
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)
175         }
176
177 }