mirror of
https://github.com/corda/corda.git
synced 2025-05-29 21:54:26 +00:00
Add get() method to identity service
This commit is contained in:
parent
570470f255
commit
f5ecddb4b2
@ -10,6 +10,11 @@ import net.corda.core.crypto.Party
|
|||||||
*/
|
*/
|
||||||
interface IdentityService {
|
interface IdentityService {
|
||||||
fun registerIdentity(party: Party)
|
fun registerIdentity(party: Party)
|
||||||
|
/**
|
||||||
|
* Get all identities known to the service. This is expensive, and [partyFromKey] or [partyFromName] should be
|
||||||
|
* used in preference where possible.
|
||||||
|
*/
|
||||||
|
fun getAllIdentities(): Iterable<Party>
|
||||||
|
|
||||||
// There is no method for removing identities, as once we are made aware of a Party we want to keep track of them
|
// There is no method for removing identities, as once we are made aware of a Party we want to keep track of them
|
||||||
// indefinitely. It may be that in the long term we need to drop or archive very old Party information for space,
|
// indefinitely. It may be that in the long term we need to drop or archive very old Party information for space,
|
||||||
|
@ -4,6 +4,7 @@ import net.corda.core.crypto.CompositeKey
|
|||||||
import net.corda.core.crypto.Party
|
import net.corda.core.crypto.Party
|
||||||
import net.corda.core.node.services.IdentityService
|
import net.corda.core.node.services.IdentityService
|
||||||
import net.corda.core.serialization.SingletonSerializeAsToken
|
import net.corda.core.serialization.SingletonSerializeAsToken
|
||||||
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.annotation.concurrent.ThreadSafe
|
import javax.annotation.concurrent.ThreadSafe
|
||||||
|
|
||||||
@ -20,6 +21,9 @@ class InMemoryIdentityService() : SingletonSerializeAsToken(), IdentityService {
|
|||||||
nameToParties[party.name] = party
|
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 partyFromKey(key: CompositeKey): Party? = keyToParties[key]
|
||||||
override fun partyFromName(name: String): Party? = nameToParties[name]
|
override fun partyFromName(name: String): Party? = nameToParties[name]
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
@ -72,6 +72,7 @@ class MockIdentityService(val identities: List<Party>) : IdentityService, Single
|
|||||||
get() = synchronized(identities) { identities.associateBy { it.name } }
|
get() = synchronized(identities) { identities.associateBy { it.name } }
|
||||||
|
|
||||||
override fun registerIdentity(party: Party) { throw UnsupportedOperationException() }
|
override fun registerIdentity(party: Party) { throw UnsupportedOperationException() }
|
||||||
|
override fun getAllIdentities(): Iterable<Party> = ArrayList(keyToParties.values)
|
||||||
override fun partyFromKey(key: CompositeKey): Party? = keyToParties[key]
|
override fun partyFromKey(key: CompositeKey): Party? = keyToParties[key]
|
||||||
override fun partyFromName(name: String): Party? = nameToParties[name]
|
override fun partyFromName(name: String): Party? = nameToParties[name]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user