Add identity manager changes from the refactoring branch.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 15:56:33 +0000 (17:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jun 2014 15:56:33 +0000 (17:56 +0200)
src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java [new file with mode: 0644]

index bc594f8..4554f40 100644 (file)
 package net.pterodactylus.sone.freenet.wot;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
 
+import javax.annotation.Nullable;
+
+import com.google.common.base.Function;
+
 /**
  * Interface for web of trust identities, defining all functions that can be
  * performed on an identity. An identity is only a container for identity data
@@ -30,6 +35,20 @@ import java.util.Set;
  */
 public interface Identity {
 
+       final Function<Identity, Collection<String>> TO_CONTEXTS = new Function<Identity, Collection<String>>() {
+               @Override
+               public Collection<String> apply(@Nullable Identity identity) {
+                       return (identity == null) ? Collections.<String>emptyList() : identity.getContexts();
+               }
+       };
+
+       final Function<Identity, Map<String, String>> TO_PROPERTIES = new Function<Identity, Map<String, String>>() {
+               @Override
+               public Map<String, String> apply(@Nullable Identity identity) {
+                       return (identity == null) ? Collections.<String, String>emptyMap() : identity.getProperties();
+               }
+       };
+
        /**
         * Returns the ID of the identity.
         *
diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java
new file mode 100644 (file)
index 0000000..a8cb62b
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Sone - IdentityChangeDetector.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.FluentIterable.from;
+import static net.pterodactylus.sone.freenet.wot.Identity.TO_CONTEXTS;
+import static net.pterodactylus.sone.freenet.wot.Identity.TO_PROPERTIES;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Detects changes between two lists of {@link Identity}s. The detector can find
+ * added and removed identities, and for identities that exist in both list
+ * their contexts and properties are checked for added, removed, or (in case of
+ * properties) changed values.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityChangeDetector {
+
+       private final Map<String, Identity> oldIdentities;
+       private Optional<IdentityProcessor> onNewIdentity = absent();
+       private Optional<IdentityProcessor> onRemovedIdentity = absent();
+       private Optional<IdentityProcessor> onChangedIdentity = absent();
+       private Optional<IdentityProcessor> onUnchangedIdentity = absent();
+
+       public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
+               this.oldIdentities = convertToMap(oldIdentities);
+       }
+
+       public void onNewIdentity(IdentityProcessor onNewIdentity) {
+               this.onNewIdentity = fromNullable(onNewIdentity);
+       }
+
+       public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
+               this.onRemovedIdentity = fromNullable(onRemovedIdentity);
+       }
+
+       public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
+               this.onChangedIdentity = fromNullable(onChangedIdentity);
+       }
+
+       public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
+               this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
+       }
+
+       public void detectChanges(final Collection<? extends Identity> newIdentities) {
+               notifyForRemovedIdentities(from(oldIdentities.values()).filter(notContainedIn(newIdentities)));
+               notifyForNewIdentities(from(newIdentities).filter(notContainedIn(oldIdentities.values())));
+               notifyForChangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)));
+               notifyForUnchangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(not(hasChanged(oldIdentities))));
+       }
+
+       private void notifyForRemovedIdentities(Iterable<Identity> identities) {
+               notify(onRemovedIdentity, identities);
+       }
+
+       private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
+               notify(onNewIdentity, newIdentities);
+       }
+
+       private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
+               notify(onChangedIdentity, identities);
+       }
+
+       private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
+               notify(onUnchangedIdentity, identities);
+       }
+
+       private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
+               if (!identityProcessor.isPresent()) {
+                       return;
+               }
+               for (Identity identity : identities) {
+                       identityProcessor.get().processIdentity(identity);
+               }
+       }
+
+       private static Predicate<Identity> hasChanged(final Map<String, Identity> oldIdentities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return (identity == null) ? false : identityHasChanged(oldIdentities.get(identity.getId()), identity);
+                       }
+               };
+       }
+
+       private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
+               return identityHasNewContexts(oldIdentity, newIdentity)
+                               || identityHasRemovedContexts(oldIdentity, newIdentity)
+                               || identityHasNewProperties(oldIdentity, newIdentity)
+                               || identityHasRemovedProperties(oldIdentity, newIdentity)
+                               || identityHasChangedProperties(oldIdentity, newIdentity);
+       }
+
+       private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_CONTEXTS.apply(newIdentity)).anyMatch(notAContextOf(oldIdentity));
+       }
+
+       private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_CONTEXTS.apply(oldIdentity)).anyMatch(notAContextOf(newIdentity));
+       }
+
+       private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(newIdentity).entrySet()).anyMatch(notAPropertyOf(oldIdentity));
+       }
+
+       private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(notAPropertyOf(newIdentity));
+       }
+
+       private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(hasADifferentValueThanIn(newIdentity));
+       }
+
+       private static Predicate<Identity> containedIn(final Map<String, Identity> identities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return identities.containsKey(identity.getId());
+                       }
+               };
+       }
+
+       private static Predicate<String> notAContextOf(final Identity identity) {
+               return new Predicate<String>() {
+                       @Override
+                       public boolean apply(String context) {
+                               return (identity == null) ? false : !identity.getContexts().contains(context);
+                       }
+               };
+       }
+
+       private static Predicate<Identity> notContainedIn(final Collection<? extends Identity> newIdentities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return (identity == null) ? false : !newIdentities.contains(identity);
+                       }
+               };
+       }
+
+       private static Predicate<Entry<String, String>> notAPropertyOf(final Identity identity) {
+               return new Predicate<Entry<String, String>>() {
+                       @Override
+                       public boolean apply(Entry<String, String> property) {
+                               return (property == null) ? false : !identity.getProperties().containsKey(property.getKey());
+                       }
+               };
+       }
+
+       private static Predicate<Entry<String, String>> hasADifferentValueThanIn(final Identity newIdentity) {
+               return new Predicate<Entry<String, String>>() {
+                       @Override
+                       public boolean apply(Entry<String, String> property) {
+                               return (property == null) ? false : !newIdentity.getProperty(property.getKey()).equals(property.getValue());
+                       }
+               };
+       }
+
+       private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
+               ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
+               for (Identity identity : identities) {
+                       mapBuilder.put(identity.getId(), identity);
+               }
+               return mapBuilder.build();
+       }
+
+       public interface IdentityProcessor {
+
+               void processIdentity(Identity identity);
+
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java
new file mode 100644 (file)
index 0000000..4f38f8e
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Sone - IdentityChangeEventSender.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
+import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
+
+import com.google.common.collect.Multimap;
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Detects changes in {@link Identity}s trusted my multiple {@link
+ * OwnIdentity}s.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see IdentityChangeDetector
+ */
+public class IdentityChangeEventSender {
+
+       private final EventBus eventBus;
+       private final Multimap<OwnIdentity, Identity> oldIdentities;
+
+       public IdentityChangeEventSender(EventBus eventBus, Multimap<OwnIdentity, Identity> oldIdentities) {
+               this.eventBus = eventBus;
+               this.oldIdentities = oldIdentities;
+       }
+
+       public void detectChanges(Multimap<OwnIdentity, Identity> identities) {
+               IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet());
+               identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities));
+               identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities));
+               identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(identities, oldIdentities));
+               identityChangeDetector.detectChanges(identities.keySet());
+       }
+
+       private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Multimap<OwnIdentity, Identity> newIdentities) {
+               return new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity));
+                               for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) {
+                                       eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity));
+                               }
+                       }
+               };
+       }
+
+       private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap<OwnIdentity, Identity> oldIdentities) {
+               return new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity));
+                               for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) {
+                                       eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity));
+                               }
+                       }
+               };
+       }
+
+       private IdentityProcessor detectChangesInTrustedIdentities(Multimap<OwnIdentity, Identity> newIdentities, Multimap<OwnIdentity, Identity> oldIdentities) {
+               return new DefaultIdentityProcessor(oldIdentities, newIdentities);
+       }
+
+       private class DefaultIdentityProcessor implements IdentityProcessor {
+
+               private final Multimap<OwnIdentity, Identity> oldIdentities;
+               private final Multimap<OwnIdentity, Identity> newIdentities;
+
+               public DefaultIdentityProcessor(Multimap<OwnIdentity, Identity> oldIdentities, Multimap<OwnIdentity, Identity> newIdentities) {
+                       this.oldIdentities = oldIdentities;
+                       this.newIdentities = newIdentities;
+               }
+
+               @Override
+               public void processIdentity(Identity ownIdentity) {
+                       IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity));
+                       identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity));
+                       identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity));
+                       identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity));
+                       identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity));
+               }
+
+               private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) {
+                       return new IdentityProcessor() {
+                               @Override
+                               public void processIdentity(Identity identity) {
+                                       eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
+                               }
+                       };
+               }
+
+               private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) {
+                       return new IdentityProcessor() {
+                               @Override
+                               public void processIdentity(Identity identity) {
+                                       eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
+                               }
+                       };
+               }
+
+               private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) {
+                       return new IdentityProcessor() {
+                               @Override
+                               public void processIdentity(Identity identity) {
+                                       eventBus.post(new IdentityAddedEvent(ownIdentity, identity));
+                               }
+                       };
+               }
+
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java
new file mode 100644 (file)
index 0000000..550079b
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Sone - IdentityLoader.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import static com.google.common.collect.HashMultimap.create;
+
+import java.util.Collection;
+import java.util.Set;
+
+import net.pterodactylus.sone.freenet.plugin.PluginException;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Multimap;
+
+/**
+ * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityLoader {
+
+       private final WebOfTrustConnector webOfTrustConnector;
+       private final Optional<String> context;
+
+       public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
+               this(webOfTrustConnector, Optional.<String>absent());
+       }
+
+       public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<String> context) {
+               this.webOfTrustConnector = webOfTrustConnector;
+               this.context = context;
+       }
+
+       public Multimap<OwnIdentity, Identity> loadIdentities() throws WebOfTrustException {
+               Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
+               return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
+       }
+
+       private Multimap<OwnIdentity, Identity> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
+               Multimap<OwnIdentity, Identity> currentIdentities = create();
+
+               for (OwnIdentity ownIdentity : ownIdentities) {
+                       if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
+                               continue;
+                       }
+
+                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull());
+                       currentIdentities.putAll(ownIdentity, trustedIdentities);
+               }
+
+               return currentIdentities;
+       }
+
+       private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
+               return context.isPresent() && !ownIdentity.hasContext(context.get());
+       }
+
+}
index 6f33a4a..e89c667 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
-import java.util.Collections;
-import java.util.HashMap;
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.collect.HashMultimap.create;
+
 import java.util.HashSet;
-import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.freenet.plugin.PluginException;
-import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
-import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
-import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
-import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
-import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
 import com.google.common.eventbus.EventBus;
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
@@ -52,30 +48,19 @@ import com.google.inject.name.Named;
  */
 public class IdentityManager extends AbstractService {
 
-       /** Object used for synchronization. */
-       @SuppressWarnings("hiding")
-       private final Object syncObject = new Object() {
-               /* inner class for better lock names. */
-       };
-
        /** The logger. */
        private static final Logger logger = Logging.getLogger(IdentityManager.class);
 
        /** The event bus. */
        private final EventBus eventBus;
 
+       private final IdentityLoader identityLoader;
+
        /** The Web of Trust connector. */
        private final WebOfTrustConnector webOfTrustConnector;
 
-       /** The context to filter for. */
-       private final String context;
-
        /** The currently known own identities. */
-       /* synchronize access on syncObject. */
-       private final Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
-
-       /** The last time all identities were loaded. */
-       private volatile long identitiesLastLoaded;
+       private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
 
        /**
         * Creates a new identity manager.
@@ -93,7 +78,7 @@ public class IdentityManager extends AbstractService {
                super("Sone Identity Manager", false);
                this.eventBus = eventBus;
                this.webOfTrustConnector = webOfTrustConnector;
-               this.context = context;
+               this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context));
        }
 
        //
@@ -101,16 +86,6 @@ public class IdentityManager extends AbstractService {
        //
 
        /**
-        * Returns the last time all identities were loaded.
-        *
-        * @return The last time all identities were loaded (in milliseconds since
-        *         Jan 1, 1970 UTC)
-        */
-       public long getIdentitiesLastLoaded() {
-               return identitiesLastLoaded;
-       }
-
-       /**
         * Returns whether the Web of Trust plugin could be reached during the last
         * try.
         *
@@ -128,30 +103,13 @@ public class IdentityManager extends AbstractService {
        }
 
        /**
-        * Returns the own identity with the given ID.
-        *
-        * @param id
-        *            The ID of the own identity
-        * @return The own identity, or {@code null} if there is no such identity
-        */
-       public OwnIdentity getOwnIdentity(String id) {
-               Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
-               for (OwnIdentity ownIdentity : allOwnIdentities) {
-                       if (ownIdentity.getId().equals(id)) {
-                               return new DefaultOwnIdentity(ownIdentity);
-                       }
-               }
-               return null;
-       }
-
-       /**
         * Returns all own identities.
         *
         * @return All own identities
         */
        public Set<OwnIdentity> getAllOwnIdentities() {
-               synchronized (syncObject) {
-                       return new HashSet<OwnIdentity>(currentOwnIdentities.values());
+               synchronized (currentOwnIdentities) {
+                       return new HashSet<OwnIdentity>(currentOwnIdentities);
                }
        }
 
@@ -164,161 +122,28 @@ public class IdentityManager extends AbstractService {
         */
        @Override
        protected void serviceRun() {
-               Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
-               while (!shouldStop()) {
-                       Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
-                       Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
+               Multimap<OwnIdentity, Identity> oldIdentities = create();
 
-                       boolean identitiesLoaded = false;
+               while (!shouldStop()) {
                        try {
-                               /* get all identities with the wanted context from WoT. */
-                               logger.finer("Getting all Own Identities from WoT...");
-                               Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
-                               logger.finest(String.format("Loaded %d Own Identities.", ownIdentities.size()));
+                               Multimap<OwnIdentity, Identity> currentIdentities = identityLoader.loadIdentities();
 
-                               /* load trusted identities. */
-                               for (OwnIdentity ownIdentity : ownIdentities) {
-                                       currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
-                                       Map<String, Identity> identities = new HashMap<String, Identity>();
-                                       currentIdentities.put(ownIdentity, identities);
+                               IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
+                               identityChangeEventSender.detectChanges(currentIdentities);
 
-                                       /*
-                                        * if the context doesn’t match, skip getting trusted
-                                        * identities.
-                                        */
-                                       if ((context != null) && !ownIdentity.hasContext(context)) {
-                                               continue;
-                                       }
+                               oldIdentities = currentIdentities;
 
-                                       /* load trusted identities. */
-                                       logger.finer(String.format("Getting trusted identities for %s...", ownIdentity.getId()));
-                                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
-                                       logger.finest(String.format("Got %d trusted identities.", trustedIdentities.size()));
-                                       for (Identity identity : trustedIdentities) {
-                                               identities.put(identity.getId(), identity);
-                                       }
+                               synchronized (currentOwnIdentities) {
+                                       currentOwnIdentities.clear();
+                                       currentOwnIdentities.addAll(currentIdentities.keySet());
                                }
-                               identitiesLoaded = true;
-                               identitiesLastLoaded = System.currentTimeMillis();
                        } catch (WebOfTrustException wote1) {
                                logger.log(Level.WARNING, "WoT has disappeared!", wote1);
                        }
 
-                       if (identitiesLoaded) {
-
-                               /* check for changes. */
-                               checkOwnIdentities(currentOwnIdentities);
-
-                               /* now check for changes in remote identities. */
-                               for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
-
-                                       /* find new identities. */
-                                       for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
-                                               if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
-                                                       logger.finest(String.format("Identity added for %s: %s", ownIdentity.getId(), currentIdentity));
-                                                       eventBus.post(new IdentityAddedEvent(ownIdentity, currentIdentity));
-                                               }
-                                       }
-
-                                       /* find removed identities. */
-                                       if (oldIdentities.containsKey(ownIdentity)) {
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               logger.finest(String.format("Identity removed for %s: %s", ownIdentity.getId(), oldIdentity));
-                                                               eventBus.post(new IdentityRemovedEvent(ownIdentity, oldIdentity));
-                                                       }
-                                               }
-
-                                               /* check for changes in the contexts. */
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               continue;
-                                                       }
-                                                       Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
-                                                       Set<String> oldContexts = oldIdentity.getContexts();
-                                                       Set<String> newContexts = newIdentity.getContexts();
-                                                       if (oldContexts.size() != newContexts.size()) {
-                                                               logger.finest(String.format("Contexts changed for %s: was: %s, is now: %s", ownIdentity.getId(), oldContexts, newContexts));
-                                                               eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
-                                                               continue;
-                                                       }
-                                                       for (String oldContext : oldContexts) {
-                                                               if (!newContexts.contains(oldContext)) {
-                                                                       logger.finest(String.format("Context was removed for %s: %s", ownIdentity.getId(), oldContext));
-                                                                       eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-
-                                               /* check for changes in the properties. */
-                                               for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
-                                                       if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
-                                                               continue;
-                                                       }
-                                                       Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
-                                                       Map<String, String> oldProperties = oldIdentity.getProperties();
-                                                       Map<String, String> newProperties = newIdentity.getProperties();
-                                                       if (oldProperties.size() != newProperties.size()) {
-                                                               logger.finest(String.format("Properties changed for %s: was: %s, is now: %s", ownIdentity.getId(), oldProperties, newProperties));
-                                                               eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
-                                                               continue;
-                                                       }
-                                                       for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
-                                                               if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
-                                                                       logger.finest(String.format("Property was removed for %s: %s", ownIdentity.getId(), oldProperty));
-                                                                       eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               /* remember the current set of identities. */
-                               oldIdentities = currentIdentities;
-                       }
-
                        /* wait a minute before checking again. */
                        sleep(60 * 1000);
                }
        }
 
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Checks the given new list of own identities for added or removed own
-        * identities, as compared to {@link #currentOwnIdentities}.
-        *
-        * @param newOwnIdentities
-        *            The new own identities
-        */
-       private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
-               synchronized (syncObject) {
-
-                       /* find removed own identities: */
-                       for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
-                               OwnIdentity newOwnIdentity = newOwnIdentities.get(oldOwnIdentity.getId());
-                               if ((newOwnIdentity == null) || ((context != null) && oldOwnIdentity.hasContext(context) && !newOwnIdentity.hasContext(context))) {
-                                       logger.finest(String.format("Own Identity removed: %s", oldOwnIdentity));
-                                       eventBus.post(new OwnIdentityRemovedEvent(new DefaultOwnIdentity(oldOwnIdentity)));
-                               }
-                       }
-
-                       /* find added own identities. */
-                       for (OwnIdentity currentOwnIdentity : newOwnIdentities.values()) {
-                               OwnIdentity oldOwnIdentity = currentOwnIdentities.get(currentOwnIdentity.getId());
-                               if (((oldOwnIdentity == null) && ((context == null) || currentOwnIdentity.hasContext(context))) || ((oldOwnIdentity != null) && (context != null) && (!oldOwnIdentity.hasContext(context) && currentOwnIdentity.hasContext(context)))) {
-                                       logger.finest(String.format("Own Identity added: %s", currentOwnIdentity));
-                                       eventBus.post(new OwnIdentityAddedEvent(new DefaultOwnIdentity(currentOwnIdentity)));
-                               }
-                       }
-
-                       currentOwnIdentities.clear();
-                       currentOwnIdentities.putAll(newOwnIdentities);
-               }
-       }
-
 }
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java b/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java
new file mode 100644 (file)
index 0000000..fe3625e
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Sone - Identities.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Creates {@link Identity}s and {@link OwnIdentity}s.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Identities {
+
+       public static OwnIdentity createOwnIdentity(String id, Collection<String> contexts, Map<String, String> properties) {
+               DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, "Nickname" + id, "Request" + id, "Insert" + id);
+               setContextsAndPropertiesOnIdentity(ownIdentity, contexts, properties);
+               return ownIdentity;
+       }
+
+       public static Identity createIdentity(String id, Collection<String> contexts, Map<String, String> properties) {
+               DefaultIdentity identity = new DefaultIdentity(id, "Nickname" + id, "Request" + id);
+               setContextsAndPropertiesOnIdentity(identity, contexts, properties);
+               return identity;
+       }
+
+       private static void setContextsAndPropertiesOnIdentity(Identity identity, Collection<String> contexts, Map<String, String> properties) {
+               identity.setContexts(contexts);
+               identity.setProperties(properties);
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java
new file mode 100644 (file)
index 0000000..b2d4ed1
--- /dev/null
@@ -0,0 +1,190 @@
+/*
+ * Sone - IdentityChangeDetectorTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import static com.google.common.collect.ImmutableMap.of;
+import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Arrays.asList;
+import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+
+import java.util.Collection;
+
+import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityChangeDetector}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityChangeDetectorTest {
+
+       private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities());
+       private final Collection<Identity> newIdentities = newArrayList();
+       private final Collection<Identity> removedIdentities = newArrayList();
+       private final Collection<Identity> changedIdentities = newArrayList();
+       private final Collection<Identity> unchangedIdentities = newArrayList();
+
+       @Before
+       public void setup() {
+               identityChangeDetector.onNewIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               newIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               removedIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               changedIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               unchangedIdentities.add(identity);
+                       }
+               });
+       }
+
+       @Test
+       public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() {
+               identityChangeDetector.detectChanges(createOldIdentities());
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAnIdentityWasRemoved() {
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, containsInAnyOrder(createIdentity2()));
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAnIdentityWasAdded() {
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
+               assertThat(newIdentities, containsInAnyOrder(createIdentity4()));
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAContextWasRemoved() {
+               Identity identity2 = createIdentity2();
+               identity2.removeContext("Context C");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity2));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAContextWasAdded() {
+               Identity identity2 = createIdentity2();
+               identity2.addContext("Context C1");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity2));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasRemoved() {
+               Identity identity1 = createIdentity1();
+               identity1.removeProperty("Key A");
+               identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity1));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasAdded() {
+               Identity identity3 = createIdentity3();
+               identity3.setProperty("Key A", "Value A");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity3));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasChanged() {
+               Identity identity3 = createIdentity3();
+               identity3.setProperty("Key E", "Value F");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity3));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
+       }
+
+       @Test
+       public void noRemovedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
+               identityChangeDetector.onRemovedIdentity(null);
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void noAddedIdentitiesAreDetectedWithoutAnIdentityProcessor() {
+               identityChangeDetector.onNewIdentity(null);
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
+       }
+
+       private static Collection<Identity> createOldIdentities() {
+               return asList(createIdentity1(), createIdentity2(), createIdentity3());
+       }
+
+       private static Identity createIdentity1() {
+               return createIdentity("Test1", asList("Context A", "Context B"), of("Key A", "Value A", "Key B", "Value B"));
+       }
+
+       private static Identity createIdentity2() {
+               return createIdentity("Test2", asList("Context C", "Context D"), of("Key C", "Value C", "Key D", "Value D"));
+       }
+
+       private static Identity createIdentity3() {
+               return createIdentity("Test3", asList("Context E", "Context F"), of("Key E", "Value E", "Key F", "Value F"));
+       }
+
+       private static Identity createIdentity4() {
+               return createIdentity("Test4", asList("Context G", "Context H"), of("Key G", "Value G", "Key H", "Value H"));
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java
new file mode 100644 (file)
index 0000000..fe4f070
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Sone - IdentityChangeEventSenderTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+import static com.google.common.collect.ImmutableMap.of;
+import static java.util.Arrays.asList;
+import static net.pterodactylus.sone.freenet.wot.Identities.createIdentity;
+import static net.pterodactylus.sone.freenet.wot.Identities.createOwnIdentity;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
+import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
+import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
+
+import com.google.common.collect.ImmutableMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.eventbus.EventBus;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityChangeEventSender}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityChangeEventSenderTest {
+
+       private final EventBus eventBus = mock(EventBus.class);
+       private final List<OwnIdentity> ownIdentities = asList(
+                       createOwnIdentity("O1", asList("Test"), of("KeyA", "ValueA")),
+                       createOwnIdentity("O2", asList("Test2"), of("KeyB", "ValueB")),
+                       createOwnIdentity("O3", asList("Test3"), of("KeyC", "ValueC"))
+       );
+       private final List<Identity> identities = asList(
+                       createIdentity("I1", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
+                       createIdentity("I2", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
+                       createIdentity("I3", Collections.<String>emptyList(), Collections.<String, String>emptyMap()),
+                       createIdentity("I2", asList("Test"), Collections.<String, String>emptyMap())
+       );
+       private final IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, createOldIdentities());
+
+       @Test
+       public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
+               Multimap<OwnIdentity, Identity> newIdentities = createNewIdentities();
+               identityChangeEventSender.detectChanges(newIdentities);
+               verify(eventBus).post(eq(new OwnIdentityRemovedEvent(ownIdentities.get(0))));
+               verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(0))));
+               verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(0), identities.get(1))));
+               verify(eventBus).post(eq(new OwnIdentityAddedEvent(ownIdentities.get(2))));
+               verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(1))));
+               verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(2), identities.get(2))));
+               verify(eventBus).post(eq(new IdentityRemovedEvent(ownIdentities.get(1), identities.get(0))));
+               verify(eventBus).post(eq(new IdentityAddedEvent(ownIdentities.get(1), identities.get(2))));
+               verify(eventBus).post(eq(new IdentityUpdatedEvent(ownIdentities.get(1), identities.get(1))));
+       }
+
+       private Multimap<OwnIdentity, Identity> createNewIdentities() {
+               ImmutableMultimap.Builder<OwnIdentity, Identity> oldIdentities = ImmutableMultimap.builder();
+               oldIdentities.put(ownIdentities.get(1), identities.get(3));
+               oldIdentities.put(ownIdentities.get(1), identities.get(2));
+               oldIdentities.put(ownIdentities.get(2), identities.get(1));
+               oldIdentities.put(ownIdentities.get(2), identities.get(2));
+               return oldIdentities.build();
+       }
+
+       private Multimap<OwnIdentity, Identity> createOldIdentities() {
+               ImmutableMultimap.Builder<OwnIdentity, Identity> oldIdentities = ImmutableMultimap.builder();
+               oldIdentities.put(ownIdentities.get(0), identities.get(0));
+               oldIdentities.put(ownIdentities.get(0), identities.get(1));
+               oldIdentities.put(ownIdentities.get(1), identities.get(0));
+               oldIdentities.put(ownIdentities.get(1), identities.get(1));
+               return oldIdentities.build();
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java
new file mode 100644 (file)
index 0000000..24e9ab6
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Sone - IdentityLoaderTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+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 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.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Multimap;
+import org.hamcrest.Matchers;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityLoader}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityLoaderTest {
+
+       private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
+       private final IdentityLoader identityLoader = new IdentityLoader(webOfTrustConnector, of("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());
+       }
+
+       private List<OwnIdentity> createOwnIdentities() {
+               return newArrayList(
+                               createOwnIdentity("O1", "ON1", "OR1", "OI1", asList("Test", "Test2"), ImmutableMap.of("KeyA", "ValueA", "KeyB", "ValueB")),
+                               createOwnIdentity("O2", "ON2", "OR2", "OI2", asList("Test"), ImmutableMap.of("KeyC", "ValueC")),
+                               createOwnIdentity("O3", "ON3", "OR3", "OI3", asList("Test2"), ImmutableMap.of("KeyE", "ValueE", "KeyD", "ValueD"))
+               );
+       }
+
+       private Set<Identity> createTrustedIdentitiesForFirstOwnIdentity() {
+               return newHashSet(
+                               createIdentity("I11", "IN11", "IR11", asList("Test"), ImmutableMap.of("KeyA", "ValueA"))
+               );
+       }
+
+       private Set<Identity> createTrustedIdentitiesForSecondOwnIdentity() {
+               return newHashSet(
+                               createIdentity("I21", "IN21", "IR21", asList("Test", "Test2"), ImmutableMap.of("KeyB", "ValueB"))
+               );
+       }
+
+       private Set<Identity> createTrustedIdentitiesForThirdOwnIdentity() {
+               return newHashSet(
+                               createIdentity("I31", "IN31", "IR31", asList("Test", "Test3"), ImmutableMap.of("KeyC", "ValueC"))
+               );
+       }
+
+       private OwnIdentity createOwnIdentity(String id, String nickname, String requestUri, String insertUri, List<String> contexts, ImmutableMap<String, String> properties) {
+               OwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
+               ownIdentity.setContexts(contexts);
+               ownIdentity.setProperties(properties);
+               return ownIdentity;
+       }
+
+       private Identity createIdentity(String id, String nickname, String requestUri, List<String> contexts, ImmutableMap<String, String> properties) {
+               Identity identity = new DefaultIdentity(id, nickname, requestUri);
+               identity.setContexts(contexts);
+               identity.setProperties(properties);
+               return identity;
+       }
+
+       @Test
+       public void loadingIdentities() throws WebOfTrustException {
+               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());
+               assertThat(identities.keySet(), hasSize(2));
+               assertThat(identities.keySet(), containsInAnyOrder(ownIdentities.get(0), ownIdentities.get(1)));
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(0), createTrustedIdentitiesForFirstOwnIdentity());
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(1), createTrustedIdentitiesForSecondOwnIdentity());
+       }
+
+       @Test
+       public void loadingIdentitiesWithoutContext() throws WebOfTrustException {
+               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));
+               assertThat(identities.keySet(), hasSize(3));
+               OwnIdentity firstOwnIdentity = ownIdentities.get(0);
+               OwnIdentity secondOwnIdentity = ownIdentities.get(1);
+               OwnIdentity thirdOwnIdentity = ownIdentities.get(2);
+               assertThat(identities.keySet(), containsInAnyOrder(firstOwnIdentity, secondOwnIdentity, thirdOwnIdentity));
+               verifyIdentitiesForOwnIdentity(identities, firstOwnIdentity, createTrustedIdentitiesForFirstOwnIdentity());
+               verifyIdentitiesForOwnIdentity(identities, secondOwnIdentity, createTrustedIdentitiesForSecondOwnIdentity());
+               verifyIdentitiesForOwnIdentity(identities, thirdOwnIdentity, createTrustedIdentitiesForThirdOwnIdentity());
+       }
+
+       private void verifyIdentitiesForOwnIdentity(Multimap<OwnIdentity, Identity> identities, OwnIdentity ownIdentity, Set<Identity> trustedIdentities) {
+               assertThat(identities.get(ownIdentity), Matchers.<Collection<Identity>>is(trustedIdentities));
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java
new file mode 100644 (file)
index 0000000..321f55c
--- /dev/null
@@ -0,0 +1,38 @@
+package net.pterodactylus.sone.freenet.wot;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import net.pterodactylus.sone.freenet.plugin.PluginException;
+
+import com.google.common.eventbus.EventBus;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityManager}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityManagerTest {
+
+       private final EventBus eventBus = mock(EventBus.class);
+       private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
+       private final IdentityManager identityManager = new IdentityManager(eventBus, webOfTrustConnector, "Test");
+
+       @Test
+       public void identityManagerPingsWotConnector() throws PluginException {
+               assertThat(identityManager.isConnected(), is(true));
+               verify(webOfTrustConnector).ping();
+       }
+
+       @Test
+       public void disconnectedWotConnectorIsRecognized() throws PluginException {
+               doThrow(PluginException.class).when(webOfTrustConnector).ping();
+               assertThat(identityManager.isConnected(), is(false));
+               verify(webOfTrustConnector).ping();
+       }
+
+}