-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOUFlowTests.kt
121 lines (103 loc) · 4.16 KB
/
IOUFlowTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.template
import com.template.flows.IOUFlow
import com.template.states.IOUState
import net.corda.core.contracts.TransactionVerificationException
import net.corda.core.node.services.queryBy
import net.corda.core.utilities.getOrThrow
import net.corda.testing.core.singleIdentity
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetworkParameters
import net.corda.testing.node.TestCordapp
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class IOUFlowTests {
private val network = MockNetwork(MockNetworkParameters(cordappsForAllNodes = listOf(
TestCordapp.findCordapp("com.template.contracts"),
TestCordapp.findCordapp("com.template.flows")
)))
private val a = network.createNode()
private val b = network.createNode()
init {
listOf(a, b).forEach {
it.registerInitiatedFlow(IOUFlow.Responder::class.java)
}
}
@Before
fun setup() = network.runNetwork()
@After
fun tearDown() = network.stopNodes()
@Test
fun `flow rejects invalid IOUs`() {
val flow = IOUFlow.Initiator(-1, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
assertFailsWith<TransactionVerificationException> { future.getOrThrow() }
}
@Test
fun `SignedTransaction returned by the flow is signed by the initiator`() {
val flow = IOUFlow.Initiator(1, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
val signedTx = future.getOrThrow()
signedTx.verifySignaturesExcept(b.info.singleIdentity().owningKey)
}
@Test
fun `SignedTransaction returned by the flow is signed by the acceptor`() {
val flow = IOUFlow.Initiator(1, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
val signedTx = future.getOrThrow()
signedTx.verifySignaturesExcept(a.info.singleIdentity().owningKey)
}
@Test
fun `flow records a transaction in both parties' transaction storages`() {
val flow = IOUFlow.Initiator(1, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
val signedTx = future.getOrThrow()
// We check the recorded transaction in both transaction storages.
for (node in listOf(a, b)) {
assertEquals(signedTx, node.services.validatedTransactions.getTransaction(signedTx.id))
}
}
@Test
fun `recorded transaction has no inputs and a single output, the input IOU`() {
val iouValue = 1
val flow = IOUFlow.Initiator(iouValue, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
val signedTx = future.getOrThrow()
// We check the recorded transaction in both vaults.
for (node in listOf(a, b)) {
val recordedTx = node.services.validatedTransactions.getTransaction(signedTx.id)
val txOutputs = recordedTx!!.tx.outputs
assert(txOutputs.size == 1)
val recordedState = txOutputs[0].data as IOUState
assertEquals(recordedState.value, iouValue)
assertEquals(recordedState.lender, a.info.singleIdentity())
assertEquals(recordedState.borrower, b.info.singleIdentity())
}
}
@Test
fun `flow records the correct IOU in both parties' vaults`() {
val iouValue = 1
val flow = IOUFlow.Initiator(1, b.info.singleIdentity())
val future = a.startFlow(flow)
network.runNetwork()
future.getOrThrow()
// We check the recorded IOU in both vaults.
for (node in listOf(a, b)) {
node.transaction {
val ious = node.services.vaultService.queryBy<IOUState>().states
assertEquals(1, ious.size)
val recordedState = ious.single().state.data
assertEquals(recordedState.value, iouValue)
assertEquals(recordedState.lender, a.info.singleIdentity())
assertEquals(recordedState.borrower, b.info.singleIdentity())
}
}
}
}