Add Freenet plugin and WoT subprojects
[fwot.git] / wot / src / test / java / net / pterodactylus / freenet / wot / IdentityChangeDetectorTest.java
1 /*
2  * Sone - IdentityChangeDetectorTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.freenet.wot;
19
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.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;
27
28 import java.util.Collection;
29
30 import net.pterodactylus.freenet.wot.IdentityChangeDetector.IdentityProcessor;
31
32 import org.junit.Before;
33 import org.junit.Test;
34
35 /**
36  * Unit test for {@link IdentityChangeDetector}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class IdentityChangeDetectorTest {
41
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();
47
48         @Before
49         public void setup() {
50                 identityChangeDetector.onNewIdentity(newIdentities::add);
51                 identityChangeDetector.onRemovedIdentity(removedIdentities::add);
52                 identityChangeDetector.onChangedIdentity(changedIdentities::add);
53                 identityChangeDetector.onUnchangedIdentity(unchangedIdentities::add);
54         }
55
56         @Test
57         public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() {
58                 identityChangeDetector.detectChanges(createOldIdentities());
59                 assertThat(newIdentities, empty());
60                 assertThat(removedIdentities, empty());
61                 assertThat(changedIdentities, empty());
62                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
63         }
64
65         @Test
66         public void detectThatAnIdentityWasRemoved() {
67                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
68                 assertThat(newIdentities, empty());
69                 assertThat(removedIdentities, containsInAnyOrder(createIdentity2()));
70                 assertThat(changedIdentities, empty());
71                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
72         }
73
74         @Test
75         public void detectThatAnIdentityWasAdded() {
76                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
77                 assertThat(newIdentities, containsInAnyOrder(createIdentity4()));
78                 assertThat(removedIdentities, empty());
79                 assertThat(changedIdentities, empty());
80                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
81         }
82
83         @Test
84         public void detectThatAContextWasRemoved() {
85                 Identity identity2 = createIdentity2();
86                 identity2.removeContext("Context C");
87                 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
88                 assertThat(newIdentities, empty());
89                 assertThat(removedIdentities, empty());
90                 assertThat(changedIdentities, containsInAnyOrder(identity2));
91                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
92         }
93
94         @Test
95         public void detectThatAContextWasAdded() {
96                 Identity identity2 = createIdentity2();
97                 identity2.addContext("Context C1");
98                 identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
99                 assertThat(newIdentities, empty());
100                 assertThat(removedIdentities, empty());
101                 assertThat(changedIdentities, containsInAnyOrder(identity2));
102                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
103         }
104
105         @Test
106         public void detectThatAPropertyWasRemoved() {
107                 Identity identity1 = createIdentity1();
108                 identity1.removeProperty("Key A");
109                 identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3()));
110                 assertThat(newIdentities, empty());
111                 assertThat(removedIdentities, empty());
112                 assertThat(changedIdentities, containsInAnyOrder(identity1));
113                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()));
114         }
115
116         @Test
117         public void detectThatAPropertyWasAdded() {
118                 Identity identity3 = createIdentity3();
119                 identity3.setProperty("Key A", "Value A");
120                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
121                 assertThat(newIdentities, empty());
122                 assertThat(removedIdentities, empty());
123                 assertThat(changedIdentities, containsInAnyOrder(identity3));
124                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
125         }
126
127         @Test
128         public void detectThatAPropertyWasChanged() {
129                 Identity identity3 = createIdentity3();
130                 identity3.setProperty("Key E", "Value F");
131                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
132                 assertThat(newIdentities, empty());
133                 assertThat(removedIdentities, empty());
134                 assertThat(changedIdentities, containsInAnyOrder(identity3));
135                 assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
136         }
137
138         @Test
139         public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
140                 identityChangeDetector.onRemovedIdentity(null);
141                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
142         }
143
144         @Test
145         public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
146                 identityChangeDetector.onNewIdentity(null);
147                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
148         }
149
150         private static Collection<Identity> createOldIdentities() {
151                 return asList(createIdentity1(), createIdentity2(), createIdentity3());
152         }
153
154         private static Identity createIdentity1() {
155                 return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
156         }
157
158         private static Identity createIdentity2() {
159                 return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
160         }
161
162         private static Identity createIdentity3() {
163                 return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
164         }
165
166         private static Identity createIdentity4() {
167                 return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));
168         }
169
170 }