From: David ‘Bombe’ Roden Date: Fri, 1 Nov 2019 20:37:51 +0000 (+0100) Subject: ♻️ Use real interface instead of mock in test X-Git-Tag: v81^2~76 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=797507ccae2bf591a7e7b091f8067c2c961837a9;p=Sone.git ♻️ Use real interface instead of mock in test --- diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt index a085b79..f5fada8 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -17,76 +17,32 @@ package net.pterodactylus.sone.freenet.wot -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, Context("Test")) - private val identityLoaderWithoutContext = IdentityLoader(webOfTrustConnector) - - @Before - fun setup() { - val ownIdentities = createOwnIdentities() - whenever(webOfTrustConnector.loadAllOwnIdentities()).thenReturn(ownIdentities.toSet()) - whenever(webOfTrustConnector.loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[0]), any())).thenReturn(createTrustedIdentitiesForFirstOwnIdentity()) - whenever(webOfTrustConnector.loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[1]), any())).thenReturn(createTrustedIdentitiesForSecondOwnIdentity()) - whenever(webOfTrustConnector.loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[2]), any())).thenReturn(createTrustedIdentitiesForThirdOwnIdentity()) - whenever(webOfTrustConnector.loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[3]), any())).thenReturn(createTrustedIdentitiesForFourthOwnIdentity()) + private val ownIdentities = createOwnIdentities() + private val webOfTrustConnector = object : TestWebOfTrustConnector() { + override fun loadAllOwnIdentities() = ownIdentities.toSet() + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = + when (ownIdentity) { + ownIdentities[0] -> createTrustedIdentitiesForFirstOwnIdentity() + ownIdentities[1] -> createTrustedIdentitiesForSecondOwnIdentity() + ownIdentities[2] -> createTrustedIdentitiesForThirdOwnIdentity() + ownIdentities[3] -> createTrustedIdentitiesForFourthOwnIdentity() + else -> throw RuntimeException() + } } - 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 identityLoader = IdentityLoader(webOfTrustConnector, Context("Test")) val identities = identityLoader.loadIdentities() - verify(webOfTrustConnector).loadAllOwnIdentities() - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[0]), eq("Test")) - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[1]), eq("Test")) - verify(webOfTrustConnector, never()).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[2]), any()) - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[3]), eq("Test")) assertThat(identities.keys, hasSize(4)) assertThat(identities.keys, containsInAnyOrder(ownIdentities[0], ownIdentities[1], ownIdentities[2], ownIdentities[3])) verifyIdentitiesForOwnIdentity(identities, ownIdentities[0], createTrustedIdentitiesForFirstOwnIdentity()) @@ -97,23 +53,14 @@ class IdentityLoaderTest { @Test fun loadingIdentitiesWithoutContext() { - val ownIdentities = createOwnIdentities() + val identityLoaderWithoutContext = IdentityLoader(webOfTrustConnector) val identities = identityLoaderWithoutContext.loadIdentities() - verify(webOfTrustConnector).loadAllOwnIdentities() - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[0]), eq(null)) - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[1]), eq(null)) - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[2]), eq(null)) - verify(webOfTrustConnector).loadTrustedIdentities(net.pterodactylus.sone.test.eq(ownIdentities[3]), eq(null)) 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()) + 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], createTrustedIdentitiesForThirdOwnIdentity()) + verifyIdentitiesForOwnIdentity(identities, ownIdentities[3], createTrustedIdentitiesForFourthOwnIdentity()) } private fun verifyIdentitiesForOwnIdentity(identities: Map>, ownIdentity: OwnIdentity, trustedIdentities: Set) { @@ -121,3 +68,51 @@ class IdentityLoaderTest { } } + +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 + } + +private open class TestWebOfTrustConnector : WebOfTrustConnector { + + override fun loadAllOwnIdentities() = emptySet() + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet() + override fun addContext(ownIdentity: OwnIdentity, context: String) = Unit + override fun removeContext(ownIdentity: OwnIdentity, context: String) = Unit + override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) = Unit + override fun removeProperty(ownIdentity: OwnIdentity, name: String) = Unit + override fun getTrust(ownIdentity: OwnIdentity, identity: Identity) = Trust(null, null, null) + override fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) = Unit + override fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) = Unit + override fun ping() = Unit + +}