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