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

Once 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 should already be selected. Ensure the target environment is set to Local Environment and the channel will default to the only channel available.
  2. In the Function field, select your method from the drop-down list. Every method available in the chaincode is listed.
  3. In the Function Param field, select the More Actions (…) button. This will launch a window with available properties for your selected method. Enter the properties, click Omit for any non-mandatory property you don't want submitted when you invoke your method, and click Save.
  4. Click Invoke.

The Output console window will show that the function has been invoked. Alternatively, in the Chaincode Actions pane, the Function Output window displays the output. Click the More Actions (…) button to see this output formatted.

If you want to save the method and parameters you just ran, you can click Save and enter a name and description for it. It will be 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 would alter the methods, select the Reload icon at the top of the Chaincode Execute pane. The change should now 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. Follow the instructions provided here to test with the command line: 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 strip down or bring up the local network again. All projects will be automatically compiled and deployed on every change.

Testing Lifecycle Operations 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 invoke and 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. Select the Oracle Blockchain Platform instance as your target environment when executing 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 invoke transactions with multiple user 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 execute 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 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 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 should already be pre-filled from the deployment step.
  2. In the Function Name field, select executeQuery from the drop-down list.
  3. In the Function Param field, select the More Actions (…) button. This will launch a window where you can enter the query string. Enter the arguments for your query, and click Save.
  4. Click Query.
The Output window and the will show the query being executed 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 Mac OSX 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.