Integration testing for CorDapps using NodeDriver
Testing is an essential activity while developing any software application. Most software developers write unit tests for the software component they build, but something that might get overlooked is an integration test. While different parts of a software application might work fine individually, there is no guarantee that they would behave the same when integrated. This is why integration tests are crucial for any software application.
We understand the importance of testing CorDapps; this is why Corda provides the TestDSL library to help CorDapp developers write unit tests for different components of their CorDapps, namely; states, contracts, and flows. But it’s also important to test out how these components would behave when put together.
What should a CorDapp Integration test do?
Ideally, an integration test of a CorDapp should execute a transaction using a flow that would generally update the ledger, and we should be able to able to query the ledger to verify that update. This would cover all three components of the CorDapp, i.e., state, contract, and flow. Flow builds a transaction, the validity of the transaction is checked by the contract and the state update is recorded on the ledger.
To achieve the above, you need to have a few Corda nodes and call the flow using the node’s RPC endpoint. But is it possible to bring up Corda nodes programmatically?
Corda provides the NodeDriver to help developers write integration tests. Using the NodeDriver, developers can bring up nodes locally to run flows and inspect state updates. The NodeDriver allows developers to access the node using RPC endpoints.
Let’s understand this by writing a simple integration test for the Token Bootcamp Cordapp. It’s a very simple CorDapp which can issue tokens to participants in the network.
You can do the integration test of the issuance functionality. So, you would trigger the TokenIssuanceFlow with PartyA as issuer and PartyB as owner and verify that after issuance, the token is reflected correctly in the ledger of each participant.
The Bootcamp CorDapp is not implemented, but you may already have the solution if you have attended one of our Corda Bootcamps. In case you haven’t had a chance to join us for a live bootcamp, you can find the recording here: https://www.youtube.com/watch?v=16sfeScW6ks. The solution is also available in Solutions.md
Step 1: Create Test Identity.
First, you need some TestIdentity for the nodes you will spin up using NodeDriver. You can use Corda TestDSL to create test identities for each of your three nodes (PartyA, PartyB and PartyC) as follows:
Note that you do not need to worry about the notary. It will be automatically taken care of by the NodeDriver.
Step 2: Start the Nodes using NodeDriver.
Below is how you can start the nodes and get a NodeHandle object corresponding to the node, which is used to access various node features like rpc.
Note that the NodeDriver takes a DriverParameters object, which can be used to tweak some configuration options of the NodeDriver.
Step3: Start the flow
Once you have the NodeHandle, you can access its rpc to trigger flows. The TokenIssue flow takes two parameters: owner and amount. Anyone who runs the flow is inferred as the issuer.
Thus PartyA is the issuer of the token and PartyB is the owner (or holder) of the token for this example. The amount of token being issued is 100.
Step 4: Query the ledger to validate the issuance
As per Corda’s privacy model, the token information must only be available with participants of the transaction, which are PartyA and PartyB . PartyCshould have no information about the token in its ledger.
To verify the same, we can again use the NodeHandle of individual nodes to perform a vaultQuery.
The above should work as an integration test for the issuance functionality of the token. The complete test case can be found here.
That brings us to the end of this post. Thank you so much for reading.
Want to learn more about building awesome blockchain applications on Corda? Be sure to visit https://corda.net, check out our community page to learn how to connect with other Corda developers, and sign up for one of our newsletters for the latest updates.
— Ashutosh Meher is a Developer Evangelist at R3, an enterprise blockchain software firm working with a global ecosystem of more than 350 participants across multiple industries from both the private and public sectors to develop on Corda, its open-source blockchain platform, and Corda Enterprise, a commercial version of Corda for enterprise usage.
Follow Ashutosh on Twitter here.