From 35b0ceac0bdd4f7840db0d8a42c230e289f1bbc5 Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Tue, 6 Jun 2017 17:09:32 +0100 Subject: [PATCH] Clean up cash tests Clean up cash tests ahead of anonymisation work. This simplifies some boiler plate setup/teardown and ensures idenities and flows are correctly registered. --- .../net/corda/{core => }/flows/TxKeyFlow.kt | 7 +- .../core/flows/ContractUpgradeFlowTest.kt | 5 +- .../corda/{core => }/flows/TxKeyFlowTests.kt | 3 +- .../net/corda/flows/CashPaymentFlowTests.kt | 10 ++- .../kotlin/net/corda/flows/IssuerFlowTest.kt | 86 ++++++++----------- .../kotlin/net/corda/testing/node/MockNode.kt | 3 + 6 files changed, 54 insertions(+), 60 deletions(-) rename core/src/main/kotlin/net/corda/{core => }/flows/TxKeyFlow.kt (95%) rename core/src/test/kotlin/net/corda/{core => }/flows/TxKeyFlowTests.kt (95%) diff --git a/core/src/main/kotlin/net/corda/core/flows/TxKeyFlow.kt b/core/src/main/kotlin/net/corda/flows/TxKeyFlow.kt similarity index 95% rename from core/src/main/kotlin/net/corda/core/flows/TxKeyFlow.kt rename to core/src/main/kotlin/net/corda/flows/TxKeyFlow.kt index 36945d0759..3493b1cfad 100644 --- a/core/src/main/kotlin/net/corda/core/flows/TxKeyFlow.kt +++ b/core/src/main/kotlin/net/corda/flows/TxKeyFlow.kt @@ -1,9 +1,12 @@ -package net.corda.core.flows +package net.corda.flows import co.paralleluniverse.fibers.Suspendable +import net.corda.core.flows.FlowLogic +import net.corda.core.flows.InitiatedBy +import net.corda.core.flows.InitiatingFlow +import net.corda.core.flows.StartableByRPC import net.corda.core.identity.AnonymousParty import net.corda.core.identity.Party -import net.corda.core.identity.PartyAndCertificate import net.corda.core.serialization.CordaSerializable import net.corda.core.utilities.ProgressTracker import net.corda.core.utilities.unwrap diff --git a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt index 4e71b4d222..d0335f7c44 100644 --- a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt +++ b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt @@ -175,12 +175,13 @@ class ContractUpgradeFlowTest { // Create some cash. val result = a.services.startFlow(CashIssueFlow(Amount(1000, USD), OpaqueBytes.of(1), a.info.legalIdentity, notary)).resultFuture mockNet.runNetwork() + val stateAndRef = result.getOrThrow().tx.outRef(0) val baseState = a.database.transaction { a.vault.unconsumedStates().single() } assertTrue(baseState.state.data is Cash.State, "Contract state is old version.") - val stateAndRef = result.getOrThrow().tx.outRef(0) // Starts contract upgrade flow. - a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java)) + val upgradeResult = a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java)).resultFuture mockNet.runNetwork() + upgradeResult.getOrThrow() // Get contract state from the vault. val firstState = a.database.transaction { a.vault.unconsumedStates().single() } assertTrue(firstState.state.data is CashV2.State, "Contract state is upgraded to the new version.") diff --git a/core/src/test/kotlin/net/corda/core/flows/TxKeyFlowTests.kt b/core/src/test/kotlin/net/corda/flows/TxKeyFlowTests.kt similarity index 95% rename from core/src/test/kotlin/net/corda/core/flows/TxKeyFlowTests.kt rename to core/src/test/kotlin/net/corda/flows/TxKeyFlowTests.kt index 344c418d56..7490819a48 100644 --- a/core/src/test/kotlin/net/corda/core/flows/TxKeyFlowTests.kt +++ b/core/src/test/kotlin/net/corda/flows/TxKeyFlowTests.kt @@ -1,4 +1,4 @@ -package net.corda.core.flows +package net.corda.flows import net.corda.core.getOrThrow import net.corda.core.identity.AbstractParty @@ -37,7 +37,6 @@ class TxKeyFlowTests { bobNode.services.identityService.registerIdentity(notaryNode.info.legalIdentityAndCert) // Run the flows - bobNode.registerInitiatedFlow(TxKeyFlow.Provider::class.java) val requesterFlow = aliceNode.services.startFlow(TxKeyFlow.Requester(bob)) // Get the results diff --git a/finance/src/test/kotlin/net/corda/flows/CashPaymentFlowTests.kt b/finance/src/test/kotlin/net/corda/flows/CashPaymentFlowTests.kt index 577c7c0029..58779cf1b7 100644 --- a/finance/src/test/kotlin/net/corda/flows/CashPaymentFlowTests.kt +++ b/finance/src/test/kotlin/net/corda/flows/CashPaymentFlowTests.kt @@ -3,8 +3,8 @@ package net.corda.flows import net.corda.contracts.asset.Cash import net.corda.core.contracts.DOLLARS import net.corda.core.contracts.`issued by` -import net.corda.core.identity.Party import net.corda.core.getOrThrow +import net.corda.core.identity.Party import net.corda.core.serialization.OpaqueBytes import net.corda.testing.node.InMemoryMessagingNetwork.ServicePeerAllocationStrategy.RoundRobin import net.corda.testing.node.MockNetwork @@ -48,15 +48,17 @@ class CashPaymentFlowTests { @Test fun `pay some cash`() { val payTo = notaryNode.info.legalIdentity - val expected = 500.DOLLARS - val future = bankOfCordaNode.services.startFlow(CashPaymentFlow(expected, + val expectedPayment = 500.DOLLARS + val expectedChange = 1500.DOLLARS + val future = bankOfCordaNode.services.startFlow(CashPaymentFlow(expectedPayment, payTo)).resultFuture mockNet.runNetwork() val paymentTx = future.getOrThrow() val states = paymentTx.tx.outputs.map { it.data }.filterIsInstance() val ourState = states.single { it.owner.owningKey != payTo.owningKey } val paymentState = states.single { it.owner.owningKey == payTo.owningKey } - assertEquals(expected.`issued by`(bankOfCorda.ref(ref)), paymentState.amount) + assertEquals(expectedChange.`issued by`(bankOfCorda.ref(ref)), ourState.amount) + assertEquals(expectedPayment.`issued by`(bankOfCorda.ref(ref)), paymentState.amount) } @Test diff --git a/finance/src/test/kotlin/net/corda/flows/IssuerFlowTest.kt b/finance/src/test/kotlin/net/corda/flows/IssuerFlowTest.kt index 81193c7e1d..6bc015c3b6 100644 --- a/finance/src/test/kotlin/net/corda/flows/IssuerFlowTest.kt +++ b/finance/src/test/kotlin/net/corda/flows/IssuerFlowTest.kt @@ -17,9 +17,10 @@ import net.corda.core.utilities.DUMMY_NOTARY import net.corda.flows.IssuerFlow.IssuanceRequester import net.corda.testing.BOC import net.corda.testing.MEGA_CORP -import net.corda.testing.ledger import net.corda.testing.node.MockNetwork import net.corda.testing.node.MockNetwork.MockNode +import org.junit.After +import org.junit.Before import org.junit.Test import rx.Observable import java.util.* @@ -32,67 +33,52 @@ class IssuerFlowTest { lateinit var bankOfCordaNode: MockNode lateinit var bankClientNode: MockNode + @Before + fun start() { + mockNet = MockNetwork(threadPerNode = true) + notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) + bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) + bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name) + } + + @After + fun cleanUp() { + mockNet.stopNodes() + } + @Test fun `test issuer flow`() { - mockNet = MockNetwork(false, true) - ledger { - notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) - bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) - bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name) + // using default IssueTo Party Reference + val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS, + bankClientNode.info.legalIdentity, OpaqueBytes.of(123)) + assertEquals(issuerResult.get(), issuer.get().resultFuture.get()) - // using default IssueTo Party Reference - val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS, - bankClientNode.info.legalIdentity, OpaqueBytes.of(123)) - assertEquals(issuerResult.get(), issuer.get().resultFuture.get()) - - // try to issue an amount of a restricted currency - assertFailsWith { - runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(100000L, currency("BRL")), - bankClientNode.info.legalIdentity, OpaqueBytes.of(123)).issueRequestResult.getOrThrow() - } - - bankOfCordaNode.stop() - bankClientNode.stop() + // try to issue an amount of a restricted currency + assertFailsWith { + runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(100000L, currency("BRL")), + bankClientNode.info.legalIdentity, OpaqueBytes.of(123)).issueRequestResult.getOrThrow() } } @Test fun `test issue flow to self`() { - mockNet = MockNetwork(false, true) - ledger { - notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) - bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) - - // using default IssueTo Party Reference - val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankOfCordaNode, 1000000.DOLLARS, - bankOfCordaNode.info.legalIdentity, OpaqueBytes.of(123)) - assertEquals(issuerResult.get(), issuer.get().resultFuture.get()) - - bankOfCordaNode.stop() - } + // using default IssueTo Party Reference + val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankOfCordaNode, 1000000.DOLLARS, + bankOfCordaNode.info.legalIdentity, OpaqueBytes.of(123)) + assertEquals(issuerResult.get(), issuer.get().resultFuture.get()) } @Test fun `test concurrent issuer flow`() { - mockNet = MockNetwork(false, true) - ledger { - notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) - bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) - bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name) - - // this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states - val amount = 10000.DOLLARS - val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random()) - val handles = amounts.map { pennies -> - runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(pennies, amount.token), - bankClientNode.info.legalIdentity, OpaqueBytes.of(123)) - } - handles.forEach { - require(it.issueRequestResult.get() is SignedTransaction) - } - - bankOfCordaNode.stop() - bankClientNode.stop() + // this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states + val amount = 10000.DOLLARS + val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random()) + val handles = amounts.map { pennies -> + runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(pennies, amount.token), + bankClientNode.info.legalIdentity, OpaqueBytes.of(123)) + } + handles.forEach { + require(it.issueRequestResult.get() is SignedTransaction) } } diff --git a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt index fb9f649a48..f316911fcb 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt @@ -6,6 +6,7 @@ import com.google.common.util.concurrent.Futures import com.google.common.util.concurrent.ListenableFuture import net.corda.core.* import net.corda.core.crypto.entropyToKeyPair +import net.corda.flows.TxKeyFlow import net.corda.core.identity.PartyAndCertificate import net.corda.core.messaging.RPCOps import net.corda.core.messaging.SingleMessageRecipient @@ -287,6 +288,8 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false, val node = nodeFactory.create(config, this, networkMapAddress, advertisedServices.toSet(), id, overrideServices, entropyRoot) if (start) { node.setup().start() + // Register flows that are normally found via plugins + node.registerInitiatedFlow(TxKeyFlow.Provider::class.java) if (threadPerNode && networkMapAddress != null) node.networkMapRegistrationFuture.getOrThrow() // Block and wait for the node to register in the net map. }