Hyperledger blockchain business model. Unit Testing and BDD (Cucumber and Gherkin).
How to test Blockchain.
What is Blockchain?
Blockchain is an enabling technology with big potential. You can think of blockchain as a shared ledger for recording transactions in a secure, and verifiable way.
In the case of cryptocurrency, this becomes a means of transferring value authentically without involving third party intermediaries, such as credit card agencies, or banks. Or introducing the risk of falsification and tampering along the way.
Blockchain has two unique characteristics that differentiate it from Excel spreadsheet or relational database:
The first is that the ledger is distributed, or shared between multiple parties. Each party has an identical copy or version of the ledger.
Whenever a new transaction is committed, each participant on the network nodes is notified.
The second characteristic is that the ledger is cryptographically immutable. If I want to enter a transaction to the ledger, the network participants must agree that the transaction is true or authentic.
This is done using a consensus mechanism that the participants agree to use and verify the authenticity and the validity of the transaction. Once the transaction is committed to the ledger, it’s signed and hashed, and cannot be changed later.
Blockchain ledger concept can be applied to any multi-party process, or an asset by moving across organizational boundaries. It has a big potential for more mainstream uses.
Think of a supply chain, maybe even an application for a loan. These are the cases where you need some system of accountability.
In the early days blockchain technology was used to facilitates the immediate transfer of value without intermediaries. It turned out appealing to many businesses.Since then business units have started stipulating terms and conditions of the transferred value using a smart contract mechanism. It serves as sort of a digital representation of a real world contract, with a set of conditions and terms that govern the changes made in the ledger.
Let’s walk through a real world example of what smart contracts might look like within the blockchain world.
Example of an Ice Cream business model?
The thing about ice cream is that it’s gotta be milked, it’s gotta be manufactured, and shipped before it’s sold.
So there are at least three parties involved in the process before the ice cream reaches the customer. The dairy farmer, the ice cream manufacturer, and the shipper.
As an ice cream retailer, I have terms with each of these parties. I may want contractually require that the dairy farmer produces only organic milk. I want my manufacturer to make the ice cream according to my quality specifications. And I certainly expect the shipper to keep the ice cream cold, as they move it across the country. Because if it melts, and later refreezes before I sell it, people can get sick.
In each case all the parties that are involved here have a vested interest to make sure that the ice cream is arriving safely, stored safely, and everyone’s using an SLA. And I have contracts already in place with these parties.
What we can do with the blockchain is codify those contracts, and track the ice cream as it moves across parties in the chain, validating that the contractual terms have been met in real time, and entering the results to the ledger.
Using the blockchain as a single source of truth, it enables you to track the process and have an insight any time. For example, I could authorize a contract use IoT sensors on the refrigerated truck to record the temperature, and notify the network if a reading falls outside the pre-agreed range. This reading can trigger remediated action downstream and could stop the store from having the delivery of a contaminated ice cream truck.
Run the business model.
Business model has a function that simulates IoT GPS sensor we need it to check the location of the truck, if it reached its destination.
Download the project to see this business model on a real example: https://github.com/NikolaiB/icecream-perishable-network
Navigate to the directory where the icecream-perishable-network code lives
Use nvm to install Node.js: nvm install — lts
Install Composer command-line interface (CLI): npm install -g composer-cli
Now the test project installed and it’s time to build and test it.
Use the Node.js package manager (npm) to run a build, and then execute the unit tests I have provided. Execute these commands:
npm install && npm test
The first thing you see here are Mocha tests, indicating the tests were successful.
Cucumber test scenarios in a language called Gherkin have fairly simple syntax and consist of Scenarios that look like this:
Feature : The application feature I want to test
Background: Given some pre-condition that applies to each test
Scenario: The test I want to run
Given some initial context (the givens),
When an event occurs,
Then ensure some outcomes.
For example let’s take a look closely to one of our scenarios:
There are three keywords:
Given I submit the following transaction of type org.acme.shipping.perishable.TemperatureReading
When I submit the following transaction of type org.acme.shipping.perishable.ShipmentReceived
Then I should have the following participants
Cucumber makes no distinction between the three keywords (they are just steps), but the patterns following the keywords must match one (and only one) step.
Otherwise, the test will not run, because there is no code to run it.
Take into account, Cucumber doesn’t distinguish between any of the step keywords (Given, When, And, and so forth). It only matches the pattern.
The pattern to match is the regular expression between the / characters in parentheses, including the capture groups (([.\w]+)\.(\w+)), which are used to parse the arguments that are passed to the function that gets invoked: namespace(org.acme.shipping.perishable), name (TemperatureReading), and table.
Objects in Gherkin
There are two ways to represent objects from the Perishable Goods network model in the Gherkin examples.
The first type of object representation is called a data table. The first line contains the property names, followed by one or more lines that contain the property values. The data table does not support nested objects.
The second type uses type a docstring, which is a JSON representation of the object, and supports nested objects (for example, notice the nested Address object).
Following this example you can create your own business model and test it.