Extending NetwokMapCache with a convenience method for by type service retrieval

This commit is contained in:
Michal Kit 2017-07-14 17:22:17 +01:00
parent 75a8e4d610
commit 66d6c2b44e
2 changed files with 41 additions and 0 deletions

View File

@ -119,6 +119,20 @@ interface NetworkMapCache {
return nodes.randomOrNull()?.notaryIdentity
}
/**
* Returns a service identity advertised by one of the nodes on the network
* @param type Specifies the type of the service
*/
fun getServiceOf(type: ServiceType): Party? {
for (node in partyNodes) {
val serviceIdentities = node.serviceIdentities(type)
if (serviceIdentities.isNotEmpty()) {
return serviceIdentities.randomOrNull()
}
}
return null;
}
/** Checks whether a given party is an advertised notary identity */
fun isNotary(party: Party): Boolean = notaryNodes.any { it.notaryIdentity == party }

View File

@ -0,0 +1,27 @@
package net.corda.node.services
import com.google.common.util.concurrent.Futures
import net.corda.core.getOrThrow
import net.corda.core.node.services.ServiceInfo
import net.corda.core.node.services.ServiceType
import net.corda.testing.DUMMY_BANK_A
import net.corda.testing.DUMMY_BANK_B
import net.corda.testing.node.NodeBasedTest
import org.bouncycastle.asn1.x500.X500Name
import org.junit.Test
import kotlin.test.assertEquals
class AdvertisedServiceTests : NodeBasedTest() {
private val serviceName = X500Name("CN=Custom Service,O=R3,OU=corda,L=London,C=GB")
private val serviceType = ServiceType.corda.getSubType("custom")
@Test
fun `service is accessible through getServiceOf`() {
val (bankA) = Futures.allAsList(
startNode(DUMMY_BANK_A.name),
startNode(DUMMY_BANK_B.name, advertisedServices = setOf(ServiceInfo(serviceType, serviceName)))
).getOrThrow()
val serviceParty = bankA.services.networkMapCache.getServiceOf(serviceType)
assertEquals(serviceName, serviceParty?.name)
}
}