From: David ‘Bombe’ Roden Date: Fri, 27 Jun 2014 16:32:08 +0000 (+0200) Subject: Use context objects and optionals in interfaces. X-Git-Tag: 0.9-rc1^2~3^2~221 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e7e16a55d9ce8bb104b3bd1260875ede6fd921e5;p=Sone.git Use context objects and optionals in interfaces. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java index 550079b..5498eec 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java @@ -18,12 +18,16 @@ package net.pterodactylus.sone.freenet.wot; import static com.google.common.collect.HashMultimap.create; +import static net.pterodactylus.sone.freenet.wot.Context.extractContext; import java.util.Collection; import java.util.Set; +import javax.annotation.Nullable; + import net.pterodactylus.sone.freenet.plugin.PluginException; +import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.Multimap; @@ -35,13 +39,13 @@ import com.google.common.collect.Multimap; public class IdentityLoader { private final WebOfTrustConnector webOfTrustConnector; - private final Optional context; + private final Optional context; public IdentityLoader(WebOfTrustConnector webOfTrustConnector) { - this(webOfTrustConnector, Optional.absent()); + this(webOfTrustConnector, Optional.absent()); } - public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional context) { + public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional context) { this.webOfTrustConnector = webOfTrustConnector; this.context = context; } @@ -59,7 +63,7 @@ public class IdentityLoader { continue; } - Set trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull()); + Set trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext)); currentIdentities.putAll(ownIdentity, trustedIdentities); } @@ -67,7 +71,7 @@ public class IdentityLoader { } private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) { - return context.isPresent() && !ownIdentity.hasContext(context.get()); + return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get()); } } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java index 6278459..7938a09 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -31,6 +31,7 @@ import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.number.Numbers; +import com.google.common.base.Optional; import com.google.common.collect.MapMaker; import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; @@ -137,8 +138,8 @@ public class WebOfTrustConnector { * @throws PluginException * if an error occured talking to the Web of Trust plugin */ - public Set loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException { - Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).put("WantTrustValues", "true").get()); + public Set loadTrustedIdentities(OwnIdentity ownIdentity, Optional context) throws PluginException { + Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", context.or("")).put("WantTrustValues", "true").get()); SimpleFieldSet fields = reply.getFields(); Set identities = new HashSet(); int identityCounter = -1; diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java index 24e9ab6..b413339 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.freenet.wot; +import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.of; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Sets.newHashSet; @@ -24,6 +25,8 @@ import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNull; @@ -36,6 +39,7 @@ import java.util.Collection; import java.util.List; import java.util.Set; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Multimap; import org.hamcrest.Matchers; @@ -50,16 +54,16 @@ import org.junit.Test; public class IdentityLoaderTest { private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class); - private final IdentityLoader identityLoader = new IdentityLoader(webOfTrustConnector, of("Test")); + private final IdentityLoader identityLoader = new IdentityLoader(webOfTrustConnector, of(new Context("Test"))); private final IdentityLoader identityLoaderWithoutContext = new IdentityLoader(webOfTrustConnector); @Before public void setup() throws WebOfTrustException { List ownIdentities = createOwnIdentities(); when(webOfTrustConnector.loadAllOwnIdentities()).thenReturn(newHashSet(ownIdentities)); - when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(0)), anyString())).thenReturn(createTrustedIdentitiesForFirstOwnIdentity()); - when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(1)), anyString())).thenReturn(createTrustedIdentitiesForSecondOwnIdentity()); - when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(2)), anyString())).thenReturn(createTrustedIdentitiesForThirdOwnIdentity()); + when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(0)), any(Optional.class))).thenReturn(createTrustedIdentitiesForFirstOwnIdentity()); + when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(1)), any(Optional.class))).thenReturn(createTrustedIdentitiesForSecondOwnIdentity()); + when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(2)), any(Optional.class))).thenReturn(createTrustedIdentitiesForThirdOwnIdentity()); } private List createOwnIdentities() { @@ -107,9 +111,9 @@ public class IdentityLoaderTest { List ownIdentities = createOwnIdentities(); Multimap identities = identityLoader.loadIdentities(); verify(webOfTrustConnector).loadAllOwnIdentities(); - verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq("Test")); - verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq("Test")); - verify(webOfTrustConnector, never()).loadTrustedIdentities(eq(ownIdentities.get(2)), anyString()); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq(of("Test"))); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(of("Test"))); + verify(webOfTrustConnector, never()).loadTrustedIdentities(eq(ownIdentities.get(2)), any(Optional.class)); assertThat(identities.keySet(), hasSize(2)); assertThat(identities.keySet(), containsInAnyOrder(ownIdentities.get(0), ownIdentities.get(1))); verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(0), createTrustedIdentitiesForFirstOwnIdentity()); @@ -121,9 +125,9 @@ public class IdentityLoaderTest { List ownIdentities = createOwnIdentities(); Multimap identities = identityLoaderWithoutContext.loadIdentities(); verify(webOfTrustConnector).loadAllOwnIdentities(); - verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), isNull(String.class)); - verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), isNull(String.class)); - verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(2)), isNull(String.class)); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq(Optional.absent())); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(Optional.absent())); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(2)), eq(Optional.absent())); assertThat(identities.keySet(), hasSize(3)); OwnIdentity firstOwnIdentity = ownIdentities.get(0); OwnIdentity secondOwnIdentity = ownIdentities.get(1);