From 66d6c2b44e1197639f8076d6f30f1dd06bc6b330 Mon Sep 17 00:00:00 2001 From: Michal Kit Date: Fri, 14 Jul 2017 17:22:17 +0100 Subject: [PATCH] Extending NetwokMapCache with a convenience method for by type service retrieval --- .../core/node/services/NetworkMapCache.kt | 14 ++++++++++ .../node/services/AdvertisedServiceTests.kt | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 node/src/integration-test/kotlin/net/corda/node/services/AdvertisedServiceTests.kt diff --git a/core/src/main/kotlin/net/corda/core/node/services/NetworkMapCache.kt b/core/src/main/kotlin/net/corda/core/node/services/NetworkMapCache.kt index a651790053..e5821d9ef2 100644 --- a/core/src/main/kotlin/net/corda/core/node/services/NetworkMapCache.kt +++ b/core/src/main/kotlin/net/corda/core/node/services/NetworkMapCache.kt @@ -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 } diff --git a/node/src/integration-test/kotlin/net/corda/node/services/AdvertisedServiceTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/AdvertisedServiceTests.kt new file mode 100644 index 0000000000..2e66e4e016 --- /dev/null +++ b/node/src/integration-test/kotlin/net/corda/node/services/AdvertisedServiceTests.kt @@ -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) + } +}