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 instantiated, 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 array of JSON strings or a string for single parameter.

Options:
    -h, --help               output command usage information
    -D, --debug              enable debug logging
    -P, --project            (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -d, --obp-dev-package    (optional) Path to the downloaded and unzipped Oracle Blockchain Development Package. If not specified, it defaults to 'CURRENT_DIRECTORY/obp'.
    -c, --channel            (optional) Blockchain Channel to deploy chaincode too. If not specified, it defaults to the 'default' channel.
    -u, --username           (optional, if -d option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.

Examples:
$> ochain invoke <method>
without chaincode initial arguments
$> ochain invoke <method> {"manufacturerld":"m01","rawMaterialAvailable":9,"productsAvailable":4,"completionDate":-05-26-2020"}'
for a single parameter
$> ochain invoke <method> ['s01','sl0']
for multiple parameters

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 array of JSON strings or a string for single parameter.

Options:
    -h, --help               output command usage information
    -D, --debug              enable debug logging
    -P, --project            (optional) Path to Chaincode project to deploy. If not specified, it defaults to current directory.
    -d, --obp-dev-package    (optional) Path to the downloaded and unzipped Oracle Blockchain Development Package. If not specified, it defaults to 'CURRENT_DIRECTORY/obp'.
    -c, --channel            (optional) Blockchain Channel to deploy chaincode too. If not specified, it defaults to the 'default' channel.
    -u, --username           (optional, if -d option is applied) A user name that has install chaincode privileges. Contact your administrator for more details.

Examples:
$> ochain query <method>
without chaincode initial arguments
$> ochain query <method> s01
for a single parameter
$> ochain query <method> 's01','{"manufacturerId":"m01"}'
for multiple parameters
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.

Automatic Install and Instantiate After Update

Whenever you update your chaincode, the changes will be compiled, installed and instantiated 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

Once 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.

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. Simply pass the Oracle Blockchain Platform connection profile as an option (-d) to the commands.

Example

> ochain invoke createSupplier
'{"supplierId":"s01","rawMaterialAvailable":5,"license":"valid
supplier","expiryDate":"2020-05-30","active":true}' -d
/Blockchain/DevTools/bp1/oraclebp1-instance-info

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 OchainController.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 -d option to specify the location of your downloaded connection profile.

Example:
> ochain query executeQuery "SELECT key, valueJson FROM <STATE> WHERE
json_extract(valueJson, '$.rawMaterialAvailable') = 4" -d
/Blockchain/DevTools/bp1/oraclebp1-instance-info

The entire SQL query is taken in the argument, so you can make changes to your query on the fly.