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
# This will build /target/your-cba-name-1.0.0-SNAPSHOT-cba.zip 
# Enrich CBA and add /target/your-cba-name-1.0.0-SNAPSHOT-enriched-cba.zip
# Publish CBA to CDS 
mvn clean install -Pdeploy-cba


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

mvn clean install -Pdeploy-cba


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





























  • No labels

5 Comments

  1. Hi,

    I am trying to build, mvn clean test, the example provided here but it fails and it looks like cba-parent:pom:1.0.0-SNAPSHOT is not found and it is referred from test-blueprint-kotlin-parent:1.0.0-SNAPSHOT. 

    BR
    Joachim


  2. Hi again,


    Now I see that the attached example is wrong as the groupId of parent is wrong. It is correct in the pom.xml example on wiki. After building CDS I could build the project.

    BR

    Joachim


    1. Thanks Joachim Blixt (Samsung) 
      I have updated the example

  3. currently mvn test is broken with version 1.1.0-SNAPSHOT. A fix has been submitted
    https://gerrit.onap.org/r/c/ccsdk/cds/+/118189

  4. Hi again,

    As I am really new in this area I was trying to follow this example and struggled a lot to get the enrichment to pass. I used debugger and so on and 
    the main problem was that the node types was not found. I found via debugger that the only way they seems to be loaded was via some bootstrap call, that was not called at all. I then saw some example of curl call when I googled. Maybe this is documented in some other pages or part of other pages but for me it was hard to really know this extra manual step.

    I used this (after CDS startup and before mvn clean install -Pdeploy-cba):

    curl --location --request POST 'http://localhost:8081/api/v1/blueprint-model/bootstrap' --header 'Content-Type: application/json' --header 'Authorization: Basic Y2NzZGthcHBzOmNjc2RrYXBwcw==' --data-raw '{

    "loadModelType" : true,

    "loadResourceDictionary" : true,

    "loadCBA" : false

    }'

    And all woorked fine!

    BR
    Joachim