From 792089e179dec2995d6fcbc32f392e6e2c68a556 Mon Sep 17 00:00:00 2001 From: Christian Sailer Date: Fri, 24 Nov 2017 14:23:37 +0000 Subject: [PATCH] Unit test for CashIssueAndPaymentFlow --- .../flows/CashIssueAndPaymentFlowTests.kt | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentFlowTests.kt diff --git a/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentFlowTests.kt b/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentFlowTests.kt new file mode 100644 index 0000000000..8033226110 --- /dev/null +++ b/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentFlowTests.kt @@ -0,0 +1,87 @@ +package com.r3.corda.enterprise.perftestcordapp.flows + +import com.r3.corda.enterprise.perftestcordapp.DOLLARS +import com.r3.corda.enterprise.perftestcordapp.`issued by` +import com.r3.corda.enterprise.perftestcordapp.contracts.asset.Cash +import net.corda.core.identity.Party +import net.corda.core.node.services.Vault +import net.corda.core.node.services.trackBy +import net.corda.core.node.services.vault.QueryCriteria +import net.corda.core.utilities.OpaqueBytes +import net.corda.core.utilities.getOrThrow +import net.corda.node.internal.StartedNode +import net.corda.testing.* +import net.corda.testing.node.InMemoryMessagingNetwork.ServicePeerAllocationStrategy.RoundRobin +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 kotlin.test.assertEquals + +class CashIssueAndPaymentFlowTests { + private lateinit var mockNet: MockNetwork + private val initialBalance = 2000.DOLLARS + private val ref = OpaqueBytes.of(0x01) + private lateinit var bankOfCordaNode: StartedNode + private lateinit var bankOfCorda: Party + private lateinit var aliceNode: StartedNode + private lateinit var notary: Party + + @Before + fun start() { + mockNet = MockNetwork(servicePeerAllocationStrategy = RoundRobin(), cordappPackages = listOf("com.r3.corda.enterprise.perftestcordapp.contracts.asset")) + bankOfCordaNode = mockNet.createPartyNode(BOC.name) + aliceNode = mockNet.createPartyNode(ALICE.name) + bankOfCorda = bankOfCordaNode.info.chooseIdentity() + mockNet.runNetwork() + notary = mockNet.defaultNotaryIdentity + } + + @After + fun cleanUp() { + mockNet.stopNodes() + } + + @Test + fun `issue and pay some cash`() { + val payTo = aliceNode.info.chooseIdentity() + val expectedPayment = 500.DOLLARS + + bankOfCordaNode.database.transaction { + // Register for vault updates + val criteria = QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.ALL) + val (_, vaultUpdatesBoc) = bankOfCordaNode.services.vaultService.trackBy(criteria) + val (_, vaultUpdatesBankClient) = aliceNode.services.vaultService.trackBy(criteria) + + val future = bankOfCordaNode.services.startFlow(CashIssueAndPaymentFlow(expectedPayment, OpaqueBytes.of(1), payTo, false, notary)).resultFuture + mockNet.runNetwork() + future.getOrThrow() + + // Check bank of corda vault - should see two consecutive updates of issuing $500 + // and paying $500 to alice + vaultUpdatesBoc.expectEvents { + sequence( + expect { update -> + require(update.produced.size == 1) { "Expected 1 produced states, actual: $update" } + val changeState = update.produced.single().state.data + assertEquals(expectedPayment.`issued by`(bankOfCorda.ref(ref)), changeState.amount) + }, + expect { update -> + require(update.consumed.size == 1) { "Expected 1 consumed states, actual: $update" } + } + ) + } + + // Check notary node vault updates + vaultUpdatesBankClient.expectEvents { + expect { (consumed, produced) -> + require(consumed.isEmpty()) { consumed.size } + require(produced.size == 1) { produced.size } + val paymentState = produced.single().state.data + assertEquals(expectedPayment.`issued by`(bankOfCorda.ref(ref)), paymentState.amount) + } + } + } + } +}