CBA Kotlin Scripts - Testing & Debugging

This guide will show how to add unit tests and debug CBA Kotlin scripts.

Add pom.xml to your CBA

.
├── Definitions │   └── definition.json ├── Scripts │   └── kotlin │   └── Script.kt ├── TOSCA-Metadata │   └── TOSCA.meta ├── pom.xml


pom.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- ~ Copyright © 2020 Bell Canada ~ ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onap.ccsdk.cds.components.cba</groupId> <artifactId>test-blueprint-kotlin-parent</artifactId> <version>1.1.0-SNAPSHOT</version> </parent> <artifactId>test-kotlin-script</artifactId> <version>1.1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <!-- Properties for -Pdeploy-cba --> <cds.username>ccsdkapps</cds.username> <cds.password>ccsdkapps</cds.password> <!-- DEFAULTS <cds.protocol>http</cds.protocol> <cds.host>localhost</cds.host> <cds.port>8081</cds.port> <cds.enrich.endpoint>api/v1/blueprint-model/enrich</cds.enrich.endpoint> <cds.publish.endpoint>api/v1/blueprint-model/publish</cds.publish.endpoint> --> </properties> </project>



Add the above pom.xml to your CBA and change artifactId to your-cba-name.

The pom references a parent which defines all test dependencies. The parent also includes a profile (-Pdeploy-cba) which will enrich and publish the CBA to a running CDS instance.

The deploy-cba profile can be configured by setting properties described in the example pom.

Run tests with maven
mvn clean test



Build zip file
# This will build /target/your-cba-name-1.0.0-SNAPSHOT-cba.zip mvn clean install



Deploy cba with maven



Add Unit tests

. ├── Definitions │ └── definition.json ├── Scripts │ └── kotlin │ └── Script.kt ├── TOSCA-Metadata │ └── TOSCA.meta ├── Tests │ └── kotlin │ └── ScriptTest.kt ├── pom.xml

Kotlin unit tests should be added under /Tests/kotlin as show above. Test files must contain "Test" in the filename, for example ScriptTest.kt or TestScript.kt

Example project:

test-kotlin-script.zip



Libraries:

https://junit.org/junit4/

https://mockk.io/



Debugging

After the pom is added, the CBA can be used in IntelliJ as a maven project.

Create a new empty project



Give it a name



Click the + symbol and select import module



Select the pom.xml in your CBA directory





You should now be able to write, run and debug unit tests



CDS Runtime debuging



Open File → Project Structure



Click the + symbol and select Import Module



Select pom.xml in the CDS root directory

You should now have both your CBA and CDS as modules in the project





Make sure your cds run configuration is setup properly and then start CDS in debug mode



Right-click your cba package and select Open In Terminal



Make sure your cds properties are correct in your CBA pom.xml, then run the below command in the terminal



Maven will enrich and deploy your CBA to CDS



You should now be able to place breakpoints in your script

Send your request to CDS and start debugging