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.Lists.newArrayList;
21 import static java.util.Arrays.asList;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.containsInAnyOrder;
24 import static org.hamcrest.Matchers.empty;
26 import java.util.Collection;
29 import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
31 import com.google.common.collect.ImmutableMap;
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()));
158 private static Collection<Identity> createOldIdentities() {
159 return asList(createIdentity1(), createIdentity2(), createIdentity3());
162 private static Identity createIdentity1() {
163 return createIdentity("Test1", asList("Context A", "Context B"), ImmutableMap.of("Key A", "Value A", "Key B", "Value B"));
166 private static Identity createIdentity2() {
167 return createIdentity("Test2", asList("Context C", "Context D"), ImmutableMap.of("Key C", "Value C", "Key D", "Value D"));
170 private static Identity createIdentity3() {
171 return createIdentity("Test3", asList("Context E", "Context F"), ImmutableMap.of("Key E", "Value E", "Key F", "Value F"));
174 private static Identity createIdentity4() {
175 return createIdentity("Test4", asList("Context G", "Context H"), ImmutableMap.of("Key G", "Value G", "Key H", "Value H"));
178 private static Identity createIdentity(String id, Collection<String> contexts, Map<String, String> properties) {
179 DefaultIdentity identity = new DefaultIdentity(id, id, id);
180 identity.setContexts(contexts);
181 identity.setProperties(properties);