2 * Sone - IdentityChangeDetectorTest.java - Copyright © 2013–2019 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.wot
20 import net.pterodactylus.sone.freenet.wot.Identities.*
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
26 * Unit test for [IdentityChangeDetector].
28 class IdentityChangeDetectorTest {
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>()
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) }
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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())
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())
140 private fun createOldIdentities() =
141 listOf(createIdentity1(), createIdentity2(), createIdentity3())
143 private fun createIdentity1() =
144 createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B"))
146 private fun createIdentity2() =
147 createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D"))
149 private fun createIdentity3() =
150 createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F"))
152 private fun createIdentity4() =
153 createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H"))