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.createIdentity
21 import org.hamcrest.MatcherAssert.assertThat
22 import org.hamcrest.Matchers.containsInAnyOrder
23 import org.hamcrest.Matchers.empty
25 import org.junit.Before
29 * Unit test for [IdentityChangeDetector].
31 class IdentityChangeDetectorTest {
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>()
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) }
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()))
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()))
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()))
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()))
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()))
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()))
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()))
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()))
130 fun `no removed identities are detected without an identity processor`() {
131 identityChangeDetector.onRemovedIdentity(null)
132 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3()))
133 assertThat(removedIdentities, empty())
137 fun `no added identities are detected without an identity processor`() {
138 identityChangeDetector.onNewIdentity(null)
139 identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()))
140 assertThat(newIdentities, empty())
143 private fun createOldIdentities(): Collection<Identity> {
144 return listOf(createIdentity1(), createIdentity2(), createIdentity3())
147 private fun createIdentity1(): Identity {
148 return createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B"))
151 private fun createIdentity2(): Identity {
152 return createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D"))
155 private fun createIdentity3(): Identity {
156 return createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F"))
159 private fun createIdentity4(): Identity {
160 return createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H"))