Add test for “add context” job.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 30 Jul 2014 12:20:53 +0000 (14:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 30 Jul 2014 12:20:53 +0000 (14:20 +0200)
src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java
src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java [new file with mode: 0644]

index dd3de58..9bd475a 100644 (file)
@@ -34,6 +34,7 @@ import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.inject.Inject;
 
 /**
@@ -474,7 +475,8 @@ public class WebOfTrustUpdater extends AbstractService {
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       private class AddContextJob extends WebOfTrustContextUpdateJob {
+       @VisibleForTesting
+       class AddContextJob extends WebOfTrustContextUpdateJob {
 
                /**
                 * Creates a new add-context job.
diff --git a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java
new file mode 100644 (file)
index 0000000..0589d9d
--- /dev/null
@@ -0,0 +1,48 @@
+package net.pterodactylus.sone.core;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
+import net.pterodactylus.sone.freenet.plugin.PluginException;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link WebOfTrustUpdater} and its subclasses.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class WebOfTrustUpdaterTest {
+
+       private static final String CONTEXT = "test-context";
+       private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
+       private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
+       private final OwnIdentity ownIdentity = mock(OwnIdentity.class);
+       private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
+
+       @Test
+       public void addContextJobAddsTheContext() throws PluginException {
+               addContextJob.run();
+               verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
+               verify(ownIdentity).addContext(eq(CONTEXT));
+               assertThat(addContextJob.waitForCompletion(), is(true));
+       }
+
+       @Test
+       public void exceptionWhileAddingAContextIsExposed() throws PluginException {
+               doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
+               addContextJob.run();
+               verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT));
+               verify(ownIdentity, never()).addContext(eq(CONTEXT));
+               assertThat(addContextJob.waitForCompletion(), is(false));
+       }
+
+}