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 org.hamcrest.MatcherAssert.*
21 import org.hamcrest.Matchers.*
25 * Unit test for [IdentityChangeDetector].
27 class IdentityChangeDetectorTest {
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>()
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) }
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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())
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())
139 private fun createOldIdentities() =
140 listOf(createIdentity1(), createIdentity2(), createIdentity3())
142 private fun createIdentity1() =
143 createIdentity("Test1", setOf("Context A", "Context B"), "Key A" to "Value A", "Key B" to "Value B")
145 private fun createIdentity2() =
146 createIdentity("Test2", setOf("Context C", "Context D"), "Key C" to "Value C", "Key D" to "Value D")
148 private fun createIdentity3() =
149 createIdentity("Test3", setOf("Context E", "Context F"), "Key E" to "Value E", "Key F" to "Value F")
151 private fun createIdentity4() =
152 createIdentity("Test4", setOf("Context G", "Context H"), "Key G" to "Value G", "Key H" to "Value H")