mirror of
https://github.com/corda/corda.git
synced 2025-06-13 04:38:19 +00:00
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:
@ -3,14 +3,14 @@ package net.corda.docs
|
||||
import net.corda.core.contracts.LinearState
|
||||
import net.corda.core.contracts.StateAndRef
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.node.ServiceHub
|
||||
import net.corda.core.node.services.queryBy
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.toFuture
|
||||
import net.corda.core.utilities.getOrThrow
|
||||
import net.corda.node.internal.StartedNode
|
||||
import net.corda.testing.DUMMY_NOTARY
|
||||
import net.corda.testing.chooseIdentity
|
||||
import net.corda.node.services.api.ServiceHubInternal
|
||||
import net.corda.testing.*
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
@ -19,8 +19,10 @@ import kotlin.test.assertEquals
|
||||
|
||||
class WorkflowTransactionBuildTutorialTest {
|
||||
lateinit var mockNet: MockNetwork
|
||||
lateinit var nodeA: StartedNode<MockNetwork.MockNode>
|
||||
lateinit var nodeB: StartedNode<MockNetwork.MockNode>
|
||||
lateinit var aliceServices: ServiceHubInternal
|
||||
lateinit var bobServices: ServiceHubInternal
|
||||
lateinit var alice: Party
|
||||
lateinit var bob: Party
|
||||
|
||||
// Helper method to locate the latest Vault version of a LinearState
|
||||
private inline fun <reified T : LinearState> ServiceHub.latest(ref: UniqueIdentifier): StateAndRef<T> {
|
||||
@ -31,10 +33,15 @@ class WorkflowTransactionBuildTutorialTest {
|
||||
@Before
|
||||
fun setup() {
|
||||
mockNet = MockNetwork(threadPerNode = true, cordappPackages = listOf("net.corda.docs"))
|
||||
// While we don't use the notary, we need there to be one on the network
|
||||
mockNet.createNotaryNode(legalName = DUMMY_NOTARY.name)
|
||||
nodeA = mockNet.createPartyNode()
|
||||
nodeB = mockNet.createPartyNode()
|
||||
nodeA.internals.registerInitiatedFlow(RecordCompletionFlow::class.java)
|
||||
val aliceNode = mockNet.createPartyNode(ALICE_NAME)
|
||||
val bobNode = mockNet.createPartyNode(BOB_NAME)
|
||||
aliceNode.internals.registerInitiatedFlow(RecordCompletionFlow::class.java)
|
||||
aliceServices = aliceNode.services
|
||||
bobServices = bobNode.services
|
||||
alice = aliceNode.services.myInfo.identityFromX500Name(ALICE_NAME)
|
||||
bob = bobNode.services.myInfo.identityFromX500Name(BOB_NAME)
|
||||
}
|
||||
|
||||
@After
|
||||
@ -45,50 +52,50 @@ class WorkflowTransactionBuildTutorialTest {
|
||||
@Test
|
||||
fun `Run workflow to completion`() {
|
||||
// Setup a vault subscriber to wait for successful upload of the proposal to NodeB
|
||||
val nodeBVaultUpdate = nodeB.services.vaultService.updates.toFuture()
|
||||
val nodeBVaultUpdate = bobServices.vaultService.updates.toFuture()
|
||||
// Kick of the proposal flow
|
||||
val flow1 = nodeA.services.startFlow(SubmitTradeApprovalFlow("1234", nodeB.info.chooseIdentity()))
|
||||
val flow1 = aliceServices.startFlow(SubmitTradeApprovalFlow("1234", bob))
|
||||
// Wait for the flow to finish
|
||||
val proposalRef = flow1.resultFuture.getOrThrow()
|
||||
val proposalLinearId = proposalRef.state.data.linearId
|
||||
// Wait for NodeB to include it's copy in the vault
|
||||
nodeBVaultUpdate.get()
|
||||
// Fetch the latest copy of the state from both nodes
|
||||
val latestFromA = nodeA.database.transaction {
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
val latestFromA = aliceServices.database.transaction {
|
||||
aliceServices.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
val latestFromB = nodeB.database.transaction {
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
val latestFromB = bobServices.database.transaction {
|
||||
bobServices.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
// Confirm the state as as expected
|
||||
assertEquals(WorkflowState.NEW, proposalRef.state.data.state)
|
||||
assertEquals("1234", proposalRef.state.data.tradeId)
|
||||
assertEquals(nodeA.info.chooseIdentity(), proposalRef.state.data.source)
|
||||
assertEquals(nodeB.info.chooseIdentity(), proposalRef.state.data.counterparty)
|
||||
assertEquals(alice, proposalRef.state.data.source)
|
||||
assertEquals(bob, proposalRef.state.data.counterparty)
|
||||
assertEquals(proposalRef, latestFromA)
|
||||
assertEquals(proposalRef, latestFromB)
|
||||
// Setup a vault subscriber to pause until the final update is in NodeA and NodeB
|
||||
val nodeAVaultUpdate = nodeA.services.vaultService.updates.toFuture()
|
||||
val secondNodeBVaultUpdate = nodeB.services.vaultService.updates.toFuture()
|
||||
val nodeAVaultUpdate = aliceServices.vaultService.updates.toFuture()
|
||||
val secondNodeBVaultUpdate = bobServices.vaultService.updates.toFuture()
|
||||
// Run the manual completion flow from NodeB
|
||||
val flow2 = nodeB.services.startFlow(SubmitCompletionFlow(latestFromB.ref, WorkflowState.APPROVED))
|
||||
val flow2 = bobServices.startFlow(SubmitCompletionFlow(latestFromB.ref, WorkflowState.APPROVED))
|
||||
// wait for the flow to end
|
||||
val completedRef = flow2.resultFuture.getOrThrow()
|
||||
// wait for the vault updates to stabilise
|
||||
nodeAVaultUpdate.get()
|
||||
secondNodeBVaultUpdate.get()
|
||||
// Fetch the latest copies from the vault
|
||||
val finalFromA = nodeA.database.transaction {
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
val finalFromA = aliceServices.database.transaction {
|
||||
aliceServices.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
val finalFromB = nodeB.database.transaction {
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
val finalFromB = bobServices.database.transaction {
|
||||
bobServices.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
// Confirm the state is as expected
|
||||
assertEquals(WorkflowState.APPROVED, completedRef.state.data.state)
|
||||
assertEquals("1234", completedRef.state.data.tradeId)
|
||||
assertEquals(nodeA.info.chooseIdentity(), completedRef.state.data.source)
|
||||
assertEquals(nodeB.info.chooseIdentity(), completedRef.state.data.counterparty)
|
||||
assertEquals(alice, completedRef.state.data.source)
|
||||
assertEquals(bob, completedRef.state.data.counterparty)
|
||||
assertEquals(completedRef, finalFromA)
|
||||
assertEquals(completedRef, finalFromB)
|
||||
}
|
||||
|
Reference in New Issue
Block a user