From: David ‘Bombe’ Roden Date: Wed, 30 Jul 2014 12:20:53 +0000 (+0200) Subject: Add test for “add context” job. X-Git-Tag: 0.9-rc1^2~3^2~196 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d249bcc142a9e351e1774ac326ff5e961e1813d9;p=Sone.git Add test for “add context” job. --- diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index dd3de58..9bd475a 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -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 David ‘Bombe’ Roden */ - 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 index 0000000..0589d9d --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -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 David ‘Bombe’ Roden + */ +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)); + } + +}