CORDA-654: Migrate test APIs to match identity changes (#1744)

Rework identity usage in tests to extract identity from nodes by name, rather than just arbitrarily choosing the first identity. This better models the intended design for production (future work).
This commit is contained in:
Ross Nicoll
2017-10-11 18:26:09 +01:00
committed by GitHub
parent ed79db6864
commit 327f0ebd73
12 changed files with 142 additions and 101 deletions

View File

@ -7,11 +7,13 @@ import net.corda.core.flows.NotaryFlow
import net.corda.core.flows.StateReplacementException
import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party
import net.corda.core.node.ServiceHub
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.transactions.WireTransaction
import net.corda.core.utilities.getOrThrow
import net.corda.core.utilities.seconds
import net.corda.node.internal.StartedNode
import net.corda.node.services.api.ServiceHubInternal
import net.corda.testing.*
import net.corda.testing.contracts.DummyContract
import net.corda.testing.node.MockNetwork
@ -39,11 +41,11 @@ class NotaryChangeTests {
oldNotaryNode = mockNet.createNotaryNode(legalName = DUMMY_NOTARY.name)
clientNodeA = mockNet.createNode()
clientNodeB = mockNet.createNode()
newNotaryNode = mockNet.createNotaryNode()
newNotaryNode = mockNet.createNotaryNode(legalName = DUMMY_NOTARY.name.copy(organisation = "Dummy Notary 2"))
mockNet.runNetwork() // Clear network map registration messages
oldNotaryNode.internals.ensureRegistered()
newNotaryParty = newNotaryNode.info.legalIdentities[1]
oldNotaryParty = oldNotaryNode.info.legalIdentities[1]
oldNotaryParty = newNotaryNode.services.networkMapCache.getNotary(DUMMY_NOTARY_SERVICE_NAME)!!
newNotaryParty = newNotaryNode.services.networkMapCache.getNotary(DUMMY_NOTARY_SERVICE_NAME.copy(organisation = "Dummy Notary 2"))!!
}
@After
@ -211,10 +213,10 @@ fun issueMultiPartyState(nodeA: StartedNode<*>, nodeB: StartedNode<*>, notaryNod
return stx.tx.outRef(0)
}
fun issueInvalidState(node: StartedNode<*>, notary: Party): StateAndRef<DummyContract.SingleOwnerState> {
val tx = DummyContract.generateInitial(Random().nextInt(), notary, node.info.chooseIdentity().ref(0))
fun issueInvalidState(services: ServiceHub, identity: Party, notary: Party): StateAndRef<DummyContract.SingleOwnerState> {
val tx = DummyContract.generateInitial(Random().nextInt(), notary, identity.ref(0))
tx.setTimeWindow(Instant.now(), 30.seconds)
val stx = node.services.signInitialTransaction(tx)
node.services.recordTransactions(stx)
val stx = services.signInitialTransaction(tx)
services.recordTransactions(stx)
return stx.tx.outRef(0)
}

View File

@ -9,10 +9,11 @@ import net.corda.core.flows.NotaryError
import net.corda.core.flows.NotaryException
import net.corda.core.flows.NotaryFlow
import net.corda.core.identity.Party
import net.corda.core.node.ServiceHub
import net.corda.core.transactions.SignedTransaction
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.utilities.getOrThrow
import net.corda.node.internal.StartedNode
import net.corda.node.services.api.ServiceHubInternal
import net.corda.node.services.issueInvalidState
import net.corda.testing.*
import net.corda.testing.contracts.DummyContract
@ -27,18 +28,22 @@ import kotlin.test.assertFailsWith
class ValidatingNotaryServiceTests {
lateinit var mockNet: MockNetwork
lateinit var notaryNode: StartedNode<MockNetwork.MockNode>
lateinit var clientNode: StartedNode<MockNetwork.MockNode>
lateinit var notaryServices: ServiceHubInternal
lateinit var aliceServices: ServiceHubInternal
lateinit var notary: Party
lateinit var alice: Party
@Before
fun setup() {
mockNet = MockNetwork(cordappPackages = listOf("net.corda.testing.contracts"))
notaryNode = mockNet.createNotaryNode(legalName = DUMMY_NOTARY.name)
clientNode = mockNet.createNode()
val notaryNode = mockNet.createNotaryNode(legalName = DUMMY_NOTARY.name)
val aliceNode = mockNet.createNode(legalName = ALICE_NAME)
mockNet.runNetwork() // Clear network map registration messages
notaryNode.internals.ensureRegistered()
notary = clientNode.services.getDefaultNotary()
notaryServices = notaryNode.services
aliceServices = aliceNode.services
notary = notaryServices.networkMapCache.getNotary(DUMMY_NOTARY_SERVICE_NAME)!!
alice = aliceServices.myInfo.identityFromX500Name(ALICE_NAME)
}
@After
@ -49,11 +54,11 @@ class ValidatingNotaryServiceTests {
@Test
fun `should report error for invalid transaction dependency`() {
val stx = run {
val inputState = issueInvalidState(clientNode, notary)
val inputState = issueInvalidState(aliceServices, alice, notary)
val tx = TransactionBuilder(notary)
.addInputState(inputState)
.addCommand(dummyCommand(clientNode.info.chooseIdentity().owningKey))
clientNode.services.signInitialTransaction(tx)
.addCommand(dummyCommand(alice.owningKey))
aliceServices.signInitialTransaction(tx)
}
val future = runClient(stx)
@ -67,11 +72,11 @@ class ValidatingNotaryServiceTests {
fun `should report error for missing signatures`() {
val expectedMissingKey = MEGA_CORP_KEY.public
val stx = run {
val inputState = issueState(clientNode)
val inputState = issueState(aliceServices, alice)
val command = Command(DummyContract.Commands.Move(), expectedMissingKey)
val tx = TransactionBuilder(notary).withItems(inputState, command)
clientNode.services.signInitialTransaction(tx)
aliceServices.signInitialTransaction(tx)
}
val ex = assertFailsWith(NotaryException::class) {
@ -87,16 +92,16 @@ class ValidatingNotaryServiceTests {
private fun runClient(stx: SignedTransaction): CordaFuture<List<TransactionSignature>> {
val flow = NotaryFlow.Client(stx)
val future = clientNode.services.startFlow(flow).resultFuture
val future = aliceServices.startFlow(flow).resultFuture
mockNet.runNetwork()
return future
}
fun issueState(node: StartedNode<*>): StateAndRef<*> {
val tx = DummyContract.generateInitial(Random().nextInt(), notary, node.info.chooseIdentity().ref(0))
val signedByNode = node.services.signInitialTransaction(tx)
val stx = notaryNode.services.addSignature(signedByNode, notary.owningKey)
node.services.recordTransactions(stx)
fun issueState(serviceHub: ServiceHub, identity: Party): StateAndRef<*> {
val tx = DummyContract.generateInitial(Random().nextInt(), notary, identity.ref(0))
val signedByNode = serviceHub.signInitialTransaction(tx)
val stx = notaryServices.addSignature(signedByNode, notary.owningKey)
serviceHub.recordTransactions(stx)
return StateAndRef(tx.outputStates().first(), StateRef(stx.id, 0))
}
}