Add test for “set property” job.
[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.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;
23
24 import org.junit.Test;
25
26 /**
27  * Unit test for {@link WebOfTrustUpdater} and its subclasses.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class WebOfTrustUpdaterTest {
32
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);
42
43         private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
44                 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
45                         @Override
46                         public void run() {
47                                 super.run();
48                                 try {
49                                         sleep(100);
50                                 } catch (InterruptedException ie1) {
51                                         throw new RuntimeException(ie1);
52                                 }
53                                 finish(success);
54                         }
55                 };
56         }
57
58         @Test
59         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
60                 new Thread(successfulWebOfTrustUpdateJob).start();
61                 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
62         }
63
64         @Test
65         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
66                 new Thread(failingWebOfTrustUpdateJob).start();
67                 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
68         }
69
70         @Test
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()));
76         }
77
78         @Test
79         public void webOfTrustContextUpdatesJobsAreNotEqualIfTheirClassDiffers() {
80                 assertThat(contextUpdateJob.equals(addContextJob), is(false));
81         }
82
83         @Test
84         public void webOfTrustContextUpdateJobToStringContainsIdentityAndContext() {
85                 assertThat(contextUpdateJob.toString(), containsString(ownIdentity.toString()));
86                 assertThat(contextUpdateJob.toString(), containsString(CONTEXT));
87         }
88
89         @Test
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));
95         }
96
97         @Test
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));
102         }
103
104         @Test
105         public void webOfTrustContextUpdateJobsAreNotEqualToNull() {
106                 assertThat(contextUpdateJob.equals(null), is(false));
107         }
108
109         @Test
110         public void addContextJobAddsTheContext() throws PluginException {
111                 addContextJob.run();
112                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
113                 verify(ownIdentity).addContext(eq(CONTEXT));
114                 assertThat(addContextJob.waitForCompletion(), is(true));
115         }
116
117         @Test
118         public void exceptionWhileAddingAContextIsExposed() throws PluginException {
119                 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
120                 addContextJob.run();
121                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
122                 verify(ownIdentity, never()).addContext(eq(CONTEXT));
123                 assertThat(addContextJob.waitForCompletion(), is(false));
124         }
125
126         @Test
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));
132         }
133
134         @Test
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));
141         }
142
143         @Test
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));
152         }
153
154         @Test
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));
162         }
163
164         @Test
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));
174         }
175
176         @Test
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()));
185         }
186
187         @Test
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) {
193                 };
194                 assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob)));
195         }
196
197         @Test
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)));
203         }
204
205         @Test
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)));
212         }
213
214         @Test
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)));
222         }
223
224 }