mirror of
https://github.com/corda/corda.git
synced 2025-01-19 19:26:27 +00:00
Merged in rnicoll-identity-get (pull request #568)
Add get() method to identity service
This commit is contained in:
commit
1ec16dac3b
@ -10,6 +10,11 @@ import net.corda.core.crypto.Party
|
||||
*/
|
||||
interface IdentityService {
|
||||
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
|
||||
// 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.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]
|
||||
}
|
||||
|
@ -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 } }
|
||||
|
||||
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 partyFromName(name: String): Party? = nameToParties[name]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user