Use context objects and optionals in interfaces.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 16:32:08 +0000 (18:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 16:32:08 +0000 (18:32 +0200)
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java
src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java

index 550079b..5498eec 100644 (file)
 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<String> context;
+       private final Optional<Context> context;
 
        public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
-               this(webOfTrustConnector, Optional.<String>absent());
+               this(webOfTrustConnector, Optional.<Context>absent());
        }
 
-       public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<String> context) {
+       public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<Context> context) {
                this.webOfTrustConnector = webOfTrustConnector;
                this.context = context;
        }
@@ -59,7 +63,7 @@ public class IdentityLoader {
                                continue;
                        }
 
-                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull());
+                       Set<Identity> 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());
        }
 
 }
index 6278459..7938a09 100644 (file)
@@ -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<Identity> 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<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, Optional<String> 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<Identity> identities = new HashSet<Identity>();
                int identityCounter = -1;
index 24e9ab6..b413339 100644 (file)
@@ -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<OwnIdentity> 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<OwnIdentity> createOwnIdentities() {
@@ -107,9 +111,9 @@ public class IdentityLoaderTest {
                List<OwnIdentity> ownIdentities = createOwnIdentities();
                Multimap<OwnIdentity, Identity> 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<OwnIdentity> ownIdentities = createOwnIdentities();
                Multimap<OwnIdentity, Identity> 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.<String>absent()));
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(Optional.<String>absent()));
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(2)), eq(Optional.<String>absent()));
                assertThat(identities.keySet(), hasSize(3));
                OwnIdentity firstOwnIdentity = ownIdentities.get(0);
                OwnIdentity secondOwnIdentity = ownIdentities.get(1);