🎨 Replace IdentityLoaderTest with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 23 Sep 2019 18:56:47 +0000 (20:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 23 Sep 2019 18:56:47 +0000 (20:56 +0200)
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java
deleted file mode 100644 (file)
index 8744c15..0000000
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Sone - IdentityLoaderTest.java - Copyright Â© 2013–2019 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 <http://www.gnu.org/licenses/>.
- */
-
-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 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.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
-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.Collections;
-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 org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Unit test for {@link IdentityLoader}.
- */
-public class IdentityLoaderTest {
-
-       private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
-       private final IdentityLoader identityLoader = new IdentityLoader(webOfTrustConnector, of(new Context("Test")));
-       private final IdentityLoader identityLoaderWithoutContext = new IdentityLoader(webOfTrustConnector);
-
-       @Before
-       public void setup() throws WebOfTrustException {
-               List<OwnIdentity> ownIdentities = createOwnIdentities();
-               when(webOfTrustConnector.loadAllOwnIdentities()).thenReturn(newHashSet(ownIdentities));
-               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("O4", "ON4", "OR$", "OI4", asList("Test"), ImmutableMap.of("KeyA", "ValueA", "KeyD", "ValueD"))
-               );
-       }
-
-       private Set<Identity> createTrustedIdentitiesForFirstOwnIdentity() {
-               return newHashSet(
-                               createIdentity("I11", "IN11", "IR11", asList("Test"), ImmutableMap.of("KeyA", "ValueA"))
-               );
-       }
-
-       private Set<Identity> createTrustedIdentitiesForSecondOwnIdentity() {
-               return newHashSet(
-                               createIdentity("I21", "IN21", "IR21", asList("Test", "Test2"), ImmutableMap.of("KeyB", "ValueB"))
-               );
-       }
-
-       private Set<Identity> createTrustedIdentitiesForThirdOwnIdentity() {
-               return newHashSet(
-                               createIdentity("I31", "IN31", "IR31", asList("Test", "Test3"), ImmutableMap.of("KeyC", "ValueC"))
-               );
-       }
-
-       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);
-               ownIdentity.setProperties(properties);
-               return ownIdentity;
-       }
-
-       private Identity createIdentity(String id, String nickname, String requestUri, List<String> contexts, ImmutableMap<String, String> properties) {
-               Identity identity = new DefaultIdentity(id, nickname, requestUri);
-               identity.setContexts(contexts);
-               identity.setProperties(properties);
-               return identity;
-       }
-
-       @Test
-       public void loadingIdentities() throws WebOfTrustException {
-               List<OwnIdentity> ownIdentities = createOwnIdentities();
-               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));
-               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities.get(3)), eq(of("Test")));
-               assertThat(identities.keySet(), hasSize(4));
-               assertThat(identities.keySet(), containsInAnyOrder(ownIdentities.get(0), ownIdentities.get(1), ownIdentities.get(2), ownIdentities.get(3)));
-               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(0), createTrustedIdentitiesForFirstOwnIdentity());
-               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(1), createTrustedIdentitiesForSecondOwnIdentity());
-               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(2), Collections.<Identity>emptySet());
-               verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(3), createTrustedIdentitiesForFourthOwnIdentity());
-       }
-
-       @Test
-       public void loadingIdentitiesWithoutContext() throws WebOfTrustException {
-               List<OwnIdentity> ownIdentities = createOwnIdentities();
-               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()));
-               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);
-               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(Map<OwnIdentity, Collection<Identity>> identities, OwnIdentity ownIdentity, Set<Identity> trustedIdentities) {
-               assertThat(identities.get(ownIdentity), Matchers.<Collection<Identity>>is(trustedIdentities));
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt
new file mode 100644 (file)
index 0000000..5c363fc
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * Sone - IdentityLoaderTest.java - Copyright Â© 2013–2019 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot
+
+import com.google.common.base.Optional.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import org.mockito.ArgumentMatchers.any
+import org.mockito.ArgumentMatchers.eq
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [IdentityLoader].
+ */
+class IdentityLoaderTest {
+
+       private val webOfTrustConnector = mock<WebOfTrustConnector>()
+       private val identityLoader = IdentityLoader(webOfTrustConnector, of(Context("Test")))
+       private val identityLoaderWithoutContext = IdentityLoader(webOfTrustConnector)
+
+       @Before
+       fun setup() {
+               val ownIdentities = createOwnIdentities()
+               whenever(webOfTrustConnector.loadAllOwnIdentities()).thenReturn(ownIdentities.toSet())
+               whenever(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities[0]), any())).thenReturn(createTrustedIdentitiesForFirstOwnIdentity())
+               whenever(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities[1]), any())).thenReturn(createTrustedIdentitiesForSecondOwnIdentity())
+               whenever(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities[2]), any())).thenReturn(createTrustedIdentitiesForThirdOwnIdentity())
+               whenever(webOfTrustConnector.loadTrustedIdentities(eq(ownIdentities[3]), any())).thenReturn(createTrustedIdentitiesForFourthOwnIdentity())
+       }
+
+       private fun createOwnIdentities() = listOf(
+                       createOwnIdentity("O1", "ON1", "OR1", "OI1", listOf("Test", "Test2"), mapOf("KeyA" to "ValueA", "KeyB" to "ValueB")),
+                       createOwnIdentity("O2", "ON2", "OR2", "OI2", listOf("Test"), mapOf("KeyC" to "ValueC")),
+                       createOwnIdentity("O3", "ON3", "OR3", "OI3", listOf("Test2"), mapOf("KeyE" to "ValueE", "KeyD" to "ValueD")),
+                       createOwnIdentity("O4", "ON4", "OR$", "OI4", listOf("Test"), mapOf("KeyA" to "ValueA", "KeyD" to "ValueD"))
+       )
+
+       private fun createTrustedIdentitiesForFirstOwnIdentity() = setOf(
+                       createIdentity("I11", "IN11", "IR11", listOf("Test"), mapOf("KeyA" to "ValueA"))
+       )
+
+       private fun createTrustedIdentitiesForSecondOwnIdentity() = setOf(
+                       createIdentity("I21", "IN21", "IR21", listOf("Test", "Test2"), mapOf("KeyB" to "ValueB"))
+       )
+
+       private fun createTrustedIdentitiesForThirdOwnIdentity() = setOf(
+                       createIdentity("I31", "IN31", "IR31", listOf("Test", "Test3"), mapOf("KeyC" to "ValueC"))
+       )
+
+       private fun createTrustedIdentitiesForFourthOwnIdentity(): Set<Identity> = emptySet()
+
+       private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List<String>, properties: Map<String, String>): OwnIdentity =
+                       DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply {
+                               setContexts(contexts)
+                               this.properties = properties
+                       }
+
+       private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: List<String>, properties: Map<String, String>): Identity =
+                       DefaultIdentity(id, nickname, requestUri).apply {
+                               setContexts(contexts)
+                               this.properties = properties
+                       }
+
+       @Test
+       fun loadingIdentities() {
+               val ownIdentities = createOwnIdentities()
+               val identities = identityLoader.loadIdentities()
+               verify(webOfTrustConnector).loadAllOwnIdentities()
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq(of("Test")))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq(of("Test")))
+               verify(webOfTrustConnector, never()).loadTrustedIdentities(eq(ownIdentities[2]), any())
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq(of("Test")))
+               assertThat(identities.keys, hasSize(4))
+               assertThat(identities.keys, containsInAnyOrder(ownIdentities[0], ownIdentities[1], ownIdentities[2], ownIdentities[3]))
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities[0], createTrustedIdentitiesForFirstOwnIdentity())
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities[1], createTrustedIdentitiesForSecondOwnIdentity())
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities[2], emptySet())
+               verifyIdentitiesForOwnIdentity(identities, ownIdentities[3], createTrustedIdentitiesForFourthOwnIdentity())
+       }
+
+       @Test
+       fun loadingIdentitiesWithoutContext() {
+               val ownIdentities = createOwnIdentities()
+               val identities = identityLoaderWithoutContext.loadIdentities()
+               verify(webOfTrustConnector).loadAllOwnIdentities()
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[0]), eq(absent()))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[1]), eq(absent()))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[2]), eq(absent()))
+               verify(webOfTrustConnector).loadTrustedIdentities(eq(ownIdentities[3]), eq(absent()))
+               assertThat(identities.keys, hasSize(4))
+               val firstOwnIdentity = ownIdentities[0]
+               val secondOwnIdentity = ownIdentities[1]
+               val thirdOwnIdentity = ownIdentities[2]
+               val fourthOwnIdentity = ownIdentities[3]
+               assertThat(identities.keys, containsInAnyOrder(firstOwnIdentity, secondOwnIdentity, thirdOwnIdentity, fourthOwnIdentity))
+               verifyIdentitiesForOwnIdentity(identities, firstOwnIdentity, createTrustedIdentitiesForFirstOwnIdentity())
+               verifyIdentitiesForOwnIdentity(identities, secondOwnIdentity, createTrustedIdentitiesForSecondOwnIdentity())
+               verifyIdentitiesForOwnIdentity(identities, thirdOwnIdentity, createTrustedIdentitiesForThirdOwnIdentity())
+               verifyIdentitiesForOwnIdentity(identities, fourthOwnIdentity, createTrustedIdentitiesForFourthOwnIdentity())
+       }
+
+       private fun verifyIdentitiesForOwnIdentity(identities: Map<OwnIdentity, Collection<Identity>>, ownIdentity: OwnIdentity, trustedIdentities: Set<Identity>) {
+               assertThat(identities[ownIdentity], equalTo<Collection<Identity>>(trustedIdentities))
+       }
+
+}