🎨 Replace change detectors with Kotlin versions
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / freenet / wot / IdentityChangeDetectorTest.kt
1 /*
2  * Sone - IdentityChangeDetectorTest.java - Copyright Â© 2013–2019 David 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 net.pterodactylus.sone.freenet.wot.Identities.*
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
23 import org.junit.*
24
25 /**
26  * Unit test for [IdentityChangeDetector].
27  */
28 class IdentityChangeDetectorTest {
29
30         private val identityChangeDetector = IdentityChangeDetector(createOldIdentities())
31         private val newIdentities = mutableListOf<Identity>()
32         private val removedIdentities = mutableListOf<Identity>()
33         private val changedIdentities = mutableListOf<Identity>()
34         private val unchangedIdentities = mutableListOf<Identity>()
35
36         @Before
37         fun setup() {
38                 identityChangeDetector.onNewIdentity = { identity -> newIdentities.add(identity) }
39                 identityChangeDetector.onRemovedIdentity = { identity -> removedIdentities.add(identity) }
40                 identityChangeDetector.onChangedIdentity = { identity -> changedIdentities.add(identity) }
41                 identityChangeDetector.onUnchangedIdentity = { identity -> unchangedIdentities.add(identity) }
42         }
43
44         @Test
45         fun `no differences are detected when sending the old identities again`() {
46                 identityChangeDetector.detectChanges(createOldIdentities())
47                 assertThat(newIdentities, empty())
48                 assertThat(removedIdentities, empty())
49                 assertThat(changedIdentities, empty())
50                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()))
51         }
52
53         @Test
54         fun `detect that an identity was removed`() {
55                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3()))
56                 assertThat(newIdentities, empty())
57                 assertThat(removedIdentities, containsInAnyOrder(createIdentity2()))
58                 assertThat(changedIdentities, empty())
59                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()))
60         }
61
62         @Test
63         fun `detect that an identity was added`() {
64                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()))
65                 assertThat(newIdentities, containsInAnyOrder(createIdentity4()))
66                 assertThat(removedIdentities, empty())
67                 assertThat(changedIdentities, empty())
68                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()))
69         }
70
71         @Test
72         fun `detect that a context was removed`() {
73                 val identity2 = createIdentity2()
74                 identity2.removeContext("Context C")
75                 identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3()))
76                 assertThat(newIdentities, empty())
77                 assertThat(removedIdentities, empty())
78                 assertThat(changedIdentities, containsInAnyOrder(identity2))
79                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()))
80         }
81
82         @Test
83         fun `detect that a context was added`() {
84                 val identity2 = createIdentity2()
85                 identity2.addContext("Context C1")
86                 identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3()))
87                 assertThat(newIdentities, empty())
88                 assertThat(removedIdentities, empty())
89                 assertThat(changedIdentities, containsInAnyOrder(identity2))
90                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()))
91         }
92
93         @Test
94         fun `detect that a property was removed`() {
95                 val identity1 = createIdentity1()
96                 identity1.removeProperty("Key A")
97                 identityChangeDetector.detectChanges(listOf(identity1, createIdentity2(), createIdentity3()))
98                 assertThat(newIdentities, empty())
99                 assertThat(removedIdentities, empty())
100                 assertThat(changedIdentities, containsInAnyOrder(identity1))
101                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()))
102         }
103
104         @Test
105         fun `detect that a property was added`() {
106                 val identity3 = createIdentity3()
107                 identity3.setProperty("Key A", "Value A")
108                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3))
109                 assertThat(newIdentities, empty())
110                 assertThat(removedIdentities, empty())
111                 assertThat(changedIdentities, containsInAnyOrder(identity3))
112                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()))
113         }
114
115         @Test
116         fun `detect that a property was changed`() {
117                 val identity3 = createIdentity3()
118                 identity3.setProperty("Key E", "Value F")
119                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3))
120                 assertThat(newIdentities, empty())
121                 assertThat(removedIdentities, empty())
122                 assertThat(changedIdentities, containsInAnyOrder(identity3))
123                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()))
124         }
125
126         @Test
127         fun `no removed identities are detected without an identity processor`() {
128                 identityChangeDetector.onRemovedIdentity = null
129                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3()))
130                 assertThat(removedIdentities, empty())
131         }
132
133         @Test
134         fun `no added identities are detected without an identity processor`() {
135                 identityChangeDetector.onNewIdentity = null
136                 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()))
137                 assertThat(newIdentities, empty())
138         }
139
140         private fun createOldIdentities() =
141                         listOf(createIdentity1(), createIdentity2(), createIdentity3())
142
143         private fun createIdentity1() =
144                         createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B"))
145
146         private fun createIdentity2() =
147                         createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D"))
148
149         private fun createIdentity3() =
150                         createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F"))
151
152         private fun createIdentity4() =
153                         createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H"))
154
155 }