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.WebOfTrustContextUpdateJob;
19 import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob;
20 import net.pterodactylus.sone.freenet.plugin.PluginException;
21 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
22 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
24 import org.junit.Test;
27 * Unit test for {@link WebOfTrustUpdater} and its subclasses.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class WebOfTrustUpdaterTest {
33 private static final String CONTEXT = "test-context";
34 private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
35 private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
36 private final OwnIdentity ownIdentity = when(mock(OwnIdentity.class).getId()).thenReturn("own-identity-id").getMock();
37 private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true);
38 private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false);
39 private final WebOfTrustContextUpdateJob contextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
40 private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
41 private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT);
43 private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
44 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
50 } catch (InterruptedException ie1) {
51 throw new RuntimeException(ie1);
59 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
60 new Thread(successfulWebOfTrustUpdateJob).start();
61 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
65 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
66 new Thread(failingWebOfTrustUpdateJob).start();
67 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
71 public void webOfTrustContextUpdateJobsAreEqualIfTheirClassOwnIdentityAndContextAreEqual() {
72 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
73 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(true));
74 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(true));
75 assertThat(contextUpdateJob.hashCode(), is(secondContextUpdateJob.hashCode()));
79 public void webOfTrustContextUpdatesJobsAreNotEqualIfTheirClassDiffers() {
80 assertThat(contextUpdateJob.equals(addContextJob), is(false));
84 public void webOfTrustContextUpdateJobToStringContainsIdentityAndContext() {
85 assertThat(contextUpdateJob.toString(), containsString(ownIdentity.toString()));
86 assertThat(contextUpdateJob.toString(), containsString(CONTEXT));
90 public void webOfTrustContextUpdateJobsAreNotEqualIfTheIdentitiesDiffer() {
91 OwnIdentity ownIdentity = mock(OwnIdentity.class);
92 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT);
93 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false));
94 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false));
98 public void webOfTrustContextUpdateJobsAreNotEqualIfTheirContextsDiffer() {
99 WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT + CONTEXT);
100 assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false));
101 assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false));
105 public void webOfTrustContextUpdateJobsAreNotEqualToNull() {
106 assertThat(contextUpdateJob.equals(null), is(false));
110 public void addContextJobAddsTheContext() throws PluginException {
112 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
113 verify(ownIdentity).addContext(eq(CONTEXT));
114 assertThat(addContextJob.waitForCompletion(), is(true));
118 public void exceptionWhileAddingAContextIsExposed() throws PluginException {
119 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
121 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
122 verify(ownIdentity, never()).addContext(eq(CONTEXT));
123 assertThat(addContextJob.waitForCompletion(), is(false));
127 public void removeContextJobRemovesTheContext() throws PluginException {
128 removeContextJob.run();
129 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
130 verify(ownIdentity).removeContext(eq(CONTEXT));
131 assertThat(removeContextJob.waitForCompletion(), is(true));
135 public void exceptionWhileRemovingAContextIsExposed() throws PluginException {
136 doThrow(PluginException.class).when(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
137 removeContextJob.run();
138 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
139 verify(ownIdentity, never()).removeContext(eq(CONTEXT));
140 assertThat(removeContextJob.waitForCompletion(), is(false));
144 public void settingAPropertySetsTheProperty() throws PluginException {
145 String propertyName = "property-name";
146 String propertyValue = "property-value";
147 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
148 setPropertyJob.run();
149 verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
150 verify(ownIdentity).setProperty(eq(propertyName), eq(propertyValue));
151 assertThat(setPropertyJob.waitForCompletion(), is(true));
155 public void settingAPropertyToNullRemovesTheProperty() throws PluginException {
156 String propertyName = "property-name";
157 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, null);
158 setPropertyJob.run();
159 verify(webOfTrustConnector).removeProperty(eq(ownIdentity), eq(propertyName));
160 verify(ownIdentity).removeProperty(eq(propertyName));
161 assertThat(setPropertyJob.waitForCompletion(), is(true));
165 public void pluginExceptionWhileSettingAPropertyIsHandled() throws PluginException {
166 String propertyName = "property-name";
167 String propertyValue = "property-value";
168 doThrow(PluginException.class).when(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
169 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
170 setPropertyJob.run();
171 verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue));
172 verify(ownIdentity, never()).setProperty(eq(propertyName), eq(propertyValue));
173 assertThat(setPropertyJob.waitForCompletion(), is(false));
177 public void setPropertyJobsWithSameClassPropertyAndValueAreEqual() {
178 String propertyName = "property-name";
179 String propertyValue = "property-value";
180 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
181 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
182 assertThat(firstSetPropertyJob, is(secondSetPropertyJob));
183 assertThat(secondSetPropertyJob, is(firstSetPropertyJob));
184 assertThat(firstSetPropertyJob.hashCode(), is(secondSetPropertyJob.hashCode()));
188 public void setPropertyJobsWithDifferentClassesAreNotEqual() {
189 String propertyName = "property-name";
190 String propertyValue = "property-value";
191 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
192 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue) {
194 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
198 public void nullIsNotASetProjectJobEither() {
199 String propertyName = "property-name";
200 String propertyValue = "property-value";
201 SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
202 assertThat(setPropertyJob, not(is((Object) null)));
206 public void setPropertyJobsWithDifferentPropertiesAreNotEqual() {
207 String propertyName = "property-name";
208 String propertyValue = "property-value";
209 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
210 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName + "2", propertyValue);
211 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
215 public void setPropertyJobsWithDifferentOwnIdentitiesAreNotEqual() {
216 OwnIdentity otherOwnIdentity = mock(OwnIdentity.class);
217 String propertyName = "property-name";
218 String propertyValue = "property-value";
219 SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue);
220 SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(otherOwnIdentity, propertyName, propertyValue);
221 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));