From: David ‘Bombe’ Roden Date: Fri, 27 Jun 2014 15:56:33 +0000 (+0200) Subject: Add identity manager changes from the refactoring branch. X-Git-Tag: 0.9-rc1^2~3^2~224 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=4c4b77eff97fd247c6b1c8bbb30aeb6ea3d2c172 Add identity manager changes from the refactoring branch. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java index bc594f8..4554f40 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java @@ -18,9 +18,14 @@ 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> TO_CONTEXTS = new Function>() { + @Override + public Collection apply(@Nullable Identity identity) { + return (identity == null) ? Collections.emptyList() : identity.getContexts(); + } + }; + + final Function> TO_PROPERTIES = new Function>() { + @Override + public Map apply(@Nullable Identity identity) { + return (identity == null) ? Collections.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 index 0000000..a8cb62b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class IdentityChangeDetector { + + private final Map oldIdentities; + private Optional onNewIdentity = absent(); + private Optional onRemovedIdentity = absent(); + private Optional onChangedIdentity = absent(); + private Optional onUnchangedIdentity = absent(); + + public IdentityChangeDetector(Collection 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 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 identities) { + notify(onRemovedIdentity, identities); + } + + private void notifyForNewIdentities(FluentIterable newIdentities) { + notify(onNewIdentity, newIdentities); + } + + private void notifyForChangedIdentities(FluentIterable identities) { + notify(onChangedIdentity, identities); + } + + private void notifyForUnchangedIdentities(FluentIterable identities) { + notify(onUnchangedIdentity, identities); + } + + private void notify(Optional identityProcessor, Iterable identities) { + if (!identityProcessor.isPresent()) { + return; + } + for (Identity identity : identities) { + identityProcessor.get().processIdentity(identity); + } + } + + private static Predicate hasChanged(final Map oldIdentities) { + return new Predicate() { + @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 containedIn(final Map identities) { + return new Predicate() { + @Override + public boolean apply(Identity identity) { + return identities.containsKey(identity.getId()); + } + }; + } + + private static Predicate notAContextOf(final Identity identity) { + return new Predicate() { + @Override + public boolean apply(String context) { + return (identity == null) ? false : !identity.getContexts().contains(context); + } + }; + } + + private static Predicate notContainedIn(final Collection newIdentities) { + return new Predicate() { + @Override + public boolean apply(Identity identity) { + return (identity == null) ? false : !newIdentities.contains(identity); + } + }; + } + + private static Predicate> notAPropertyOf(final Identity identity) { + return new Predicate>() { + @Override + public boolean apply(Entry property) { + return (property == null) ? false : !identity.getProperties().containsKey(property.getKey()); + } + }; + } + + private static Predicate> hasADifferentValueThanIn(final Identity newIdentity) { + return new Predicate>() { + @Override + public boolean apply(Entry property) { + return (property == null) ? false : !newIdentity.getProperty(property.getKey()).equals(property.getValue()); + } + }; + } + + private static Map convertToMap(Collection identities) { + ImmutableMap.Builder 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 index 0000000..4f38f8e --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + * @see IdentityChangeDetector + */ +public class IdentityChangeEventSender { + + private final EventBus eventBus; + private final Multimap oldIdentities; + + public IdentityChangeEventSender(EventBus eventBus, Multimap oldIdentities) { + this.eventBus = eventBus; + this.oldIdentities = oldIdentities; + } + + public void detectChanges(Multimap 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 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 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 newIdentities, Multimap oldIdentities) { + return new DefaultIdentityProcessor(oldIdentities, newIdentities); + } + + private class DefaultIdentityProcessor implements IdentityProcessor { + + private final Multimap oldIdentities; + private final Multimap newIdentities; + + public DefaultIdentityProcessor(Multimap oldIdentities, Multimap 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 index 0000000..550079b --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class IdentityLoader { + + private final WebOfTrustConnector webOfTrustConnector; + private final Optional context; + + public IdentityLoader(WebOfTrustConnector webOfTrustConnector) { + this(webOfTrustConnector, Optional.absent()); + } + + public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional context) { + this.webOfTrustConnector = webOfTrustConnector; + this.context = context; + } + + public Multimap loadIdentities() throws WebOfTrustException { + Collection currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities(); + return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities); + } + + private Multimap loadTrustedIdentitiesForOwnIdentities(Collection ownIdentities) throws PluginException { + Multimap currentIdentities = create(); + + for (OwnIdentity ownIdentity : ownIdentities) { + if (identityDoesNotHaveTheCorrectContext(ownIdentity)) { + continue; + } + + Set trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull()); + currentIdentities.putAll(ownIdentity, trustedIdentities); + } + + return currentIdentities; + } + + private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) { + return context.isPresent() && !ownIdentity.hasContext(context.get()); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java index 6f33a4a..e89c667 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -17,24 +17,20 @@ 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 currentOwnIdentities = new HashMap(); - - /** The last time all identities were loaded. */ - private volatile long identitiesLastLoaded; + private final Set 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 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 getAllOwnIdentities() { - synchronized (syncObject) { - return new HashSet(currentOwnIdentities.values()); + synchronized (currentOwnIdentities) { + return new HashSet(currentOwnIdentities); } } @@ -164,161 +122,28 @@ public class IdentityManager extends AbstractService { */ @Override protected void serviceRun() { - Map> oldIdentities = Collections.emptyMap(); - while (!shouldStop()) { - Map> currentIdentities = new HashMap>(); - Map currentOwnIdentities = new HashMap(); + Multimap 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 ownIdentities = webOfTrustConnector.loadAllOwnIdentities(); - logger.finest(String.format("Loaded %d Own Identities.", ownIdentities.size())); + Multimap currentIdentities = identityLoader.loadIdentities(); - /* load trusted identities. */ - for (OwnIdentity ownIdentity : ownIdentities) { - currentOwnIdentities.put(ownIdentity.getId(), ownIdentity); - Map identities = new HashMap(); - 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 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 oldContexts = oldIdentity.getContexts(); - Set 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 oldProperties = oldIdentity.getProperties(); - Map 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 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 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 index 0000000..fe3625e --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/Identities.java @@ -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 . + */ + +package net.pterodactylus.sone.freenet.wot; + +import java.util.Collection; +import java.util.Map; + +/** + * Creates {@link Identity}s and {@link OwnIdentity}s. + * + * @author David ‘Bombe’ Roden + */ +public class Identities { + + public static OwnIdentity createOwnIdentity(String id, Collection contexts, Map 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 contexts, Map properties) { + DefaultIdentity identity = new DefaultIdentity(id, "Nickname" + id, "Request" + id); + setContextsAndPropertiesOnIdentity(identity, contexts, properties); + return identity; + } + + private static void setContextsAndPropertiesOnIdentity(Identity identity, Collection contexts, Map 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 index 0000000..b2d4ed1 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class IdentityChangeDetectorTest { + + private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities()); + private final Collection newIdentities = newArrayList(); + private final Collection removedIdentities = newArrayList(); + private final Collection changedIdentities = newArrayList(); + private final Collection 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 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 index 0000000..fe4f070 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +public class IdentityChangeEventSenderTest { + + private final EventBus eventBus = mock(EventBus.class); + private final List 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 identities = asList( + createIdentity("I1", Collections.emptyList(), Collections.emptyMap()), + createIdentity("I2", Collections.emptyList(), Collections.emptyMap()), + createIdentity("I3", Collections.emptyList(), Collections.emptyMap()), + createIdentity("I2", asList("Test"), Collections.emptyMap()) + ); + private final IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, createOldIdentities()); + + @Test + public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() { + Multimap 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 createNewIdentities() { + ImmutableMultimap.Builder 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 createOldIdentities() { + ImmutableMultimap.Builder 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 index 0000000..24e9ab6 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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 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 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 createTrustedIdentitiesForFirstOwnIdentity() { + return newHashSet( + createIdentity("I11", "IN11", "IR11", asList("Test"), ImmutableMap.of("KeyA", "ValueA")) + ); + } + + private Set createTrustedIdentitiesForSecondOwnIdentity() { + return newHashSet( + createIdentity("I21", "IN21", "IR21", asList("Test", "Test2"), ImmutableMap.of("KeyB", "ValueB")) + ); + } + + private Set 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 contexts, ImmutableMap 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 contexts, ImmutableMap properties) { + Identity identity = new DefaultIdentity(id, nickname, requestUri); + identity.setContexts(contexts); + identity.setProperties(properties); + return identity; + } + + @Test + public void loadingIdentities() throws WebOfTrustException { + 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()); + 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 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)); + 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 identities, OwnIdentity ownIdentity, Set trustedIdentities) { + assertThat(identities.get(ownIdentity), Matchers.>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 index 0000000..321f55c --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.java @@ -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 David ‘Bombe’ Roden + */ +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(); + } + +}