Test Your Chaincode Using Visual Studio Code

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

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

Blockchain App Builder contains a built-in wizard to assist you with invoking or querying your chaincode.

  1. Select your chaincode project in the Chaincodes pane. In the Chaincode Details pane, select Execute. The chaincode name is already selected. Ensure the target environment is set to Local Environment, so that the channel defaults to the only channel available.
  2. In the Function field, select a method from the drop-down list. Every method available in the chaincode is listed.
  3. In the Function Param field, click More Actions button. This launches a window showing available properties for your selected method. Enter the properties, and click Omit for any non-mandatory property that you don't want submitted when you call the method. Click Save.
  4. Click Invoke.

The Output console window will show that the function has been called. Alternately, in the Chaincode Actions pane, the Function Output window shows the output. Click More Actions to see this output formatted.

To save the method and parameters that you just ran, click Save and enter a name and description. The function is saved in your chaincode project in the Queries folder. To use it again, right-click it and select Open.

If you make any changes to the controller file that alter the methods, select the Reload icon at the top of the Chaincode Execute pane. The change will then be reflected in the Function drop-down list.

Note:

If you don't want to use the wizard for testing, you can also run the Blockchain App Builder command line tools in the Visual Studio Code Terminal window. For more information on testing from the command line, see Test Your Chaincode on a Local Hyperledger Fabric Network.

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 and save your chaincode, the changes will be compiled, installed and deployed automatically. There is no need to take down or bring up the local network again. All projects will be automatically compiled and deployed for every change.

Testing Lifecycle Operations 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.

You test chaincode running on a remote Oracle Blockchain Platform network similar to testing it on a local network, as described in Test Your Chaincode on a Local Hyperledger Fabric Network.

You can use the same commands to complete 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. Select the Oracle Blockchain Platform instance as your target environment when running your tests.

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 Visual Studio Code extension to call transactions with multiple users to test token chaincodes. To test with multiple users, change the authorization parameters (user name and password) in the Environments tab and then save the environment. While invoking transactions, select the same environment from the drop-down list and then run the transaction.

Oracle Blockchain Platform REST Proxy

You can use the REST proxy in Oracle Blockchain Platform to run your a 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 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 call this method to run Berkeley DB SQL rich queries on the Oracle Blockchain Platform network. You must select the Oracle Blockchain Platform environment that you created as your target environment when running the queries.

Example:
  1. In the Chaincode Details pane, select Execute. The chaincode name, target environment, and channel are already specified from the deployment step.
  2. In the Function Name field, select executeQuery from the drop-down list.
  3. In the Function Param field, click More Actions. This launches a window where you can enter the query string. Enter the arguments for your query, and then click Save.
  4. Click Query.
The Output window opens and shows the query being run, and the result.
ochain query executeQuery "SELECT key, valueJson FROM <STATE> WHERE
json_extract(valueJson, '$.rawMaterialAvailable') = 4" 

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

Generate CLI Commands from Queries

If you have saved queries in a chaincode project in Visual Studio Code, you can automatically generate the equivalent CLI commands.

You must have at least one saved query in a chaincode project to generate CLI commands for macOS and Linux and for Microsoft Windows.

  1. Expand the project in the Chaincodes pane.
  2. Right-click Queries.
  3. Click Generate CLI Commands.

Two text files are generated and displayed in the Queries section of the Chaincodes pane: CLIcommandsForLinux.txt and CLIcommandsForWindows.txt. Select the file name to open the file and show the corresponding CLI commands.