Fix own identities without trust identities from disappearing.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Aug 2014 19:27:09 +0000 (21:27 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Aug 2014 19:27:09 +0000 (21:27 +0200)
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java

index 4f38f8e..d087dec 100644 (file)
@@ -17,6 +17,9 @@
 
 package net.pterodactylus.sone.freenet.wot;
 
+import java.util.Collection;
+import java.util.Map;
+
 import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
@@ -24,7 +27,6 @@ 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;
 
 /**
@@ -37,14 +39,14 @@ import com.google.common.eventbus.EventBus;
 public class IdentityChangeEventSender {
 
        private final EventBus eventBus;
-       private final Multimap<OwnIdentity, Identity> oldIdentities;
+       private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
 
-       public IdentityChangeEventSender(EventBus eventBus, Multimap<OwnIdentity, Identity> oldIdentities) {
+       public IdentityChangeEventSender(EventBus eventBus, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
                this.eventBus = eventBus;
                this.oldIdentities = oldIdentities;
        }
 
-       public void detectChanges(Multimap<OwnIdentity, Identity> identities) {
+       public void detectChanges(Map<OwnIdentity, Collection<Identity>> identities) {
                IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet());
                identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities));
                identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities));
@@ -52,7 +54,7 @@ public class IdentityChangeEventSender {
                identityChangeDetector.detectChanges(identities.keySet());
        }
 
-       private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Multimap<OwnIdentity, Identity> newIdentities) {
+       private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> newIdentities) {
                return new IdentityProcessor() {
                        @Override
                        public void processIdentity(Identity identity) {
@@ -64,7 +66,7 @@ public class IdentityChangeEventSender {
                };
        }
 
-       private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap<OwnIdentity, Identity> oldIdentities) {
+       private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> oldIdentities) {
                return new IdentityProcessor() {
                        @Override
                        public void processIdentity(Identity identity) {
@@ -76,16 +78,16 @@ public class IdentityChangeEventSender {
                };
        }
 
-       private IdentityProcessor detectChangesInTrustedIdentities(Multimap<OwnIdentity, Identity> newIdentities, Multimap<OwnIdentity, Identity> oldIdentities) {
+       private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
                return new DefaultIdentityProcessor(oldIdentities, newIdentities);
        }
 
        private class DefaultIdentityProcessor implements IdentityProcessor {
 
-               private final Multimap<OwnIdentity, Identity> oldIdentities;
-               private final Multimap<OwnIdentity, Identity> newIdentities;
+               private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
+               private final Map<OwnIdentity, Collection<Identity>> newIdentities;
 
-               public DefaultIdentityProcessor(Multimap<OwnIdentity, Identity> oldIdentities, Multimap<OwnIdentity, Identity> newIdentities) {
+               public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
                        this.oldIdentities = oldIdentities;
                        this.newIdentities = newIdentities;
                }
index 823653e..e19cbfc 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
-import static com.google.common.collect.HashMultimap.create;
 import static net.pterodactylus.sone.freenet.wot.Context.extractContext;
 
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
-import javax.annotation.Nullable;
-
 import net.pterodactylus.sone.freenet.plugin.PluginException;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.collect.Multimap;
 import com.google.inject.Inject;
 
 /**
@@ -52,13 +49,13 @@ public class IdentityLoader {
                this.context = context;
        }
 
-       public Multimap<OwnIdentity, Identity> loadIdentities() throws WebOfTrustException {
+       public Map<OwnIdentity, Collection<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();
+       private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
+               Map<OwnIdentity, Collection<Identity>> currentIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
 
                for (OwnIdentity ownIdentity : ownIdentities) {
                        if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
@@ -66,7 +63,7 @@ public class IdentityLoader {
                        }
 
                        Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
-                       currentIdentities.putAll(ownIdentity, trustedIdentities);
+                       currentIdentities.put(ownIdentity, trustedIdentities);
                }
 
                return currentIdentities;
index 22c1b71..5978169 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
-import static com.google.common.base.Optional.fromNullable;
-import static com.google.common.collect.HashMultimap.create;
-
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -29,7 +29,6 @@ import net.pterodactylus.sone.freenet.plugin.PluginException;
 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;
@@ -118,11 +117,11 @@ public class IdentityManager extends AbstractService {
         */
        @Override
        protected void serviceRun() {
-               Multimap<OwnIdentity, Identity> oldIdentities = create();
+               Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
 
                while (!shouldStop()) {
                        try {
-                               Multimap<OwnIdentity, Identity> currentIdentities = identityLoader.loadIdentities();
+                               Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
 
                                IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
                                identityChangeEventSender.detectChanges(currentIdentities);
index fe4f070..8e1cf6a 100644 (file)
@@ -25,8 +25,11 @@ import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
+import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
@@ -34,8 +37,6 @@ 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;
 
@@ -62,7 +63,7 @@ public class IdentityChangeEventSenderTest {
 
        @Test
        public void addingAnOwnIdentityIsDetectedAndReportedCorrectly() {
-               Multimap<OwnIdentity, Identity> newIdentities = createNewIdentities();
+               Map<OwnIdentity, Collection<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))));
@@ -75,22 +76,18 @@ public class IdentityChangeEventSenderTest {
                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 Map<OwnIdentity, Collection<Identity>> createNewIdentities() {
+               Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
+               oldIdentities.put(ownIdentities.get(1), asList(identities.get(3), identities.get(2)));
+               oldIdentities.put(ownIdentities.get(2), asList(identities.get(1), identities.get(2)));
+               return oldIdentities;
        }
 
-       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();
+       private Map<OwnIdentity, Collection<Identity>> createOldIdentities() {
+               Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
+               oldIdentities.put(ownIdentities.get(0), asList(identities.get(0), identities.get(1)));
+               oldIdentities.put(ownIdentities.get(1), asList(identities.get(0), identities.get(1)));
+               return oldIdentities;
        }
 
 }
index b413339..4772302 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
-import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.of;
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Arrays.asList;
+import static java.util.Collections.emptySet;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.hasSize;
-import static org.hamcrest.Matchers.is;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isNull;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -37,11 +34,11 @@ import static org.mockito.Mockito.when;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Multimap;
 import org.hamcrest.Matchers;
 import org.junit.Before;
 import org.junit.Test;
@@ -64,13 +61,15 @@ public class IdentityLoaderTest {
                when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(0)), any(Optional.class))).thenReturn(createTrustedIdentitiesForFirstOwnIdentity());
                when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(1)), any(Optional.class))).thenReturn(createTrustedIdentitiesForSecondOwnIdentity());
                when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(2)), any(Optional.class))).thenReturn(createTrustedIdentitiesForThirdOwnIdentity());
+               when(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities.get(3)), any(Optional.class))).thenReturn(createTrustedIdentitiesForFourthOwnIdentity());
        }
 
        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"))
+                               createOwnIdentity("O3", "ON3", "OR3", "OI3", asList("Test2"), ImmutableMap.of("KeyE", "ValueE", "KeyD", "ValueD")),
+                               createOwnIdentity("O4", "ON4", "OR$", "OI4", asList("Test"), ImmutableMap.of("KeyA", "ValueA", "KeyD", "ValueD"))
                );
        }
 
@@ -92,6 +91,10 @@ public class IdentityLoaderTest {
                );
        }
 
+       private Set<Identity> createTrustedIdentitiesForFourthOwnIdentity() {
+               return emptySet();
+       }
+
        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);
@@ -109,36 +112,41 @@ public class IdentityLoaderTest {
        @Test
        public void loadingIdentities() throws WebOfTrustException {
                List<OwnIdentity> ownIdentities = createOwnIdentities();
-               Multimap<OwnIdentity, Identity> identities = identityLoader.loadIdentities();
+               Map<OwnIdentity, Collection<Identity>> identities = identityLoader.loadIdentities();
                verify(webOfTrustConnector).loadAllOwnIdentities();
                verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq(of("Test")));
                verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(of("Test")));
                verify(webOfTrustConnector, never()).loadTrustedIdentities(eq(ownIdentities.get(2)), any(Optional.class));
-               assertThat(identities.keySet(), hasSize(2));
-               assertThat(identities.keySet(), containsInAnyOrder(ownIdentities.get(0), ownIdentities.get(1)));
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(3)), eq(of("Test")));
+               assertThat(identities.keySet(), hasSize(3));
+               assertThat(identities.keySet(), containsInAnyOrder(ownIdentities.get(0), ownIdentities.get(1), ownIdentities.get(3)));
                verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(0), createTrustedIdentitiesForFirstOwnIdentity());
                verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(1), createTrustedIdentitiesForSecondOwnIdentity());
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(3), createTrustedIdentitiesForFourthOwnIdentity());
        }
 
        @Test
        public void loadingIdentitiesWithoutContext() throws WebOfTrustException {
                List<OwnIdentity> ownIdentities = createOwnIdentities();
-               Multimap<OwnIdentity, Identity> identities = identityLoaderWithoutContext.loadIdentities();
+               Map<OwnIdentity, Collection<Identity>> identities = identityLoaderWithoutContext.loadIdentities();
                verify(webOfTrustConnector).loadAllOwnIdentities();
                verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq(Optional.<String>absent()));
                verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(Optional.<String>absent()));
                verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(2)), eq(Optional.<String>absent()));
-               assertThat(identities.keySet(), hasSize(3));
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(3)), eq(Optional.<String>absent()));
+               assertThat(identities.keySet(), hasSize(4));
                OwnIdentity firstOwnIdentity = ownIdentities.get(0);
                OwnIdentity secondOwnIdentity = ownIdentities.get(1);
                OwnIdentity thirdOwnIdentity = ownIdentities.get(2);
-               assertThat(identities.keySet(), containsInAnyOrder(firstOwnIdentity, secondOwnIdentity, thirdOwnIdentity));
+               OwnIdentity fourthOwnIdentity = ownIdentities.get(3);
+               assertThat(identities.keySet(), containsInAnyOrder(firstOwnIdentity, secondOwnIdentity, thirdOwnIdentity, fourthOwnIdentity));
                verifyIdentitiesForOwnIdentity(identities, firstOwnIdentity, createTrustedIdentitiesForFirstOwnIdentity());
                verifyIdentitiesForOwnIdentity(identities, secondOwnIdentity, createTrustedIdentitiesForSecondOwnIdentity());
                verifyIdentitiesForOwnIdentity(identities, thirdOwnIdentity, createTrustedIdentitiesForThirdOwnIdentity());
+               verifyIdentitiesForOwnIdentity(identities, fourthOwnIdentity, createTrustedIdentitiesForFourthOwnIdentity());
        }
 
-       private void verifyIdentitiesForOwnIdentity(Multimap<OwnIdentity, Identity> identities, OwnIdentity ownIdentity, Set<Identity> trustedIdentities) {
+       private void verifyIdentitiesForOwnIdentity(Map<OwnIdentity, Collection<Identity>> identities, OwnIdentity ownIdentity, Set<Identity> trustedIdentities) {
                assertThat(identities.get(ownIdentity), Matchers.<Collection<Identity>>is(trustedIdentities));
        }