Increase test coverage.
[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.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;
11
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;
18
19 import org.junit.Test;
20
21 /**
22  * Unit test for {@link WebOfTrustUpdater} and its subclasses.
23  *
24  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25  */
26 public class WebOfTrustUpdaterTest {
27
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);
36
37         private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
38                 return webOfTrustUpdater.new WebOfTrustUpdateJob() {
39                         @Override
40                         public void run() {
41                                 super.run();
42                                 try {
43                                         sleep(100);
44                                 } catch (InterruptedException ie1) {
45                                         throw new RuntimeException(ie1);
46                                 }
47                                 finish(success);
48                         }
49                 };
50         }
51
52         @Test
53         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
54                 new Thread(successfulWebOfTrustUpdateJob).start();
55                 assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
56         }
57
58         @Test
59         public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
60                 new Thread(failingWebOfTrustUpdateJob).start();
61                 assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
62         }
63
64         @Test
65         public void addContextJobAddsTheContext() throws PluginException {
66                 addContextJob.run();
67                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
68                 verify(ownIdentity).addContext(eq(CONTEXT));
69                 assertThat(addContextJob.waitForCompletion(), is(true));
70         }
71
72         @Test
73         public void exceptionWhileAddingAContextIsExposed() throws PluginException {
74                 doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
75                 addContextJob.run();
76                 verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
77                 verify(ownIdentity, never()).addContext(eq(CONTEXT));
78                 assertThat(addContextJob.waitForCompletion(), is(false));
79         }
80
81         @Test
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));
87         }
88
89         @Test
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));
96         }
97
98 }