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