CORDA-1862: Allow MockNetwork to create StartedMockNode from UnstartedMockNode. (#3731)

* Allow MockNetwork to create StartedMockNode from UnstartedMockNode.
* Reimplement by adding a `started` property to UnstartedMockNode.
* Throw IllegalStateException instead of NoSuchElementException.
* Add an isStarted property to UnstartedMockNode.
This commit is contained in:
Chris Rankin 2018-08-01 08:35:39 +01:00 committed by GitHub
parent 0762a61aca
commit 01d896394a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View File

@ -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. */

View File

@ -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<IllegalArgumentException> { 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<IllegalStateException> { unstarted.started }
assertThat(ex).hasMessage("Node ID=$NODE_ID is not running")
}
}