From 52f608f5902d6d05dd39599cdb2ed28f2465b4be Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 7 Aug 2014 21:27:09 +0200 Subject: [PATCH] Fix own identities without trust identities from disappearing. --- .../freenet/wot/IdentityChangeEventSender.java | 22 +++++++------- .../sone/freenet/wot/IdentityLoader.java | 15 ++++------ .../sone/freenet/wot/IdentityManager.java | 11 ++++--- .../freenet/wot/IdentityChangeEventSenderTest.java | 31 +++++++++----------- .../sone/freenet/wot/IdentityLoaderTest.java | 34 +++++++++++++--------- 5 files changed, 58 insertions(+), 55 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java index 4f38f8e..d087dec 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java @@ -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 oldIdentities; + private final Map> oldIdentities; - public IdentityChangeEventSender(EventBus eventBus, Multimap oldIdentities) { + public IdentityChangeEventSender(EventBus eventBus, Map> oldIdentities) { this.eventBus = eventBus; this.oldIdentities = oldIdentities; } - public void detectChanges(Multimap identities) { + public void detectChanges(Map> 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 newIdentities) { + private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Map> newIdentities) { return new IdentityProcessor() { @Override public void processIdentity(Identity identity) { @@ -64,7 +66,7 @@ public class IdentityChangeEventSender { }; } - private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap oldIdentities) { + private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map> oldIdentities) { return new IdentityProcessor() { @Override public void processIdentity(Identity identity) { @@ -76,16 +78,16 @@ public class IdentityChangeEventSender { }; } - private IdentityProcessor detectChangesInTrustedIdentities(Multimap newIdentities, Multimap oldIdentities) { + private IdentityProcessor detectChangesInTrustedIdentities(Map> newIdentities, Map> oldIdentities) { return new DefaultIdentityProcessor(oldIdentities, newIdentities); } private class DefaultIdentityProcessor implements IdentityProcessor { - private final Multimap oldIdentities; - private final Multimap newIdentities; + private final Map> oldIdentities; + private final Map> newIdentities; - public DefaultIdentityProcessor(Multimap oldIdentities, Multimap newIdentities) { + public DefaultIdentityProcessor(Map> oldIdentities, Map> newIdentities) { this.oldIdentities = oldIdentities; this.newIdentities = newIdentities; } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java index 823653e..e19cbfc 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityLoader.java @@ -17,19 +17,16 @@ 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 loadIdentities() throws WebOfTrustException { + public Map> loadIdentities() throws WebOfTrustException { Collection currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities(); return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities); } - private Multimap loadTrustedIdentitiesForOwnIdentities(Collection ownIdentities) throws PluginException { - Multimap currentIdentities = create(); + private Map> loadTrustedIdentitiesForOwnIdentities(Collection ownIdentities) throws PluginException { + Map> currentIdentities = new HashMap>(); for (OwnIdentity ownIdentity : ownIdentities) { if (identityDoesNotHaveTheCorrectContext(ownIdentity)) { @@ -66,7 +63,7 @@ public class IdentityLoader { } Set trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext)); - currentIdentities.putAll(ownIdentity, trustedIdentities); + currentIdentities.put(ownIdentity, trustedIdentities); } return currentIdentities; 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 22c1b71..5978169 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -17,10 +17,10 @@ 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 oldIdentities = create(); + Map> oldIdentities = new HashMap>(); while (!shouldStop()) { try { - Multimap currentIdentities = identityLoader.loadIdentities(); + Map> currentIdentities = identityLoader.loadIdentities(); IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities); identityChangeEventSender.detectChanges(currentIdentities); diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java index fe4f070..8e1cf6a 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.java @@ -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 newIdentities = createNewIdentities(); + Map> 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 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 Map> createNewIdentities() { + Map> oldIdentities = new HashMap>(); + 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 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(); + private Map> createOldIdentities() { + Map> oldIdentities = new HashMap>(); + 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; } } diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java index b413339..4772302 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java @@ -17,19 +17,16 @@ 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 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 createTrustedIdentitiesForFourthOwnIdentity() { + return emptySet(); + } + 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); @@ -109,36 +112,41 @@ public class IdentityLoaderTest { @Test public void loadingIdentities() throws WebOfTrustException { List ownIdentities = createOwnIdentities(); - Multimap identities = identityLoader.loadIdentities(); + Map> 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 ownIdentities = createOwnIdentities(); - Multimap identities = identityLoaderWithoutContext.loadIdentities(); + Map> identities = identityLoaderWithoutContext.loadIdentities(); verify(webOfTrustConnector).loadAllOwnIdentities(); verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(0)), eq(Optional.absent())); verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(1)), eq(Optional.absent())); verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(2)), eq(Optional.absent())); - assertThat(identities.keySet(), hasSize(3)); + verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(3)), eq(Optional.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 identities, OwnIdentity ownIdentity, Set trustedIdentities) { + private void verifyIdentitiesForOwnIdentity(Map> identities, OwnIdentity ownIdentity, Set trustedIdentities) { assertThat(identities.get(ownIdentity), Matchers.>is(trustedIdentities)); } -- 2.7.4