From 46d3d75e26cda2484932fcc2607d6768bc99e810 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 23 Sep 2019 20:56:47 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20IdentityLoaderTest=20wi?= =?utf8?q?th=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/IdentityLoaderTest.java | 154 --------------------- .../sone/freenet/wot/IdentityLoaderTest.kt | 124 +++++++++++++++++ 2 files changed, 124 insertions(+), 154 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt 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 index 8744c15..0000000 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.java +++ /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 . - */ - -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 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 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 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 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); - 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(); - 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)); - 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.emptySet()); - verifyIdentitiesForOwnIdentity(identities, ownIdentities.get(3), createTrustedIdentitiesForFourthOwnIdentity()); - } - - @Test - public void loadingIdentitiesWithoutContext() throws WebOfTrustException { - List ownIdentities = createOwnIdentities(); - 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())); - 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); - 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> identities, OwnIdentity ownIdentity, Set trustedIdentities) { - assertThat(identities.get(ownIdentity), Matchers.>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 index 0000000..5c363fc --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -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 . + */ + +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() + 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 = emptySet() + + private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: List, properties: Map): OwnIdentity = + DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply { + setContexts(contexts) + this.properties = properties + } + + private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: List, properties: Map): 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: OwnIdentity, trustedIdentities: Set) { + assertThat(identities[ownIdentity], equalTo>(trustedIdentities)) + } + +} -- 2.7.4