Add get() method to identity service

This commit is contained in:
Ross Nicoll
2016-11-28 14:34:33 +00:00
parent 570470f255
commit f5ecddb4b2
4 changed files with 60 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import net.corda.core.crypto.CompositeKey
import net.corda.core.crypto.Party
import net.corda.core.node.services.IdentityService
import net.corda.core.serialization.SingletonSerializeAsToken
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import javax.annotation.concurrent.ThreadSafe
@ -20,6 +21,9 @@ class InMemoryIdentityService() : SingletonSerializeAsToken(), IdentityService {
nameToParties[party.name] = party
}
// We give the caller a copy of the data set to avoid any locking problems
override fun getAllIdentities(): Iterable<Party> = ArrayList(keyToParties.values)
override fun partyFromKey(key: CompositeKey): Party? = keyToParties[key]
override fun partyFromName(name: String): Party? = nameToParties[name]
}

View File

@ -0,0 +1,50 @@
package net.corda.node.services
import net.corda.node.services.identity.InMemoryIdentityService
import net.corda.testing.ALICE
import net.corda.testing.ALICE_PUBKEY
import net.corda.testing.BOB
import net.corda.testing.BOB_PUBKEY
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
/**
* Tests for the in memory identity service.
*/
class InMemoryIdentityServiceTests {
@Test
fun `get all identities`() {
val service = InMemoryIdentityService()
assertNull(service.getAllIdentities().firstOrNull())
service.registerIdentity(ALICE)
var expected = setOf(ALICE)
var actual = service.getAllIdentities().toHashSet()
assertEquals(expected, actual)
// Add a second party and check we get both back
service.registerIdentity(BOB)
expected = setOf(ALICE, BOB)
actual = service.getAllIdentities().toHashSet()
assertEquals(expected, actual)
}
@Test
fun `get identity by key`() {
val service = InMemoryIdentityService()
assertNull(service.partyFromKey(ALICE_PUBKEY))
service.registerIdentity(ALICE)
assertEquals(ALICE, service.partyFromKey(ALICE_PUBKEY))
assertNull(service.partyFromKey(BOB_PUBKEY))
}
@Test
fun `get identity by name`() {
val service = InMemoryIdentityService()
assertNull(service.partyFromName(ALICE.name))
service.registerIdentity(ALICE)
assertEquals(ALICE, service.partyFromName(ALICE.name))
assertNull(service.partyFromName(BOB.name))
}
}