bc918e006239a38a4745ab85647fe9b1f01fef2d
[Sone.git] / src / test / java / net / pterodactylus / sone / core / WebOfTrustUpdaterTest.java
1 package net.pterodactylus.sone.core;
2
3 import static java.lang.Thread.sleep;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.containsString;
6 import static org.hamcrest.Matchers.is;
7 import static org.hamcrest.Matchers.not;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Mockito.doThrow;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.never;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
16 import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob;
17 import net.pterodactylus.sone.core.WebOfTrustUpdater.SetPropertyJob;
18 import net.pterodactylus.sone.core.WebOfTrustUpdater.SetTrustJob;
19 import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustContextUpdateJob;
20 import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob;
21 import net.pterodactylus.sone.freenet.plugin.PluginException;
22 import net.pterodactylus.sone.freenet.wot.Identity;
23 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
24 import net.pterodactylus.sone.freenet.wot.Trust;
25 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
26 import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
27
28 import org.junit.Test;
29
30 /**
31  * Unit test for {@link WebOfTrustUpdater} and its subclasses.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class WebOfTrustUpdaterTest {
36
37         private static final String CONTEXT = "test-context";
38         private static final Integer SCORE = 50;
39         private static final Integer OTHER_SCORE = 25;
40         private static final String TRUST_COMMENT = "set in a test";
41         private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
42         private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
43         private final OwnIdentity ownIdentity = when(mock(OwnIdentity.class).getId()).thenReturn("own-identity-id").getMock();
44         private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true);
45         private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false);
46         private final WebOfTrustContextUpdateJob contextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
47         private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
48         private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT);
49         private final Identity trustee = when(mock(Identity.class).getId()).thenReturn("trustee-id").getMock();
50
51         private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
52                 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
53                         @Override
54                         public void run() {
55                                 super.run();
56                                 try {
57                                         sleep(100);
58                                 } catch (InterruptedException ie1) {
59                                         throw new RuntimeException(ie1);
60                                 }
61                                 finish(success);
62                         }
63                 };
64         }
65
66         @Test
67         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
68                 new Thread(successfulWebOfTrustUpdateJob).start();
69                 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
70         }
71
72         @Test
73         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
74                 new Thread(failingWebOfTrustUpdateJob).start();
75                 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
76         }
77
78         @Test
79         public void webOfTrustContextUpdateJobsAreEqualIfTheirClassOwnIdentityAndContextAreEqual() {
80                 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
81                 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(true));
82                 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(true));
83                 assertThat(contextUpdateJob.hashCode(), is(secondContextUpdateJob.hashCode()));
84         }
85
86         @Test
87         public void webOfTrustContextUpdatesJobsAreNotEqualIfTheirClassDiffers() {
88                 assertThat(contextUpdateJob.equals(addContextJob), is(false));
89         }
90
91         @Test
92         public void webOfTrustContextUpdateJobToStringContainsIdentityAndContext() {
93                 assertThat(contextUpdateJob.toString(), containsString(ownIdentity.toString()));
94                 assertThat(contextUpdateJob.toString(), containsString(CONTEXT));
95         }
96
97         @Test
98         public void webOfTrustContextUpdateJobsAreNotEqualIfTheIdentitiesDiffer() {
99                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
100                 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
101                 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false));
102                 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false));
103         }
104
105         @Test
106         public void webOfTrustContextUpdateJobsAreNotEqualIfTheirContextsDiffer() {
107                 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT + CONTEXT);
108                 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false));
109                 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false));
110         }
111
112         @Test
113         public void webOfTrustContextUpdateJobsAreNotEqualToNull() {
114                 assertThat(contextUpdateJob.equals(null), is(false));
115         }
116
117         @Test
118         public void addContextJobAddsTheContext() throws PluginException {
119                 addContextJob.run();
120                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
121                 verify(ownIdentity).addContext(eq(CONTEXT));
122                 assertThat(addContextJob.waitForCompletion(), is(true));
123         }
124
125         @Test
126         public void exceptionWhileAddingAContextIsExposed() throws PluginException {
127                 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
128                 addContextJob.run();
129                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
130                 verify(ownIdentity, never()).addContext(eq(CONTEXT));
131                 assertThat(addContextJob.waitForCompletion(), is(false));
132         }
133
134         @Test
135         public void removeContextJobRemovesTheContext() throws PluginException {
136                 removeContextJob.run();
137                 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
138                 verify(ownIdentity).removeContext(eq(CONTEXT));
139                 assertThat(removeContextJob.waitForCompletion(), is(true));
140         }
141
142         @Test
143         public void exceptionWhileRemovingAContextIsExposed() throws PluginException {
144                 doThrow(PluginException.class).when(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
145                 removeContextJob.run();
146                 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
147                 verify(ownIdentity, never()).removeContext(eq(CONTEXT));
148                 assertThat(removeContextJob.waitForCompletion(), is(false));
149         }
150
151         @Test
152         public void settingAPropertySetsTheProperty() throws PluginException {
153                 String propertyName = "property-name";
154                 String propertyValue = "property-value";
155                 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
156                 setPropertyJob.run();
157                 verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
158                 verify(ownIdentity).setProperty(eq(propertyName), eq(propertyValue));
159                 assertThat(setPropertyJob.waitForCompletion(), is(true));
160         }
161
162         @Test
163         public void settingAPropertyToNullRemovesTheProperty() throws PluginException {
164                 String propertyName = "property-name";
165                 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, null);
166                 setPropertyJob.run();
167                 verify(webOfTrustConnector).removeProperty(eq(ownIdentity), eq(propertyName));
168                 verify(ownIdentity).removeProperty(eq(propertyName));
169                 assertThat(setPropertyJob.waitForCompletion(), is(true));
170         }
171
172         @Test
173         public void pluginExceptionWhileSettingAPropertyIsHandled() throws PluginException {
174                 String propertyName = "property-name";
175                 String propertyValue = "property-value";
176                 doThrow(PluginException.class).when(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
177                 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
178                 setPropertyJob.run();
179                 verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
180                 verify(ownIdentity, never()).setProperty(eq(propertyName), eq(propertyValue));
181                 assertThat(setPropertyJob.waitForCompletion(), is(false));
182         }
183
184         @Test
185         public void setPropertyJobsWithSameClassPropertyAndValueAreEqual() {
186                 String propertyName = "property-name";
187                 String propertyValue = "property-value";
188                 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
189                 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
190                 assertThat(firstSetPropertyJob, is(secondSetPropertyJob));
191                 assertThat(secondSetPropertyJob, is(firstSetPropertyJob));
192                 assertThat(firstSetPropertyJob.hashCode(), is(secondSetPropertyJob.hashCode()));
193         }
194
195         @Test
196         public void setPropertyJobsWithDifferentClassesAreNotEqual() {
197                 String propertyName = "property-name";
198                 String propertyValue = "property-value";
199                 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
200                 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue) {
201                 };
202                 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
203         }
204
205         @Test
206         public void nullIsNotASetProjectJobEither() {
207                 String propertyName = "property-name";
208                 String propertyValue = "property-value";
209                 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
210                 assertThat(setPropertyJob, not(is((Object) null)));
211         }
212
213         @Test
214         public void setPropertyJobsWithDifferentPropertiesAreNotEqual() {
215                 String propertyName = "property-name";
216                 String propertyValue = "property-value";
217                 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
218                 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName + "2", propertyValue);
219                 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
220         }
221
222         @Test
223         public void setPropertyJobsWithDifferentOwnIdentitiesAreNotEqual() {
224                 OwnIdentity otherOwnIdentity = mock(OwnIdentity.class);
225                 String propertyName = "property-name";
226                 String propertyValue = "property-value";
227                 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
228                 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(otherOwnIdentity, propertyName, propertyValue);
229                 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
230         }
231
232         @Test
233         public void setTrustJobSetsTrust() throws PluginException {
234                 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
235                 setTrustJob.run();
236                 verify(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT));
237                 verify(trustee).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
238                 assertThat(setTrustJob.waitForCompletion(), is(true));
239         }
240
241         @Test
242         public void settingNullTrustRemovesTrust() throws WebOfTrustException {
243                 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, null, TRUST_COMMENT);
244                 setTrustJob.run();
245                 verify(webOfTrustConnector).removeTrust(eq(ownIdentity), eq(trustee));
246                 verify(trustee).removeTrust(eq(ownIdentity));
247                 assertThat(setTrustJob.waitForCompletion(), is(true));
248         }
249
250         @Test
251         public void exceptionWhileSettingTrustIsCaught() throws PluginException {
252                 doThrow(PluginException.class).when(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT));
253                 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
254                 setTrustJob.run();
255                 verify(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT));
256                 verify(trustee, never()).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
257                 assertThat(setTrustJob.waitForCompletion(), is(false));
258         }
259
260         @Test
261         public void setTrustJobsWithDifferentClassesAreNotEqual() {
262                 SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
263                 SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT) {
264                 };
265                 assertThat(firstSetTrustJob, not(is(secondSetTrustJob)));
266                 assertThat(secondSetTrustJob, not(is(firstSetTrustJob)));
267         }
268
269         @Test
270         public void setTrustJobsWithDifferentTrustersAreNotEqual() {
271                 SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
272                 SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(mock(OwnIdentity.class), trustee, SCORE, TRUST_COMMENT);
273                 assertThat(firstSetTrustJob, not(is(secondSetTrustJob)));
274                 assertThat(secondSetTrustJob, not(is(firstSetTrustJob)));
275         }
276
277         @Test
278         public void setTrustJobsWithDifferentTrusteesAreNotEqual() {
279                 SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
280                 SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, mock(Identity.class), SCORE, TRUST_COMMENT);
281                 assertThat(firstSetTrustJob, not(is(secondSetTrustJob)));
282                 assertThat(secondSetTrustJob, not(is(firstSetTrustJob)));
283         }
284
285         @Test
286         public void setTrustJobsWithDifferentScoreAreEqual() {
287                 SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
288                 SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, OTHER_SCORE, TRUST_COMMENT);
289                 assertThat(firstSetTrustJob, is(secondSetTrustJob));
290                 assertThat(secondSetTrustJob, is(firstSetTrustJob));
291                 assertThat(firstSetTrustJob.hashCode(), is(secondSetTrustJob.hashCode()));
292         }
293
294         @Test
295         public void setTrustJobDoesNotEqualNull() {
296                 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
297                 assertThat(setTrustJob, not(is((Object) null)));
298         }
299
300         @Test
301         public void toStringOfSetTrustJobContainsIdsOfTrusterAndTrustee() {
302                 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
303                 assertThat(setTrustJob.toString(), containsString(ownIdentity.getId()));
304                 assertThat(setTrustJob.toString(), containsString(trustee.getId()));
305         }
306
307 }