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