🎨 Replace CoreTest with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / CoreTest.kt
1 package net.pterodactylus.sone.core
2
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.*
12 import org.hamcrest.*
13 import org.junit.*
14 import org.mockito.ArgumentMatchers.eq
15 import org.mockito.Mockito.inOrder
16 import org.mockito.Mockito.verify
17 import org.mockito.hamcrest.MockitoHamcrest.*
18
19 /**
20  * Unit test for [Core] and its subclasses.
21  */
22 class CoreTest {
23
24         @Test
25         fun `mark post known marks post as known`() {
26                 val core = mock<Core>()
27                 val post = mock<Post>()
28                 core.MarkPostKnown(post).run()
29                 verify(core).markPostKnown(eq(post))
30         }
31
32         @Test
33         fun `mark reply known marks reply as known`() {
34                 val core = mock<Core>()
35                 val postReply = mock<PostReply>()
36                 core.MarkReplyKnown(postReply).run()
37                 verify(core).markReplyKnown(eq(postReply))
38         }
39
40         @Test
41         fun `removing an identity sends removal events for all sone elements`() {
42                 // given
43                 val configuration = mock<Configuration>()
44                 val freenetInterface = mock<FreenetInterface>()
45                 val identityManager = mock<IdentityManager>()
46                 val soneDownloader = mock<SoneDownloader>()
47                 val imageInserter = mock<ImageInserter>()
48                 val updateChecker = mock<UpdateChecker>()
49                 val webOfTrustUpdater = mock<WebOfTrustUpdater>()
50                 val eventBus = mock<EventBus>()
51                 val database = mock<Database>()
52                 val core = Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database)
53                 val ownIdentity = mock<OwnIdentity>()
54                 val identity = mock<Identity>()
55                 whenever(identity.id).thenReturn("sone-id")
56                 val sone = mock<Sone>()
57                 whenever(database.getSone("sone-id")).thenReturn(sone)
58                 val postReply1 = mock<PostReply>()
59                 val postReply2 = mock<PostReply>()
60                 whenever(sone.replies).thenReturn(ImmutableSet.of(postReply1, postReply2))
61                 val post1 = mock<Post>()
62                 val post2 = mock<Post>()
63                 whenever(sone.posts).thenReturn(ImmutableList.of(post1, post2))
64
65                 // when
66                 core.identityRemoved(IdentityRemovedEvent(ownIdentity, identity))
67
68                 // then
69                 val inOrder = inOrder(eventBus, database)
70                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)))
71                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)))
72                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)))
73                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)))
74                 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)))
75                 inOrder.verify(database).removeSone(sone)
76         }
77
78         private fun isPostRemoved(post: Post): Matcher<Any> {
79                 return object : TypeSafeDiagnosingMatcher<Any>() {
80                         override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
81                                 if (item !is PostRemovedEvent) {
82                                         mismatchDescription.appendText("is not PostRemovedEvent")
83                                         return false
84                                 }
85                                 if (item.post !== post) {
86                                         mismatchDescription.appendText("post is ").appendValue(item.post)
87                                         return false
88                                 }
89                                 return true
90                         }
91
92                         override fun describeTo(description: Description) {
93                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post)
94                         }
95                 }
96         }
97
98         private fun isPostReplyRemoved(postReply: PostReply): Matcher<Any> {
99                 return object : TypeSafeDiagnosingMatcher<Any>() {
100                         override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
101                                 if (item !is PostReplyRemovedEvent) {
102                                         mismatchDescription.appendText("is not PostReplyRemovedEvent")
103                                         return false
104                                 }
105                                 if (item.postReply !== postReply) {
106                                         mismatchDescription.appendText("post reply is ").appendValue(item.postReply)
107                                         return false
108                                 }
109                                 return true
110                         }
111
112                         override fun describeTo(description: Description) {
113                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply)
114                         }
115                 }
116         }
117
118         private fun isSoneRemoved(sone: Sone): Matcher<Any> {
119                 return object : TypeSafeDiagnosingMatcher<Any>() {
120                         override fun matchesSafely(item: Any, mismatchDescription: Description): Boolean {
121                                 if (item !is SoneRemovedEvent) {
122                                         mismatchDescription.appendText("is not SoneRemovedEvent")
123                                         return false
124                                 }
125                                 if (item.sone !== sone) {
126                                         mismatchDescription.appendText("sone is ").appendValue(item.sone)
127                                         return false
128                                 }
129                                 return true
130                         }
131
132                         override fun describeTo(description: Description) {
133                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone)
134                         }
135                 }
136         }
137
138 }