Move album parsing to new configuration 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.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  * @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         @Test
159         public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
160                 identityChangeDetector.onRemovedIdentity(null);
161                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
162         }
163
164         @Test
165         public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
166                 identityChangeDetector.onNewIdentity(null);
167                 identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
168         }
169
170         private static Collection<Identity> createOldIdentities() {
171                 return asList(createIdentity1(), createIdentity2(), createIdentity3());
172         }
173
174         private static Identity createIdentity1() {
175                 return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
176         }
177
178         private static Identity createIdentity2() {
179                 return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
180         }
181
182         private static Identity createIdentity3() {
183                 return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
184         }
185
186         private static Identity createIdentity4() {
187                 return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));
188         }
189
190 }