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;
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;
}
continue;
}
- Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull());
+ Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
currentIdentities.putAll(ownIdentity, trustedIdentities);
}
}
private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
- return context.isPresent() && !ownIdentity.hasContext(context.get());
+ return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get());
}
}
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;
* @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;
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;
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;
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;
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() {
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());
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);