Test Your Chaincode Using the CLI

If your chaincode is running on a network, you can test any of the generated methods. Additionally, If you chose to create the executeQuery method during your chaincode development, you can run SQL rich queries if your chaincode is deployed to an Oracle Blockchain Platform network.

Test Your Chaincode on a Local Hyperledger Fabric Network

Once your chaincode project is running on a local network, you can test it.

Open a new shell and navigate to the project directory to interact with your chaincodes. After a chaincode is installed and deployed, you can submit transactions to the functions inside your chaincode by using the ochain invoke and ochain query commands.

ochain invoke

Usage: ochain invoke <methodName> <methodArguments>

The following are arguments and options taken by the ochain invoke command:
my-mac:TSProject myname$ ochain invoke -h 
Usage: invoke [options] <methodName> [...args] 
Invoke a Chaincode transaction

Arguments :
    <methodName>  (required) Name of chaincode method to invoke.
    [...args]     (optional) Chaincode method input parameters if any. Parameters should be space separated strings/JSON strings for objects.

Options:
    -h, --help                output command usage information
    -D, --debug               enable debug logging
    -P, --project <path>      (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -c, --channel <channel>   (optional) Blockchain Channel to deploy chaincode to. If not specified, it defaults to the 'default' channel.
    -u, --username <username> (optional, if -r option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.
    -s, --sign <password>     (optional) Password to authenticate user.
    -r, --url <url>           (required) URL of the remote OBP instance. Example: https://<blockchain-instance-url>:7443/

Examples:
$> ochain invoke <method>
(without chaincode initial arguments)  
$> ochain invoke <method> '{"manufacturerId":"m01","rawMaterialAvailable":9,"productsAvailable":4,"completionDate":"05-26-2020"}'
(for a single parameter)
$> ochain invoke <method> 's01' 's10'
$> ochain invoke <method> 's01' '{"manufacturerId":"m01","rawMaterialAvailable":9,"productsAvailable":4,"completionDate":"05-26-2020"}'
(for multiple parameters)
$> ochain invoke <method> 's01' 's10' -r <url of the remote OBP instance> -u <username> -s <password>
(for remote invocation)

Mac OSX and Linux

If the method takes one argument, enter it as a string. For example:
ochain invoke createSupplier
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid supplier","expiryDate":"2020-05-30","active":true}'
Another example:
ochain invoke getSupplierDetails 's01'
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid supplier","expiryDate":"2020-05-30","active":true}'
If the method takes more than one argument, they should be separated by a space. For example:
ochain invoke getSupplierByRange 's01' 's03'
If you have embedded assets in your chaincode such as an employee asset which uses an embedded address asset:
name: employee
  properties:
     name: employeeId
     type: string
     mandatory: true
     id: true

     name: firstName
     type: string
     validate: max(30)
     mandatory: true

     name: lastName
     type: string
     validate: max(30)
     mandatory: true

     name: age
     type: number
     validate: positive(),min(18)

     name: address
     type: address
name: address

type: embedded

properties:
   name: street
   type: string

   name: city
   type: string

   name: state
   type: string

   name: country
   type: string
You would use something similar to the following to invoke the chaincode:
ochain invoke createEmployee '{"employeeID":"e01", "firstName":"John", "lastName":"Doe", 
"age":35, "address":{"street":"Elm Ave", "city":"LA", "state":"California", "country":"US"}}'

Windows

Windows command prompt doesn't accept single quotes ('), so all arguments have to be kept in double quotes ("). Any argument that contains a double quote must be escaped.

For example:
ochain invoke createSupplier
"{\"supplierId\":\"s01\",\"rawMaterialAvailable\":5,\"license\":\"valid
supplier\",\"expiryDate\":\"2020-05-30\",\"active\":true}"
If the method takes more than one argument, they should be separated by a space. For example:
ochain invoke getSupplierByRange "s01" "s03"
If you have embedded assets in your chaincode such as an employee asset which uses an embedded address asset as shown above, you can use something similar to the following to invoke the chaincode:
ochain invoke createEmployee "{\"employeeID\":\"e01\", \"firstName\":\"John\", 
\"lastName\":\"Doe\", \"age\":35, \"address\":{\"street\":\"Elm Ave\", \"city\":\"LA\", 
\"state\":\"California\", \"country\":\"US\"}}"

Validations

The method arguments are validated against the validations specified in the specification file. If any validation fails, errors will be listed in the output.

When it invokes successfully it should display a log similar to:
========== Started Invoke Chaincode ==========
[2020-06-23T18:37:54.563] [INFO] default - Successfully sent Proposal and received ProposalResponse
[2020-06-23T18:37:56.619] [INFO default - The chaincode invoke transaction has been committed on peer localhost:7051
[2020-06-23T18:37:56.619] [INFO] default - The chaincode invoke transaction was valid.
[2020-06-23T18:37:56.620] [INFO default - Successfully sent transaction to the orderer.
[2020-06-23T18:37:56.620] [INFO] default - Successfully invoked method "createSupplier" on chaincode "TSProject" on channel "mychannel"
[2020-06-23T18:37:56.620] [INFO] default - 
========== Finished Invoke Chaincode ==========

ochain query

Usage: ochain query <methodName> <methodArguments>

Following are the arguments and options taken by the ochain query command:
my-mac:TSProject myname$ ochain query -h 
Usage: query [options] <methodName> [...args] 
Invoke a Chaincode Query.

Arguments :
    <methodName>     (required) Name of chaincode method to invoke.
    [...args]        (optional) Chaincode method input parameters if any. Parameters should be space separated strings/JSON strings for objects.

Options:
    -h, --help                output command usage information
    -D, --debug               enable debug logging
    -P, --project <path>      (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -c, --channel <channel>   (optional) Blockchain Channel to deploy chaincode to. If not specified, it defaults to the 'default' channel.
    -u, --username <username> (optional, if -r option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.
    -s, --sign <password>     (optional) Password to authenticate user.
    -r, --url <url>           (required) URL of the remote OBP instance. Example: https://<blockchain-instance-url>:7443/

Examples:
$> ochain query <method>
(without chaincode initial arguments)
$> ochain query <method> s01
(for a single parameter)
$> ochain query <method> 's01' 's10'
$> ochain query <method> 's01' '{"manufacturerId":"m01"}'
(for multiple parameters)
$> ochain query <method> 's01' 's10' -r <url of the remote OBP instance> -u <username> -s <password>
(for remote query)
The ochain query command follows the same rules of passing <methodName> and <methodArguments> as ochain invoke.
  • On Mac OSX and Linux, single quotes can be used and there's no need to escape quotes within arguments.
  • On Windows, all arguments must surrounded by double quotes and any quote within an argument must be escaped.

Testing Multiple Token Users Locally

To test a token project with multiple users locally, you can use the tokenUser property to change the caller of each transaction. Every scaffolded chaincode project includes a .ochain.json file, which stores metadata of the chaincode. You change the caller by updating the value of tokenUser field in the .ochain.json file.

{
  "name": "digiCurrCC",
  "description": "Chaincode package for digiCurrCC",
  "chaincodeName": "digiCurrCC",
  "chaincodeType": "node",
  "configFileLocation": "/Users/user1/token.yml",
  "appBuilderVersion": "21.2.3",
  "nodeVersion": "v12.18.1",
  "tokenUser": "admin"
}

When a project is scaffolded, the tokenUser property is set to the default admin user of the local network. To change the caller of a transaction, change the tokenUser property to match the user_id property that was set when the account was created when the createAccount (TypeScript) or CreateAccount (Go) method was called.

Automatic Installation and Deployment After Update

Whenever you update your chaincode, the changes will be compiled, installed and deployed automatically when it's deployed to a local network. There is no need to strip down or bring up the local network again. All projects will be automatically compiled and deployed on every change.

Test Your Chaincode on a Remote Oracle Blockchain Platform Network

After your chaincode project has successfully deployed to your remote Oracle Blockchain Platform network, you can test it as described in Test Your Chaincode on a Local Hyperledger Fabric Network.

If you deployed your chaincode manually, instead of using Blockchain App Builder, you must call the init function manually before you test your chaincode.

You can use the same ochain invoke and ochain query commands to perform all method transactions on a remote Oracle Blockchain Platform Cloud or Enterprise Edition network; everything supported on the local network is also supported on the remote network. Pass the URL of the remote Oracle Blockchain Platform instance (-r), user name (-u) and password (-s) options to the command.

Example

ochain invoke createSupplier 
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid 
supplier","expiryDate":"2020-05-30","active":true}' -r 
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/ 
-u idcqa -s password

Testing Token Projects on a Remote Oracle Blockchain Platform Network

You can test chaincode projects that work with tokens by using Blockchain App Builder, the Oracle Blockchain Platform REST proxy, or the Hyperledger Fabric SDK.

Blockchain App Builder

You can use the Blockchain App Builder CLI to invoke transactions with multiple users to test token chaincodes.

To test with multiple users, change the authorization parameters (user name and password options) in the invoke and query commands.

Oracle Blockchain Platform REST Proxy

You can use the REST proxy in Oracle Blockchain Platform to run your token chaincode on a remote Oracle Blockchain Platform network. Use any REST Proxy client, such as Postman REST Client, to test your chaincode project.

To test multiple users, change the authorization parameters (user name and password) in your REST client, or connect to a different instance of Oracle Blockchain Platform.

Execute Berkeley DB SQL Rich Queries

If you chose to create the executeQuery method during your chaincode development, you can run SQL rich queries if your chaincode is deployed to an Oracle Blockchain Platform network.

If you have used executeQuery in the customMethods section of the specification file, a corresponding executeQuery method will be created in the controller.

Specification file:
customMethods:
    - executeQuery
    - "fetchRawMaterial(supplierid: string, rawMaterialSupply: number)"
    - "getRawMaterialFromSupplier(manufacturerId: string, supplierld: string, rawMaterialSupply: number)"
    - "createProducts(manufacturerId: string, rawMaterialConsumed: number, productsCreated: number)"
    - "sendProductsToDistribution()"
Controller file:
**
*
* BDB sql rich queries can be executed in OBP CS/EE.
* This method can be invoked only when connected to remote OBP CS/EE network.
*
*/
@Validator(yup.string())
public async executeQuery(query: string) {
    const result = await this.query(query);
    return result;
}

You can invoke this method to execute Berkeley DB SQL rich queries on Oracle Blockchain Platform network, ensuring that you use the -r, -u and -s options to specify the remote Oracle Blockchain Platform instance URL, user name and password respectively.

Example query for TypeScript:
ochain query  executeQuery "SELECT key, valueJson FROM <STATE> WHERE 
json_extract(valueJson, '$.rawMaterialAvailable') = 4" -r 
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/ 
-u idcqa -s password
Example query for Go:
ochain query  executeQuery "SELECT key, valueJson FROM <STATE> WHERE 
json_extract(valueJson, \"$.rawMaterialAvailable\") = 4" -r 
[https://%3cblockchain-instance-url%3e:7443/]https://<blockchain-instance-url>:7443/ 
-u idcqa -s password

The entire SQL query is taken in the argument, so you can make changes to your query on the fly. The syntax is different for TypeScript and Go chaincode. As shown in the previous example, the Go query uses double quotation marks for the query parameters instead of single quotation marks. The double quotation marks must be preceded by backslash characters.