1 package net.pterodactylus.sone.core;
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;
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;
28 import org.junit.Test;
31 * Unit test for {@link WebOfTrustUpdater} and its subclasses.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class WebOfTrustUpdaterTest {
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();
51 private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
52 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
58 } catch (InterruptedException ie1) {
59 throw new RuntimeException(ie1);
67 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
68 new Thread(successfulWebOfTrustUpdateJob).start();
69 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
73 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
74 new Thread(failingWebOfTrustUpdateJob).start();
75 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
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()));
87 public void webOfTrustContextUpdatesJobsAreNotEqualIfTheirClassDiffers() {
88 assertThat(contextUpdateJob.equals(addContextJob), is(false));
92 public void webOfTrustContextUpdateJobToStringContainsIdentityAndContext() {
93 assertThat(contextUpdateJob.toString(), containsString(ownIdentity.toString()));
94 assertThat(contextUpdateJob.toString(), containsString(CONTEXT));
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));
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));
113 public void webOfTrustContextUpdateJobsAreNotEqualToNull() {
114 assertThat(contextUpdateJob.equals(null), is(false));
118 public void addContextJobAddsTheContext() throws PluginException {
120 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
121 verify(ownIdentity).addContext(eq(CONTEXT));
122 assertThat(addContextJob.waitForCompletion(), is(true));
126 public void exceptionWhileAddingAContextIsExposed() throws PluginException {
127 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
129 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
130 verify(ownIdentity, never()).addContext(eq(CONTEXT));
131 assertThat(addContextJob.waitForCompletion(), is(false));
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));
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));
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));
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));
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));
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()));
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) {
202 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
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)));
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)));
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)));
233 public void setTrustJobSetsTrust() throws PluginException {
234 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
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));
242 public void settingNullTrustRemovesTrust() throws WebOfTrustException {
243 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, null, TRUST_COMMENT);
245 verify(webOfTrustConnector).removeTrust(eq(ownIdentity), eq(trustee));
246 verify(trustee).removeTrust(eq(ownIdentity));
247 assertThat(setTrustJob.waitForCompletion(), is(true));
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);
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));
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) {
265 assertThat(firstSetTrustJob, not(is(secondSetTrustJob)));
266 assertThat(secondSetTrustJob, not(is(firstSetTrustJob)));
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)));
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)));
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()));
295 public void setTrustJobDoesNotEqualNull() {
296 SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT);
297 assertThat(setTrustJob, not(is((Object) null)));
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()));