Add test for “remove context” job.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / WebOfTrustUpdaterTest.java
1 package net.pterodactylus.sone.core;
2
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;
10
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;
16
17 import org.junit.Test;
18
19 /**
20  * Unit test for {@link WebOfTrustUpdater} and its subclasses.
21  *
22  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23  */
24 public class WebOfTrustUpdaterTest {
25
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);
32
33         @Test
34         public void addContextJobAddsTheContext() throws PluginException {
35                 addContextJob.run();
36                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
37                 verify(ownIdentity).addContext(eq(CONTEXT));
38                 assertThat(addContextJob.waitForCompletion(), is(true));
39         }
40
41         @Test
42         public void exceptionWhileAddingAContextIsExposed() throws PluginException {
43                 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
44                 addContextJob.run();
45                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
46                 verify(ownIdentity, never()).addContext(eq(CONTEXT));
47                 assertThat(addContextJob.waitForCompletion(), is(false));
48         }
49
50         @Test
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));
56         }
57
58         @Test
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));
65         }
66
67 }