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.is;
6 import static org.mockito.Matchers.eq;
7 import static org.mockito.Mockito.doThrow;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.never;
10 import static org.mockito.Mockito.verify;
12 import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
13 import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob;
14 import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob;
15 import net.pterodactylus.sone.freenet.plugin.PluginException;
16 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
17 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
19 import org.junit.Test;
22 * Unit test for {@link WebOfTrustUpdater} and its subclasses.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 public class WebOfTrustUpdaterTest {
28 private static final String CONTEXT = "test-context";
29 private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
30 private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
31 private final OwnIdentity ownIdentity = mock(OwnIdentity.class);
32 private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true);
33 private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false);
34 private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
35 private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT);
37 private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
38 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
44 } catch (InterruptedException ie1) {
45 throw new RuntimeException(ie1);
53 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
54 new Thread(successfulWebOfTrustUpdateJob).start();
55 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
59 public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
60 new Thread(failingWebOfTrustUpdateJob).start();
61 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
65 public void addContextJobAddsTheContext() throws PluginException {
67 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
68 verify(ownIdentity).addContext(eq(CONTEXT));
69 assertThat(addContextJob.waitForCompletion(), is(true));
73 public void exceptionWhileAddingAContextIsExposed() throws PluginException {
74 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
76 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
77 verify(ownIdentity, never()).addContext(eq(CONTEXT));
78 assertThat(addContextJob.waitForCompletion(), is(false));
82 public void removeContextJobRemovesTheContext() throws PluginException {
83 removeContextJob.run();
84 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
85 verify(ownIdentity).removeContext(eq(CONTEXT));
86 assertThat(removeContextJob.waitForCompletion(), is(true));
90 public void exceptionWhileRemovingAContextIsExposed() throws PluginException {
91 doThrow(PluginException.class).when(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
92 removeContextJob.run();
93 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
94 verify(ownIdentity, never()).removeContext(eq(CONTEXT));
95 assertThat(removeContextJob.waitForCompletion(), is(false));