1 package net.pterodactylus.sone.core;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.doThrow;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.never;
9 import static org.mockito.Mockito.verify;
11 import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
12 import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob;
13 import net.pterodactylus.sone.freenet.plugin.PluginException;
14 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
15 import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
17 import org.junit.Test;
20 * Unit test for {@link WebOfTrustUpdater} and its subclasses.
22 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class WebOfTrustUpdaterTest {
26 private static final String CONTEXT = "test-context";
27 private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
28 private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
29 private final OwnIdentity ownIdentity = mock(OwnIdentity.class);
30 private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
31 private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT);
34 public void addContextJobAddsTheContext() throws PluginException {
36 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
37 verify(ownIdentity).addContext(eq(CONTEXT));
38 assertThat(addContextJob.waitForCompletion(), is(true));
42 public void exceptionWhileAddingAContextIsExposed() throws PluginException {
43 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
45 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
46 verify(ownIdentity, never()).addContext(eq(CONTEXT));
47 assertThat(addContextJob.waitForCompletion(), is(false));
51 public void removeContextJobRemovesTheContext() throws PluginException {
52 removeContextJob.run();
53 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
54 verify(ownIdentity).removeContext(eq(CONTEXT));
55 assertThat(removeContextJob.waitForCompletion(), is(true));
59 public void exceptionWhileRemovingAContextIsExposed() throws PluginException {
60 doThrow(PluginException.class).when(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
61 removeContextJob.run();
62 verify(webOfTrustConnector).removeContext(eq(ownIdentity), eq(CONTEXT));
63 verify(ownIdentity, never()).removeContext(eq(CONTEXT));
64 assertThat(removeContextJob.waitForCompletion(), is(false));