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;
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;
/**
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));
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) {
};
}
- 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) {
};
}
- 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;
}
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;
/**
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)) {
}
Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
- currentIdentities.putAll(ownIdentity, trustedIdentities);
+ currentIdentities.put(ownIdentity, trustedIdentities);
}
return currentIdentities;
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;
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;
*/
@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);
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;
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;
@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))));
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;
}
}
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;
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;
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"))
);
}
);
}
+ 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);
@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));
}