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 static com.google.common.collect.ImmutableMap.of;
21 import static com.google.common.collect.Lists.newArrayList;
22 import static java.util.Arrays.asList;
23 import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.containsInAnyOrder;
26 import static org.hamcrest.Matchers.empty;
28 import java.util.Collection;
30 import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
32 import org.junit.Before;
33 import org.junit.Test;
36 * Unit test for {@link IdentityChangeDetector}.
38 public class IdentityChangeDetectorTest {
40 private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities());
41 private final Collection<Identity> newIdentities = newArrayList();
42 private final Collection<Identity> removedIdentities = newArrayList();
43 private final Collection<Identity> changedIdentities = newArrayList();
44 private final Collection<Identity> unchangedIdentities = newArrayList();
48 identityChangeDetector.onNewIdentity(new IdentityProcessor() {
50 public void processIdentity(Identity identity) {
51 newIdentities.add(identity);
54 identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
56 public void processIdentity(Identity identity) {
57 removedIdentities.add(identity);
60 identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
62 public void processIdentity(Identity identity) {
63 changedIdentities.add(identity);
66 identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
68 public void processIdentity(Identity identity) {
69 unchangedIdentities.add(identity);
75 public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() {
76 identityChangeDetector.detectChanges(createOldIdentities());
77 assertThat(newIdentities, empty());
78 assertThat(removedIdentities, empty());
79 assertThat(changedIdentities, empty());
80 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
84 public void detectThatAnIdentityWasRemoved() {
85 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
86 assertThat(newIdentities, empty());
87 assertThat(removedIdentities, containsInAnyOrder(createIdentity2()));
88 assertThat(changedIdentities, empty());
89 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
93 public void detectThatAnIdentityWasAdded() {
94 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
95 assertThat(newIdentities, containsInAnyOrder(createIdentity4()));
96 assertThat(removedIdentities, empty());
97 assertThat(changedIdentities, empty());
98 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
102 public void detectThatAContextWasRemoved() {
103 Identity identity2 = createIdentity2();
104 identity2.removeContext("Context C");
105 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
106 assertThat(newIdentities, empty());
107 assertThat(removedIdentities, empty());
108 assertThat(changedIdentities, containsInAnyOrder(identity2));
109 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
113 public void detectThatAContextWasAdded() {
114 Identity identity2 = createIdentity2();
115 identity2.addContext("Context C1");
116 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
117 assertThat(newIdentities, empty());
118 assertThat(removedIdentities, empty());
119 assertThat(changedIdentities, containsInAnyOrder(identity2));
120 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
124 public void detectThatAPropertyWasRemoved() {
125 Identity identity1 = createIdentity1();
126 identity1.removeProperty("Key A");
127 identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3()));
128 assertThat(newIdentities, empty());
129 assertThat(removedIdentities, empty());
130 assertThat(changedIdentities, containsInAnyOrder(identity1));
131 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()));
135 public void detectThatAPropertyWasAdded() {
136 Identity identity3 = createIdentity3();
137 identity3.setProperty("Key A", "Value A");
138 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
139 assertThat(newIdentities, empty());
140 assertThat(removedIdentities, empty());
141 assertThat(changedIdentities, containsInAnyOrder(identity3));
142 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
146 public void detectThatAPropertyWasChanged() {
147 Identity identity3 = createIdentity3();
148 identity3.setProperty("Key E", "Value F");
149 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
150 assertThat(newIdentities, empty());
151 assertThat(removedIdentities, empty());
152 assertThat(changedIdentities, containsInAnyOrder(identity3));
153 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
157 public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
158 identityChangeDetector.onRemovedIdentity(null);
159 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
163 public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
164 identityChangeDetector.onNewIdentity(null);
165 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
168 private static Collection<Identity> createOldIdentities() {
169 return asList(createIdentity1(), createIdentity2(), createIdentity3());
172 private static Identity createIdentity1() {
173 return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
176 private static Identity createIdentity2() {
177 return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
180 private static Identity createIdentity3() {
181 return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
184 private static Identity createIdentity4() {
185 return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));