🚧 Add web of trust pinger and events
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustPingerTest.kt
1 /**
2  * Sone - WebOfTrustPinger.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.freenet.plugin.*
23 import net.pterodactylus.sone.utils.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import java.util.concurrent.atomic.*
27 import kotlin.test.*
28
29 /**
30  * Unit test for [WebOfTrustPinger].
31  */
32 class WebOfTrustPingerTest {
33
34         private val eventBus = EventBus()
35         private val webOfTrustReachable = AtomicBoolean()
36         private val webOfTrustReacher: () -> Unit = { webOfTrustReachable.get().onFalse { throw PluginException() } }
37         private val rescheduled = AtomicBoolean()
38         private val reschedule: () -> Unit = { rescheduled.set(true) }
39         private val pinger = WebOfTrustPinger(eventBus, webOfTrustReacher, reschedule)
40
41         @Test
42         fun `pinger sends wot appeared event when run first and wot is reachable`() {
43                 webOfTrustReachable.set(true)
44                 val appearedReceived = AtomicBoolean()
45                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
46                 pinger()
47                 assertThat(appearedReceived.get(), equalTo(true))
48         }
49
50         @Test
51         fun `pinger reschedules when wot is reachable`() {
52                 webOfTrustReachable.set(true)
53                 pinger()
54                 assertThat(rescheduled.get(), equalTo(true))
55         }
56
57         @Test
58         fun `pinger sends wot disappeared event when run first and wot is not reachable`() {
59                 val appearedReceived = AtomicBoolean()
60                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
61                 pinger()
62                 assertThat(appearedReceived.get(), equalTo(false))
63         }
64
65         @Test
66         fun `pinger reschedules when wot is not reachable`() {
67                 pinger()
68                 assertThat(rescheduled.get(), equalTo(true))
69         }
70
71         @Test
72         fun `pinger sends wot disappeared event when run twice and wot is not reachable on second call`() {
73                 val disappearedReceived = AtomicBoolean()
74                 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
75                 webOfTrustReachable.set(true)
76                 pinger()
77                 webOfTrustReachable.set(false)
78                 pinger()
79                 assertThat(disappearedReceived.get(), equalTo(true))
80         }
81
82         @Test
83         fun `pinger sends wot appeared event only once`() {
84                 webOfTrustReachable.set(true)
85                 val appearedReceived = AtomicBoolean()
86                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
87                 pinger()
88                 appearedReceived.set(false)
89                 pinger()
90                 assertThat(appearedReceived.get(), equalTo(false))
91         }
92
93         @Test
94         fun `pinger sends wot disappeared event only once`() {
95                 val disappearedReceived = AtomicBoolean()
96                 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
97                 pinger()
98                 disappearedReceived.set(false)
99                 pinger()
100                 assertThat(disappearedReceived.get(), equalTo(false))
101         }
102
103 }
104
105 private class WebOfTrustAppearedCatcher(private val received: () -> Unit) {
106         @Subscribe
107         fun webOfTrustAppeared(webOfTrustAppeared: WebOfTrustAppeared) {
108                 received()
109         }
110 }
111
112 private class WebOfTrustDisappearedCatcher(private val received: () -> Unit) {
113         @Subscribe
114         fun webOfTrustDisappeared(webOfTrustDisappeared: WebOfTrustDisappeared) {
115                 received()
116         }
117 }