diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt index e5ee8728c8..d81fc4c529 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt @@ -128,6 +128,18 @@ class UnstartedMockNode private constructor(private val node: InternalMockNetwor * @return A [StartedMockNode] object. */ fun start(): StartedMockNode = StartedMockNode.create(node.start()) + + /** + * A [StartedMockNode] object for this running node. + * @throws [IllegalStateException] if the node is not running yet. + */ + val started: StartedMockNode + get() = StartedMockNode.create(node.started ?: throw IllegalStateException("Node ID=$id is not running")) + + /** + * Whether this node has been started yet. + */ + val isStarted: Boolean get() = node.started != null } /** A class that represents a started mock node for testing. */ diff --git a/testing/node-driver/src/test/kotlin/net/corda/testing/node/MockNetworkTest.kt b/testing/node-driver/src/test/kotlin/net/corda/testing/node/MockNetworkTest.kt new file mode 100644 index 0000000000..e9aa07276d --- /dev/null +++ b/testing/node-driver/src/test/kotlin/net/corda/testing/node/MockNetworkTest.kt @@ -0,0 +1,47 @@ +package net.corda.testing.node + +import net.corda.testing.core.* +import org.assertj.core.api.Assertions.* +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import kotlin.test.assertFailsWith + +class MockNetworkTest { + private companion object { + private const val NODE_ID = 101 + } + private lateinit var mockNetwork: MockNetwork + + @Before + fun setup() { + mockNetwork = MockNetwork(cordappPackages = emptyList()) + } + + @After + fun done() { + mockNetwork.stopNodes() + } + + @Test + fun `with a started node`() { + val unstarted = mockNetwork.createUnstartedNode(DUMMY_BANK_A_NAME, forcedID = NODE_ID) + assertFalse(unstarted.isStarted) + + mockNetwork.startNodes() + assertTrue(unstarted.isStarted) + + val started = unstarted.started + assertEquals(NODE_ID, started.id) + assertEquals(DUMMY_BANK_A_NAME, started.info.identityFromX500Name(DUMMY_BANK_A_NAME).name) + assertFailsWith { started.info.identityFromX500Name(DUMMY_BANK_B_NAME) } + } + + @Test + fun `with an unstarted node`() { + val unstarted = mockNetwork.createUnstartedNode(DUMMY_BANK_A_NAME, forcedID = NODE_ID) + val ex = assertFailsWith { unstarted.started } + assertThat(ex).hasMessage("Node ID=$NODE_ID is not running") + } +} \ No newline at end of file