📄 Update year in file headers
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustPingerTest.kt
1 /**
2  * Sone - WebOfTrustPingerTest.kt - Copyright Â© 2019–2020 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 java.util.function.*
28 import kotlin.test.*
29
30 /**
31  * Unit test for [WebOfTrustPinger].
32  */
33 class WebOfTrustPingerTest {
34
35         private val eventBus = EventBus()
36         private val webOfTrustReachable = AtomicBoolean()
37         private val webOfTrustReacher = Runnable { webOfTrustReachable.get().onFalse { throw PluginException() } }
38         private val rescheduled = AtomicBoolean()
39         private val reschedule: Consumer<Runnable> = Consumer { if (it == pinger) rescheduled.set(true) }
40         private val pinger = WebOfTrustPinger(eventBus, webOfTrustReacher, reschedule)
41
42         @Test
43         fun `pinger sends wot appeared event when run first and wot is reachable`() {
44                 webOfTrustReachable.set(true)
45                 val appearedReceived = AtomicBoolean()
46                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
47                 pinger()
48                 assertThat(appearedReceived.get(), equalTo(true))
49         }
50
51         @Test
52         fun `pinger reschedules when wot is reachable`() {
53                 webOfTrustReachable.set(true)
54                 pinger()
55                 assertThat(rescheduled.get(), equalTo(true))
56         }
57
58         @Test
59         fun `pinger sends wot disappeared event when run first and wot is not reachable`() {
60                 val appearedReceived = AtomicBoolean()
61                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
62                 pinger()
63                 assertThat(appearedReceived.get(), equalTo(false))
64         }
65
66         @Test
67         fun `pinger reschedules when wot is not reachable`() {
68                 pinger()
69                 assertThat(rescheduled.get(), equalTo(true))
70         }
71
72         @Test
73         fun `pinger sends wot disappeared event when run twice and wot is not reachable on second call`() {
74                 val disappearedReceived = AtomicBoolean()
75                 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
76                 webOfTrustReachable.set(true)
77                 pinger()
78                 webOfTrustReachable.set(false)
79                 pinger()
80                 assertThat(disappearedReceived.get(), equalTo(true))
81         }
82
83         @Test
84         fun `pinger sends wot appeared event only once`() {
85                 webOfTrustReachable.set(true)
86                 val appearedReceived = AtomicBoolean()
87                 eventBus.register(WebOfTrustAppearedCatcher { appearedReceived.set(true) })
88                 pinger()
89                 appearedReceived.set(false)
90                 pinger()
91                 assertThat(appearedReceived.get(), equalTo(false))
92         }
93
94         @Test
95         fun `pinger sends wot disappeared event only once`() {
96                 val disappearedReceived = AtomicBoolean()
97                 eventBus.register(WebOfTrustDisappearedCatcher { disappearedReceived.set(true) })
98                 pinger()
99                 disappearedReceived.set(false)
100                 pinger()
101                 assertThat(disappearedReceived.get(), equalTo(false))
102         }
103
104 }
105
106 private class WebOfTrustAppearedCatcher(private val received: () -> Unit) {
107         @Subscribe
108         fun webOfTrustAppeared(@Suppress("UNUSED_PARAMETER") webOfTrustAppeared: WebOfTrustAppeared) {
109                 received()
110         }
111 }
112
113 private class WebOfTrustDisappearedCatcher(private val received: () -> Unit) {
114         @Subscribe
115         fun webOfTrustDisappeared(@Suppress("UNUSED_PARAMETER") webOfTrustDisappeared: WebOfTrustDisappeared) {
116                 received()
117         }
118 }