From: David ‘Bombe’ Roden Date: Mon, 27 Jul 2020 20:34:42 +0000 (+0200) Subject: ✅ Use dummy WOT connector for identity loader test X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=06341a31cb9d80b027e395510e93c078aa988fcf;p=Sone.git ✅ Use dummy WOT connector for identity loader 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 4188adf..1123bec 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -32,9 +32,9 @@ import org.junit.Test class IdentityLoaderTest { private val ownIdentities = createOwnIdentities() - private val webOfTrustConnector = object : TestWebOfTrustConnector() { - override fun loadAllOwnIdentities() = ownIdentities.toSet() - override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = + private val webOfTrustConnector = dummyWebOfTrustConnector + .overrideLoadAllOwnIdentities { ownIdentities.toSet() } + .overrideLoadTrustedIdentities { ownIdentity, _ -> when (ownIdentity) { ownIdentities[0] -> createTrustedIdentitiesForFirstOwnIdentity() ownIdentities[1] -> createTrustedIdentitiesForSecondOwnIdentity() @@ -42,7 +42,7 @@ class IdentityLoaderTest { ownIdentities[3] -> createTrustedIdentitiesForFourthOwnIdentity() else -> throw RuntimeException() } - } + } @Test fun loadingIdentities() { @@ -76,13 +76,14 @@ class IdentityLoaderTest { fun `loading all identities merges remote identities’ trust values`() { val ownIdentity1 = createOwnIdentity("o1") val ownIdentity2 = createOwnIdentity("o2") - val webOfTrustConnector = object : TestWebOfTrustConnector() { - override fun loadAllOwnIdentities() = setOf(ownIdentity1, ownIdentity2) - override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?) = when (ownIdentity) { - ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) - else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) - } - } + val webOfTrustConnector = dummyWebOfTrustConnector + .overrideLoadAllOwnIdentities { setOf(ownIdentity1, ownIdentity2) } + .overrideLoadAllIdentities { ownIdentity, _ -> + when (ownIdentity) { + ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) + else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) + } + } val identityLoader = IdentityLoader(webOfTrustConnector) val allIdentities = identityLoader.loadAllIdentities() assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], notNullValue()) @@ -92,13 +93,14 @@ class IdentityLoaderTest { fun `loading trusted identities merges remote identities’ trust values`() { val ownIdentity1 = createOwnIdentity("o1") val ownIdentity2 = createOwnIdentity("o2") - val webOfTrustConnector = object : TestWebOfTrustConnector() { - override fun loadAllOwnIdentities() = setOf(ownIdentity1, ownIdentity2) - override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = when (ownIdentity) { - ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) - else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) - } - } + val webOfTrustConnector = dummyWebOfTrustConnector + .overrideLoadAllOwnIdentities { setOf(ownIdentity1, ownIdentity2) } + .overrideLoadTrustedIdentities { ownIdentity, _ -> + when (ownIdentity) { + ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) + else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) + } + } val identityLoader = IdentityLoader(webOfTrustConnector) val allIdentities = identityLoader.loadTrustedIdentities() assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], notNullValue()) @@ -126,16 +128,3 @@ private fun createTrustedIdentitiesForThirdOwnIdentity() = setOf( ) private fun createTrustedIdentitiesForFourthOwnIdentity(): Set = emptySet() - -private open class TestWebOfTrustConnector : WebOfTrustConnector { - - override fun loadAllOwnIdentities() = emptySet() - override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet() - override fun loadAllIdentities(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 ping() = Unit - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt index bf7b830..a5c26b9 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt @@ -13,6 +13,18 @@ val dummyWebOfTrustConnector = object : WebOfTrustConnector { open class DelegatingWebOfTrustConnector(private val delegate: WebOfTrustConnector) : WebOfTrustConnector by delegate +fun WebOfTrustConnector.overrideLoadAllOwnIdentities(override: () -> Set): WebOfTrustConnector = object : DelegatingWebOfTrustConnector(this) { + override fun loadAllOwnIdentities() = override() +} + +fun WebOfTrustConnector.overrideLoadTrustedIdentities(override: (ownIdentity: OwnIdentity, context: String?) -> Set): WebOfTrustConnector = object : DelegatingWebOfTrustConnector(this) { + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = override(ownIdentity, context) +} + +fun WebOfTrustConnector.overrideLoadAllIdentities(override: (ownIdentity: OwnIdentity, context: String?) -> Set): WebOfTrustConnector = object : DelegatingWebOfTrustConnector(this) { + override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?) = override(ownIdentity, context) +} + fun WebOfTrustConnector.overridePing(override: () -> Unit): WebOfTrustConnector = object : DelegatingWebOfTrustConnector(this) { override fun ping() = override() }