Rework the text parser.
[Sone.git] / src / test / java / net / pterodactylus / sone / 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.sone.freenet.wot;
19
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;
25
26 import java.util.Collection;
27 import java.util.Map;
28
29 import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
30
31 import com.google.common.collect.ImmutableMap;
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(new IdentityProcessor() {
51                         @Override
52                         public void processIdentity(Identity identity) {
53                                 newIdentities.add(identity);
54                         }
55                 });
56                 identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
57                         @Override
58                         public void processIdentity(Identity identity) {
59                                 removedIdentities.add(identity);
60                         }
61                 });
62                 identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
63                         @Override
64                         public void processIdentity(Identity identity) {
65                                 changedIdentities.add(identity);
66                         }
67                 });
68                 identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
69                         @Override
70                         public void processIdentity(Identity identity) {
71                                 unchangedIdentities.add(identity);
72                         }
73                 });
74         }
75
76         @Test
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()));
83         }
84
85         @Test
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()));
92         }
93
94         @Test
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()));
101         }
102
103         @Test
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()));
112         }
113
114         @Test
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()));
123         }
124
125         @Test
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()));
134         }
135
136         @Test
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()));
145         }
146
147         @Test
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()));
156         }
157
158         private static Collection<Identity> createOldIdentities() {
159                 return asList(createIdentity1(), createIdentity2(), createIdentity3());
160         }
161
162         private static Identity createIdentity1() {
163                 return createIdentity("Test1", asList("Context A", "Context B"), ImmutableMap.of("Key A", "Value A", "Key B", "Value B"));
164         }
165
166         private static Identity createIdentity2() {
167                 return createIdentity("Test2", asList("Context C", "Context D"), ImmutableMap.of("Key C", "Value C", "Key D", "Value D"));
168         }
169
170         private static Identity createIdentity3() {
171                 return createIdentity("Test3", asList("Context E", "Context F"), ImmutableMap.of("Key E", "Value E", "Key F", "Value F"));
172         }
173
174         private static Identity createIdentity4() {
175                 return createIdentity("Test4", asList("Context G", "Context H"), ImmutableMap.of("Key G", "Value G", "Key H", "Value H"));
176         }
177
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);
182                 return identity;
183         }
184
185 }