From 43364d6d488aede562d4ca0d2ec6a0e04f125110 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 3 Aug 2014 19:42:55 +0200 Subject: [PATCH] =?utf8?q?Add=20unit=20test=20for=20=E2=80=9Cset=20trust?= =?utf8?q?=E2=80=9D=20job.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/core/WebOfTrustUpdater.java | 3 +- .../net/pterodactylus/sone/freenet/wot/Trust.java | 15 +++- .../sone/core/WebOfTrustUpdaterTest.java | 83 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index 4d0528b..539897a 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -332,7 +332,8 @@ public class WebOfTrustUpdater extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class SetTrustJob extends WebOfTrustUpdateJob { + @VisibleForTesting + class SetTrustJob extends WebOfTrustUpdateJob { /** The identity giving the trust. */ private final OwnIdentity truster; diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java index 6fa37d0..fe76846 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.freenet.wot; +import static com.google.common.base.Objects.equal; + /** * Container class for trust in the web of trust. * @@ -79,9 +81,16 @@ public class Trust { return distance; } - /** - * {@inheritDoc} - */ + @Override + public boolean equals(Object object) { + if (!(object instanceof Trust)) { + return false; + } + Trust trust = (Trust) object; + return equal(getExplicit(), trust.getExplicit()) && equal(getImplicit(), trust.getImplicit()) && equal(getDistance(), trust.getDistance()); + } + + /** {@inheritDoc} */ @Override public String toString() { return getClass().getName() + "[explicit=" + explicit + ",implicit=" + implicit + ",distance=" + distance + "]"; diff --git a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java index 40230ab..bc918e0 100644 --- a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -15,11 +15,15 @@ import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.SetPropertyJob; +import net.pterodactylus.sone.core.WebOfTrustUpdater.SetTrustJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustContextUpdateJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob; import net.pterodactylus.sone.freenet.plugin.PluginException; +import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.freenet.wot.Trust; import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector; +import net.pterodactylus.sone.freenet.wot.WebOfTrustException; import org.junit.Test; @@ -31,6 +35,9 @@ import org.junit.Test; public class WebOfTrustUpdaterTest { private static final String CONTEXT = "test-context"; + private static final Integer SCORE = 50; + private static final Integer OTHER_SCORE = 25; + private static final String TRUST_COMMENT = "set in a test"; private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class); private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector); private final OwnIdentity ownIdentity = when(mock(OwnIdentity.class).getId()).thenReturn("own-identity-id").getMock(); @@ -39,6 +46,7 @@ public class WebOfTrustUpdaterTest { private final WebOfTrustContextUpdateJob contextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT); private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT); private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT); + private final Identity trustee = when(mock(Identity.class).getId()).thenReturn("trustee-id").getMock(); private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) { return webOfTrustUpdater.new WebOfTrustUpdateJob() { @@ -221,4 +229,79 @@ public class WebOfTrustUpdaterTest { assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob))); } + @Test + public void setTrustJobSetsTrust() throws PluginException { + SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + setTrustJob.run(); + verify(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT)); + verify(trustee).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0))); + assertThat(setTrustJob.waitForCompletion(), is(true)); + } + + @Test + public void settingNullTrustRemovesTrust() throws WebOfTrustException { + SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, null, TRUST_COMMENT); + setTrustJob.run(); + verify(webOfTrustConnector).removeTrust(eq(ownIdentity), eq(trustee)); + verify(trustee).removeTrust(eq(ownIdentity)); + assertThat(setTrustJob.waitForCompletion(), is(true)); + } + + @Test + public void exceptionWhileSettingTrustIsCaught() throws PluginException { + doThrow(PluginException.class).when(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT)); + SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + setTrustJob.run(); + verify(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT)); + verify(trustee, never()).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0))); + assertThat(setTrustJob.waitForCompletion(), is(false)); + } + + @Test + public void setTrustJobsWithDifferentClassesAreNotEqual() { + SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT) { + }; + assertThat(firstSetTrustJob, not(is(secondSetTrustJob))); + assertThat(secondSetTrustJob, not(is(firstSetTrustJob))); + } + + @Test + public void setTrustJobsWithDifferentTrustersAreNotEqual() { + SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(mock(OwnIdentity.class), trustee, SCORE, TRUST_COMMENT); + assertThat(firstSetTrustJob, not(is(secondSetTrustJob))); + assertThat(secondSetTrustJob, not(is(firstSetTrustJob))); + } + + @Test + public void setTrustJobsWithDifferentTrusteesAreNotEqual() { + SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, mock(Identity.class), SCORE, TRUST_COMMENT); + assertThat(firstSetTrustJob, not(is(secondSetTrustJob))); + assertThat(secondSetTrustJob, not(is(firstSetTrustJob))); + } + + @Test + public void setTrustJobsWithDifferentScoreAreEqual() { + SetTrustJob firstSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + SetTrustJob secondSetTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, OTHER_SCORE, TRUST_COMMENT); + assertThat(firstSetTrustJob, is(secondSetTrustJob)); + assertThat(secondSetTrustJob, is(firstSetTrustJob)); + assertThat(firstSetTrustJob.hashCode(), is(secondSetTrustJob.hashCode())); + } + + @Test + public void setTrustJobDoesNotEqualNull() { + SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + assertThat(setTrustJob, not(is((Object) null))); + } + + @Test + public void toStringOfSetTrustJobContainsIdsOfTrusterAndTrustee() { + SetTrustJob setTrustJob = webOfTrustUpdater.new SetTrustJob(ownIdentity, trustee, SCORE, TRUST_COMMENT); + assertThat(setTrustJob.toString(), containsString(ownIdentity.getId())); + assertThat(setTrustJob.toString(), containsString(trustee.getId())); + } + } -- 2.7.4