🔀 Merge branch “release-80”
[Sone.git] / src / test / java / net / pterodactylus / sone / freenet / wot / IdentityChangeDetectorTest.java
1 /*
2  * Sone - IdentityChangeDetectorTest.java - Copyright © 2013–2019 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.sone.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.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;
27
28 import java.util.Collection;
29
30 import net.pterodactylus.sone.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 public class IdentityChangeDetectorTest {
39
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();
45
46         @Before
47         public void setup() {
48                 identityChangeDetector.onNewIdentity(new IdentityProcessor() {
49                         @Override
50                         public void processIdentity(Identity identity) {
51                                 newIdentities.add(identity);
52                         }
53                 });
54                 identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
55                         @Override
56                         public void processIdentity(Identity identity) {
57                                 removedIdentities.add(identity);
58                         }
59                 });
60                 identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
61                         @Override
62                         public void processIdentity(Identity identity) {
63                                 changedIdentities.add(identity);
64                         }
65                 });
66                 identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
67                         @Override
68                         public void processIdentity(Identity identity) {
69                                 unchangedIdentities.add(identity);
70                         }
71                 });
72         }
73
74         @Test
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()));
81         }
82
83         @Test
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()));
90         }
91
92         @Test
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()));
99         }
100
101         @Test
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()));
110         }
111
112         @Test
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()));
121         }
122
123         @Test
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()));
132         }
133
134         @Test
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()));
143         }
144
145         @Test
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()));
154         }
155
156         @Test
157         public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
158                 identityChangeDetector.onRemovedIdentity(null);
159                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
160         }
161
162         @Test
163         public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
164                 identityChangeDetector.onNewIdentity(null);
165                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
166         }
167
168         private static Collection<Identity> createOldIdentities() {
169                 return asList(createIdentity1(), createIdentity2(), createIdentity3());
170         }
171
172         private static Identity createIdentity1() {
173                 return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
174         }
175
176         private static Identity createIdentity2() {
177                 return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
178         }
179
180         private static Identity createIdentity3() {
181                 return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
182         }
183
184         private static Identity createIdentity4() {
185                 return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));
186         }
187
188 }