From: David ‘Bombe’ Roden Date: Mon, 11 Nov 2013 21:45:27 +0000 (+0100) Subject: Add an identity loader. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=60b26c2ec10dd7881eb637f0698defce6c0aedf8;p=Sone.git Add an identity loader. --- 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/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)); + } + +}