Skip to content

Commit 12fa945

Browse files
authored
CORDA-680 Update cordapp packages documentation (corda#1901)
* Introduce MockNetworkParameters
1 parent dfd9070 commit 12fa945

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

confidential-identities/src/test/kotlin/net/corda/confidential/SwapIdentitiesFlowTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SwapIdentitiesFlowTests {
1313
@Test
1414
fun `issue key`() {
1515
// We run this in parallel threads to help catch any race conditions that may exist.
16-
val mockNet = MockNetwork(false, true)
16+
val mockNet = MockNetwork(threadPerNode = true)
1717

1818
// Set up values we'll need
1919
val notaryNode = mockNet.createNotaryNode()
@@ -53,7 +53,7 @@ class SwapIdentitiesFlowTests {
5353
@Test
5454
fun `verifies identity name`() {
5555
// We run this in parallel threads to help catch any race conditions that may exist.
56-
val mockNet = MockNetwork(false, true)
56+
val mockNet = MockNetwork(threadPerNode = true)
5757

5858
// Set up values we'll need
5959
val notaryNode = mockNet.createNotaryNode(DUMMY_NOTARY.name)
@@ -78,7 +78,7 @@ class SwapIdentitiesFlowTests {
7878
@Test
7979
fun `verifies signature`() {
8080
// We run this in parallel threads to help catch any race conditions that may exist.
81-
val mockNet = MockNetwork(false, true)
81+
val mockNet = MockNetwork(threadPerNode = true)
8282

8383
// Set up values we'll need
8484
val notaryNode = mockNet.createNotaryNode(DUMMY_NOTARY.name)

docs/source/key-concepts-contract-constraints.rst

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ to specify JAR URLs in the case that the CorDapp(s) involved in testing already
9595
MockNetwork/MockNode
9696
********************
9797

98-
The most simple way to ensure that a vanilla instance of a MockNode generates the correct CorDapps is to make a call
99-
to ``setCordappPackages`` before the MockNetwork/Node are created and then ``unsetCordappPackages`` after the test
100-
has finished. These calls will cause the ``AbstractNode`` to use the named packages as sources for CorDapps. All files
98+
The most simple way to ensure that a vanilla instance of a MockNode generates the correct CorDapps is to use the
99+
``cordappPackages`` constructor parameter (Kotlin) or the ``setCordappPackages`` method on ``MockNetworkParameters`` (Java)
100+
when creating the MockNetwork. This will cause the ``AbstractNode`` to use the named packages as sources for CorDapps. All files
101101
within those packages will be zipped into a JAR and added to the attachment store and loaded as CorDapps by the
102102
``CordappLoader``. An example of this usage would be:
103103

@@ -108,17 +108,7 @@ within those packages will be zipped into a JAR and added to the attachment stor
108108

109109
@Before
110110
void setup() {
111-
// The ordering of the two below lines is important - if the MockNetwork is created before the nodes and network
112-
// are created the CorDapps will not be loaded into the MockNodes correctly.
113-
setCordappPackages(Arrays.asList("com.domain.cordapp"))
114-
network = new MockNetwork()
115-
}
116-
117-
@After
118-
void teardown() {
119-
// This must be called at the end otherwise the global state set by setCordappPackages may leak into future
120-
// tests in the same test runner environment.
121-
unsetCordappPackages()
111+
network = new MockNetwork(new MockNetworkParameters().setCordappPackages(Arrays.asList("com.domain.cordapp")))
122112
}
123113

124114
... // Your tests go here

docs/source/upgrade-notes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ versions you are currently using are still in force.
2525

2626
We also strongly recommend cross referencing with the :doc:`changelog` to confirm changes.
2727

28+
UNRELEASED
29+
----------
30+
31+
Testing
32+
^^^^^^^
33+
34+
* The registration mechanism for CorDapps in ``MockNetwork`` unit tests has changed.
35+
36+
It is now done via the ``cordappPackages`` constructor parameter of MockNetwork.
37+
This takes a list of `String` values which should be the
38+
package names of the CorDapps containing the contract verification code you wish to load.
39+
The ``unsetCordappPackages`` method is now redundant and has been removed.
40+
2841
:ref:`Milestone 14 <changelog_m14>`
2942
------------
3043

node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeFlowTests.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
9999
// We run this in parallel threads to help catch any race conditions that may exist. The other tests
100100
// we run in the unit test thread exclusively to speed things up, ensure deterministic results and
101101
// allow interruption half way through.
102-
mockNet = MockNetwork(false, true, cordappPackages = cordappPackages)
102+
mockNet = MockNetwork(threadPerNode = true, cordappPackages = cordappPackages)
103103
ledger(MockServices(cordappPackages), initialiseSerialization = false) {
104104
val notaryNode = mockNet.createNotaryNode()
105105
val aliceNode = mockNet.createPartyNode(ALICE_NAME)
@@ -149,7 +149,7 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
149149

150150
@Test(expected = InsufficientBalanceException::class)
151151
fun `trade cash for commercial paper fails using soft locking`() {
152-
mockNet = MockNetwork(false, true, cordappPackages = cordappPackages)
152+
mockNet = MockNetwork(threadPerNode = true, cordappPackages = cordappPackages)
153153
ledger(MockServices(cordappPackages), initialiseSerialization = false) {
154154
val notaryNode = mockNet.createNotaryNode()
155155
val aliceNode = mockNet.createPartyNode(ALICE_NAME)
@@ -205,7 +205,7 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
205205

206206
@Test
207207
fun `shutdown and restore`() {
208-
mockNet = MockNetwork(false, cordappPackages = cordappPackages)
208+
mockNet = MockNetwork(cordappPackages = cordappPackages)
209209
ledger(MockServices(cordappPackages), initialiseSerialization = false) {
210210
val notaryNode = mockNet.createNotaryNode()
211211
val aliceNode = mockNet.createPartyNode(ALICE_NAME)
@@ -326,7 +326,7 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
326326

327327
@Test
328328
fun `check dependencies of sale asset are resolved`() {
329-
mockNet = MockNetwork(false, cordappPackages = cordappPackages)
329+
mockNet = MockNetwork(cordappPackages = cordappPackages)
330330
val notaryNode = mockNet.createNotaryNode()
331331
val aliceNode = makeNodeWithTracking(ALICE_NAME)
332332
val bobNode = makeNodeWithTracking(BOB_NAME)
@@ -433,7 +433,7 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
433433

434434
@Test
435435
fun `track works`() {
436-
mockNet = MockNetwork(false, cordappPackages = cordappPackages)
436+
mockNet = MockNetwork(cordappPackages = cordappPackages)
437437
val notaryNode = mockNet.createNotaryNode()
438438
val aliceNode = makeNodeWithTracking(ALICE_NAME)
439439
val bobNode = makeNodeWithTracking(BOB_NAME)
@@ -515,15 +515,15 @@ class TwoPartyTradeFlowTests(val anonymous: Boolean) {
515515

516516
@Test
517517
fun `dependency with error on buyer side`() {
518-
mockNet = MockNetwork(false, cordappPackages = cordappPackages)
518+
mockNet = MockNetwork(cordappPackages = cordappPackages)
519519
ledger(MockServices(cordappPackages), initialiseSerialization = false) {
520520
runWithError(true, false, "at least one cash input")
521521
}
522522
}
523523

524524
@Test
525525
fun `dependency with error on seller side`() {
526-
mockNet = MockNetwork(false, cordappPackages = cordappPackages)
526+
mockNet = MockNetwork(cordappPackages = cordappPackages)
527527
ledger(MockServices(cordappPackages), initialiseSerialization = false) {
528528
runWithError(false, true, "Issuances have a time-window")
529529
}

samples/network-visualiser/src/main/kotlin/net/corda/netmap/simulation/Simulation.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
140140
}
141141
}
142142

143-
val mockNet = MockNetwork(networkSendManuallyPumped, runAsync, cordappPackages = listOf("net.corda.irs.contract", "net.corda.finance.contract"))
143+
val mockNet = MockNetwork(
144+
networkSendManuallyPumped = networkSendManuallyPumped,
145+
threadPerNode = runAsync,
146+
cordappPackages = listOf("net.corda.irs.contract", "net.corda.finance.contract"))
144147
// This one must come first.
145148
val networkMap = mockNet.startNetworkMapNode(nodeFactory = NetworkMapNodeFactory)
146149
val notary = mockNet.createNotaryNode(validating = false, nodeFactory = NotaryNodeFactory)

testing/node-driver/src/main/kotlin/net/corda/testing/driver/Driver.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ sealed class PortAllocation {
272272
}
273273
}
274274

275-
/**
276-
* Helper builder for configuring a [Node] from Java.
277-
*/
275+
/** Helper builder for configuring a [Node] from Java. */
276+
@Suppress("unused")
278277
data class NodeParameters(
279278
val providedName: CordaX500Name? = null,
280279
val rpcUsers: List<User> = emptyList(),
@@ -366,9 +365,8 @@ fun <A> driver(
366365
return driver(defaultParameters = parameters, dsl = dsl)
367366
}
368367

369-
/**
370-
* Helper builder for configuring a [driver] from Java.
371-
*/
368+
/** Helper builder for configuring a [driver] from Java. */
369+
@Suppress("unused")
372370
data class DriverParameters(
373371
val isDebug: Boolean = false,
374372
val driverDirectory: Path = Paths.get("build", getTimestampAsDirectoryName()),

testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNode.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ fun StartedNode<MockNetwork.MockNode>.pumpReceive(block: Boolean = false): InMem
6161
return (network as InMemoryMessagingNetwork.InMemoryMessaging).pumpReceive(block)
6262
}
6363

64+
/** Helper builder for configuring a [MockNetwork] from Java. */
65+
@Suppress("unused")
66+
data class MockNetworkParameters(
67+
val networkSendManuallyPumped: Boolean = false,
68+
val threadPerNode: Boolean = false,
69+
val servicePeerAllocationStrategy: InMemoryMessagingNetwork.ServicePeerAllocationStrategy = InMemoryMessagingNetwork.ServicePeerAllocationStrategy.Random(),
70+
val defaultFactory: MockNetwork.Factory<*> = MockNetwork.DefaultFactory,
71+
val initialiseSerialization: Boolean = true,
72+
val cordappPackages: List<String> = emptyList()) {
73+
fun setNetworkSendManuallyPumped(networkSendManuallyPumped: Boolean) = copy(networkSendManuallyPumped = networkSendManuallyPumped)
74+
fun setThreadPerNode(threadPerNode: Boolean) = copy(threadPerNode = threadPerNode)
75+
fun setServicePeerAllocationStrategy(servicePeerAllocationStrategy: InMemoryMessagingNetwork.ServicePeerAllocationStrategy) = copy(servicePeerAllocationStrategy = servicePeerAllocationStrategy)
76+
fun setDefaultFactory(defaultFactory: MockNetwork.Factory<*>) = copy(defaultFactory = defaultFactory)
77+
fun setInitialiseSerialization(initialiseSerialization: Boolean) = copy(initialiseSerialization = initialiseSerialization)
78+
fun setCordappPackages(cordappPackages: List<String>) = copy(cordappPackages = cordappPackages)
79+
}
80+
6481
/**
6582
* A mock node brings up a suite of in-memory services in a fast manner suitable for unit testing.
6683
* Components that do IO are either swapped out for mocks, or pointed to a [Jimfs] in memory filesystem or an in
@@ -74,13 +91,16 @@ fun StartedNode<MockNetwork.MockNode>.pumpReceive(block: Boolean = false): InMem
7491
*
7592
* LogHelper.setLevel("+messages")
7693
*/
77-
class MockNetwork(private val networkSendManuallyPumped: Boolean = false,
78-
private val threadPerNode: Boolean = false,
79-
servicePeerAllocationStrategy: InMemoryMessagingNetwork.ServicePeerAllocationStrategy =
80-
InMemoryMessagingNetwork.ServicePeerAllocationStrategy.Random(),
81-
private val defaultFactory: Factory<*> = MockNetwork.DefaultFactory,
82-
private val initialiseSerialization: Boolean = true,
83-
private val cordappPackages: List<String> = emptyList()) : Closeable {
94+
class MockNetwork(defaultParameters: MockNetworkParameters = MockNetworkParameters(),
95+
private val networkSendManuallyPumped: Boolean = defaultParameters.networkSendManuallyPumped,
96+
private val threadPerNode: Boolean = defaultParameters.threadPerNode,
97+
servicePeerAllocationStrategy: InMemoryMessagingNetwork.ServicePeerAllocationStrategy = defaultParameters.servicePeerAllocationStrategy,
98+
private val defaultFactory: Factory<*> = defaultParameters.defaultFactory,
99+
private val initialiseSerialization: Boolean = defaultParameters.initialiseSerialization,
100+
private val cordappPackages: List<String> = defaultParameters.cordappPackages) : Closeable {
101+
/** Helper constructor for creating a [MockNetwork] with custom parameters from Java. */
102+
constructor(parameters: MockNetworkParameters) : this(defaultParameters = parameters)
103+
84104
companion object {
85105
// TODO In future PR we're removing the concept of network map node so the details of this mock are not important.
86106
val MOCK_NET_MAP = Party(CordaX500Name(organisation = "Mock Network Map", locality = "Madrid", country = "ES"), DUMMY_KEY_1.public)

0 commit comments

Comments
 (0)