2 * Sone - IdentityChangeDetectorTest.java - Copyright © 2013 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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class IdentityChangeDetectorTest {
42 private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities());
43 private final Collection<Identity> newIdentities = newArrayList();
44 private final Collection<Identity> removedIdentities = newArrayList();
45 private final Collection<Identity> changedIdentities = newArrayList();
46 private final Collection<Identity> unchangedIdentities = newArrayList();
50 identityChangeDetector.onNewIdentity(new IdentityProcessor() {
52 public void processIdentity(Identity identity) {
53 newIdentities.add(identity);
56 identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
58 public void processIdentity(Identity identity) {
59 removedIdentities.add(identity);
62 identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
64 public void processIdentity(Identity identity) {
65 changedIdentities.add(identity);
68 identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
70 public void processIdentity(Identity identity) {
71 unchangedIdentities.add(identity);
77 public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() {
78 identityChangeDetector.detectChanges(createOldIdentities());
79 assertThat(newIdentities, empty());
80 assertThat(removedIdentities, empty());
81 assertThat(changedIdentities, empty());
82 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
86 public void detectThatAnIdentityWasRemoved() {
87 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
88 assertThat(newIdentities, empty());
89 assertThat(removedIdentities, containsInAnyOrder(createIdentity2()));
90 assertThat(changedIdentities, empty());
91 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
95 public void detectThatAnIdentityWasAdded() {
96 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
97 assertThat(newIdentities, containsInAnyOrder(createIdentity4()));
98 assertThat(removedIdentities, empty());
99 assertThat(changedIdentities, empty());
100 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
104 public void detectThatAContextWasRemoved() {
105 Identity identity2 = createIdentity2();
106 identity2.removeContext("Context C");
107 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
108 assertThat(newIdentities, empty());
109 assertThat(removedIdentities, empty());
110 assertThat(changedIdentities, containsInAnyOrder(identity2));
111 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
115 public void detectThatAContextWasAdded() {
116 Identity identity2 = createIdentity2();
117 identity2.addContext("Context C1");
118 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
119 assertThat(newIdentities, empty());
120 assertThat(removedIdentities, empty());
121 assertThat(changedIdentities, containsInAnyOrder(identity2));
122 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
126 public void detectThatAPropertyWasRemoved() {
127 Identity identity1 = createIdentity1();
128 identity1.removeProperty("Key A");
129 identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3()));
130 assertThat(newIdentities, empty());
131 assertThat(removedIdentities, empty());
132 assertThat(changedIdentities, containsInAnyOrder(identity1));
133 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()));
137 public void detectThatAPropertyWasAdded() {
138 Identity identity3 = createIdentity3();
139 identity3.setProperty("Key A", "Value A");
140 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
141 assertThat(newIdentities, empty());
142 assertThat(removedIdentities, empty());
143 assertThat(changedIdentities, containsInAnyOrder(identity3));
144 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
148 public void detectThatAPropertyWasChanged() {
149 Identity identity3 = createIdentity3();
150 identity3.setProperty("Key E", "Value F");
151 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
152 assertThat(newIdentities, empty());
153 assertThat(removedIdentities, empty());
154 assertThat(changedIdentities, containsInAnyOrder(identity3));
155 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
159 public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
160 identityChangeDetector.onRemovedIdentity(null);
161 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
165 public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
166 identityChangeDetector.onNewIdentity(null);
167 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
170 private static Collection<Identity> createOldIdentities() {
171 return asList(createIdentity1(), createIdentity2(), createIdentity3());
174 private static Identity createIdentity1() {
175 return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
178 private static Identity createIdentity2() {
179 return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
182 private static Identity createIdentity3() {
183 return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
186 private static Identity createIdentity4() {
187 return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));