private val logger: Logger = Logger.getLogger(IdentityLoader::class.java.name)
- @Throws(WebOfTrustException::class)
- fun loadTrustedIdentities() =
- time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
- webOfTrustConnector.loadAllOwnIdentities()
- }.let(this::loadTrustedIdentitiesForOwnIdentities)
- .mergeRemoteIdentities()
-
fun loadAllIdentities() =
time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
webOfTrustConnector.loadAllOwnIdentities()
}.let(this::loadAllIdentitiesForOwnIdentities)
.mergeRemoteIdentities()
- @Throws(PluginException::class)
- private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
- ownIdentities
- .also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
- .associateWith { ownIdentity ->
- logger.fine { "Getting trusted identities for $ownIdentity..." }
- if (ownIdentity.doesNotHaveCorrectContext()) {
- logger.fine { "Skipping $ownIdentity because of incorrect context." }
- emptySet()
- } else {
- logger.fine { "Loading trusted identities for $ownIdentity from WoT..." }
- time({ stopwatch, identities -> "Loaded ${identities.size} identities for ${ownIdentity.nickname} in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
- webOfTrustConnector.loadTrustedIdentities(ownIdentity, context?.context)
- }
- }
- }
-
private fun loadAllIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
ownIdentities
.also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
import net.pterodactylus.sone.test.createIdentity
import net.pterodactylus.sone.test.createOwnIdentity
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.containsInAnyOrder
import org.hamcrest.Matchers.equalTo
-import org.hamcrest.Matchers.hasSize
-import org.hamcrest.Matchers.notNullValue
import org.junit.Test
/**
*/
class IdentityLoaderTest {
- private val ownIdentities = createOwnIdentities()
- private val webOfTrustConnector = dummyWebOfTrustConnector
- .overrideLoadAllOwnIdentities { ownIdentities.toSet() }
- .overrideLoadTrustedIdentities { ownIdentity, _ ->
- when (ownIdentity) {
- ownIdentities[0] -> createTrustedIdentitiesForFirstOwnIdentity()
- ownIdentities[1] -> createTrustedIdentitiesForSecondOwnIdentity()
- ownIdentities[2] -> createTrustedIdentitiesForThirdOwnIdentity()
- ownIdentities[3] -> createTrustedIdentitiesForFourthOwnIdentity()
- else -> throw RuntimeException()
- }
- }
-
- @Test
- fun loadingIdentities() {
- val identityLoader = IdentityLoader(webOfTrustConnector, Context("Test"))
- val identities = identityLoader.loadTrustedIdentities()
- 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 identityLoaderWithoutContext = IdentityLoader(webOfTrustConnector)
- val identities = identityLoaderWithoutContext.loadTrustedIdentities()
- 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], createTrustedIdentitiesForThirdOwnIdentity())
- verifyIdentitiesForOwnIdentity(identities, ownIdentities[3], createTrustedIdentitiesForFourthOwnIdentity())
- }
-
- private fun verifyIdentitiesForOwnIdentity(identities: Map<OwnIdentity, Collection<Identity>>, ownIdentity: OwnIdentity, trustedIdentities: Set<Identity>) {
- assertThat(identities[ownIdentity], equalTo<Collection<Identity>>(trustedIdentities))
- }
-
@Test
fun `loading all identities merges remote identities’ trust values`() {
val ownIdentity1 = createOwnIdentity("o1")
}
val identityLoader = IdentityLoader(webOfTrustConnector)
val allIdentities = identityLoader.loadAllIdentities()
- assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], notNullValue())
- }
-
- @Test
- fun `loading trusted identities merges remote identities’ trust values`() {
- val ownIdentity1 = createOwnIdentity("o1")
- val ownIdentity2 = createOwnIdentity("o2")
- 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())
+ assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], equalTo(Trust(80, 40, 2)))
+ assertThat(allIdentities[ownIdentity2]!!.first().trust[ownIdentity1], equalTo(Trust(100, 50, 2)))
}
}
-
-private fun createOwnIdentities() = listOf(
- createOwnIdentity("O1", "ON1", "OR1", "OI1", setOf("Test", "Test2"), mapOf("KeyA" to "ValueA", "KeyB" to "ValueB")),
- createOwnIdentity("O2", "ON2", "OR2", "OI2", setOf("Test"), mapOf("KeyC" to "ValueC")),
- createOwnIdentity("O3", "ON3", "OR3", "OI3", setOf("Test2"), mapOf("KeyE" to "ValueE", "KeyD" to "ValueD")),
- createOwnIdentity("O4", "ON4", "OR$", "OI4", setOf("Test"), mapOf("KeyA" to "ValueA", "KeyD" to "ValueD"))
-)
-
-private fun createTrustedIdentitiesForFirstOwnIdentity() = setOf(
- createIdentity("I11", "IN11", "IR11", setOf("Test"), mapOf("KeyA" to "ValueA"))
-)
-
-private fun createTrustedIdentitiesForSecondOwnIdentity() = setOf(
- createIdentity("I21", "IN21", "IR21", setOf("Test", "Test2"), mapOf("KeyB" to "ValueB"))
-)
-
-private fun createTrustedIdentitiesForThirdOwnIdentity() = setOf(
- createIdentity("I31", "IN31", "IR31", setOf("Test", "Test3"), mapOf("KeyC" to "ValueC"))
-)
-
-private fun createTrustedIdentitiesForFourthOwnIdentity(): Set<Identity> = emptySet()