Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / utils / ResourceAssignmentUtilsTest.kt
index 7631339..2f07e3b 100644 (file)
@@ -25,17 +25,21 @@ import com.fasterxml.jackson.databind.JsonNode
 import com.fasterxml.jackson.databind.node.TextNode
 import io.mockk.every
 import io.mockk.spyk
+import kotlinx.coroutines.runBlocking
 import org.junit.Before
 import org.junit.Test
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
+import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
 import kotlin.test.assertEquals
 
 data class IpAddress(val port: String, val ip: String)
@@ -44,7 +48,9 @@ data class ExpectedResponseIp(val ip: String)
 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
 
 class ResourceAssignmentUtilsTest {
+
     private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
+    private lateinit var resourceAssignment: ResourceAssignment
 
     private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
     private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
@@ -63,9 +69,11 @@ class ResourceAssignmentUtilsTest {
     @Before
     fun setup() {
 
-        val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
-            "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
-        )
+        val bluePrintContext = runBlocking {
+            BluePrintMetadataUtils.getBluePrintContext(
+                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
+            )
+        }
 
         resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
 
@@ -94,11 +102,11 @@ class ResourceAssignmentUtilsTest {
             id = "ipAddress"
         }
 
-        var mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
+        val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
         mapOfPropertiesIpAddress["port"] = propertiesDefinition1
         mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
 
-        var mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
+        val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
         mapOfPropertiesHost["name"] = propertiesDefinition3
         mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
 
@@ -112,7 +120,9 @@ class ResourceAssignmentUtilsTest {
             properties = mapOfPropertiesHost
         }
 
-        every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address") } returns myDataTypeIpaddress
+        every {
+            resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
+        } returns myDataTypeIpaddress
 
         every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
 
@@ -122,17 +132,23 @@ class ResourceAssignmentUtilsTest {
     @Test
     fun `generateResourceDataForAssignments - positive test`() {
         // given a valid resource assignment
-        val validResourceAssignment = createResourceAssignmentForTest("valid_value")
+        val validResourceAssignment1 = createResourceAssignmentForTest("valid_value", "pnf-id")
+        val validResourceAssignment2 = createResourceAssignmentForTest("also_valid", "a1")
 
         // and a list containing that resource assignment
-        val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
+        val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment1, validResourceAssignment2)
 
         // when the values of the resources are evaluated
         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
 
         // then the assignment should produce a valid result
-        val expected = "{\n" + "  \"pnf-id\" : \"valid_value\"\n" + "}"
-        assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
+        val expected = """
+            {
+              "a1" : "also_valid",
+              "pnf-id" : "valid_value"
+            }
+        """.trimIndent()
+        assertEquals(expected, outcome.trimIndent(), "unexpected outcome generated")
     }
 
     @Test
@@ -151,10 +167,54 @@ class ResourceAssignmentUtilsTest {
         assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
     }
 
-    private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
+    @Test
+    fun generateResolutionSummaryDataTest() {
+        val resourceAssignment = createResourceAssignmentForTest(null)
+        val resourceDefinition = ResourceDefinition()
+        val nodeTemplate = NodeTemplate().apply {
+            properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
+        }
+        resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
+        resourceDefinition.property = PropertyDefinition().apply {
+            this.description = "pnf-id"
+            this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
+        }
+
+        val result = ResourceAssignmentUtils.generateResolutionSummaryData(
+            listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition)
+        )
+
+        assertEquals(
+            """
+            {
+                "resolution-summary":[
+                    {
+                        "name":"pnf-id",
+                        "value":"",
+                        "required":false,
+                        "type":"string",
+                        "key-identifiers":[],
+                        "dictionary-description":"pnf-id",
+                        "dictionary-metadata":[
+                            {"name":"aai-path","value":"//path/in/aai"}
+                        ],
+                        "dictionary-name":"pnf-id",
+                        "dictionary-source":"input",
+                        "request-payload":{"mock":true},
+                        "status":"",
+                        "message":""
+                    }
+                ]
+            }
+        """.replace("\n|\\s".toRegex(), ""),
+            result
+        )
+    }
+
+    private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
         val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
         val resourceAssignmentForTest = ResourceAssignment().apply {
-            name = "pnf-id"
+            name = resourceName
             dictionaryName = "pnf-id"
             dictionarySource = "input"
             property = PropertyDefinition().apply {
@@ -171,13 +231,26 @@ class ResourceAssignmentUtilsTest {
             "sample-value", "string", "",
             inputMapToTestPrimitiveTypeWithValue
         )
-        assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of simple String")
+        assertEquals(
+            expectedValueToTestPrimitiveType,
+            outcome,
+            "Unexpected outcome returned for primitive type of simple String"
+        )
+        assertEquals(0, resourceAssignment.keyIdentifiers.size)
 
         outcome = prepareResponseNodeForTest(
             "sample-key-value", "string", "",
             inputMapToTestPrimitiveTypeWithKeyValue
         )
-        assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of key-value String")
+        assertEquals(
+            expectedValueToTestPrimitiveType,
+            outcome,
+            "Unexpected outcome returned for primitive type of key-value String"
+        )
+        assertEquals(
+            expectedValueToTestPrimitiveType,
+            resourceAssignment.keyIdentifiers[0].value
+        )
     }
 
     @Test
@@ -186,13 +259,28 @@ class ResourceAssignmentUtilsTest {
             "listOfString", "list",
             "string", inputMapToTestCollectionOfPrimitiveType
         )
-        assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for list of String")
+        assertEquals(
+            expectedValueToTesCollectionOfPrimitiveType,
+            outcome,
+            "unexpected outcome returned for list of String"
+        )
 
-        outcome = prepareResponseNodeForTest(
-            "mapOfString", "map", "string",
-            inputMapToTestCollectionOfPrimitiveType
+        val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
+        assertEquals(
+            expectedKeyIdentifierValue,
+            resourceAssignment.keyIdentifiers[0].value
         )
-        assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for map of String")
+
+        // FIXME("Map is not collection type, It is known complex type")
+        // outcome = prepareResponseNodeForTest(
+        //     "mapOfString", "map", "string",
+        //     inputMapToTestCollectionOfPrimitiveType
+        // )
+        // assertEquals(
+        //     expectedValueToTesCollectionOfPrimitiveType,
+        //     outcome,
+        //     "unexpected outcome returned for map of String"
+        // )
     }
 
     @Test
@@ -201,13 +289,21 @@ class ResourceAssignmentUtilsTest {
             "listOfMyDataTypeWithOneOutputKeyMapping", "list",
             "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
         )
-        assertEquals(expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
+        assertEquals(
+            expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
+            outcome,
+            "unexpected outcome returned for list of String"
+        )
 
         outcome = prepareResponseNodeForTest(
             "listOfMyDataTypeWithAllOutputKeyMapping", "list",
             "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
         )
-        assertEquals(expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
+        assertEquals(
+            expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
+            outcome,
+            "unexpected outcome returned for list of String"
+        )
     }
 
     @Test
@@ -216,7 +312,15 @@ class ResourceAssignmentUtilsTest {
             "complexTypeOneKeys", "host",
             "", inputMapToTestComplexTypeWithOneOutputKeyMapping
         )
-        assertEquals(expectedValueToTestComplexTypeWithOneOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
+        assertEquals(
+            expectedValueToTestComplexTypeWithOneOutputKeyMapping,
+            outcome,
+            "Unexpected outcome returned for complex type"
+        )
+        assertEquals(
+            expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
+            resourceAssignment.keyIdentifiers[0].value
+        )
     }
 
     @Test
@@ -225,7 +329,43 @@ class ResourceAssignmentUtilsTest {
             "complexTypeAllKeys", "host",
             "", inputMapToTestComplexTypeWithAllOutputKeyMapping
         )
-        assertEquals(expectedValueToTestComplexTypeWithAllOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
+        assertEquals(
+            expectedValueToTestComplexTypeWithAllOutputKeyMapping,
+            outcome,
+            "Unexpected outcome returned for complex type"
+        )
+        assertEquals(2, resourceAssignment.keyIdentifiers.size)
+        assertEquals(
+            expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
+            resourceAssignment.keyIdentifiers[0].value
+        )
+
+        assertEquals(
+            expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
+            resourceAssignment.keyIdentifiers[1].value
+        )
+    }
+
+    @Test
+    fun `transform resolved value with inline template`() {
+        resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
+        resourceAssignment = ResourceAssignment()
+        resourceAssignment.name = "int_pktgen_private_net_id"
+        resourceAssignment.property = PropertyDefinition()
+        resourceAssignment.property!!.type = "string"
+        val value = "".asJsonType()
+
+        // Enable transform template
+        resourceAssignment.property!!.metadata =
+            mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
+
+        ResourceAssignmentUtils
+            .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
+
+        assertEquals(
+            "abc-vnf_private2",
+            resourceAssignment.property!!.value!!.asText()
+        )
     }
 
     private fun initInputMapAndExpectedValuesForPrimitiveType() {
@@ -241,7 +381,7 @@ class ResourceAssignmentUtilsTest {
         val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
         listOfIps.forEach {
             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
-            arrayChildNode.set("value", it.asJsonPrimitive())
+            arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
             arrayNodeForList1.add(arrayChildNode)
         }
         inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
@@ -258,29 +398,30 @@ class ResourceAssignmentUtilsTest {
         val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
         listOfIpAddresses.forEach {
             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
-            arrayChildNode.set("value", it.asJsonType())
+            arrayChildNode.set<JsonNode>("value", it.asJsonType())
             arrayNodeForList2.add(arrayChildNode)
         }
         inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
 
         val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
         var childNode = JacksonUtils.objectMapper.createObjectNode()
-        childNode.set("port", "1111".asJsonPrimitive())
-        childNode.set("ip", "1.2.3.1".asJsonPrimitive())
+        childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
+        childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
         arrayNodeForList3.add(childNode)
         childNode = JacksonUtils.objectMapper.createObjectNode()
-        childNode.set("port", "2222".asJsonPrimitive())
-        childNode.set("ip", "1.2.3.2".asJsonPrimitive())
+        childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
+        childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
         arrayNodeForList3.add(childNode)
         childNode = JacksonUtils.objectMapper.createObjectNode()
-        childNode.set("port", "3333".asJsonPrimitive())
-        childNode.set("ip", "1.2.3.3".asJsonPrimitive())
+        childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
+        childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
         arrayNodeForList3.add(childNode)
         inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
 
         expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
             ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
-            ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
+            ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")),
+            ExpectedResponseIpAddress(
                 IpAddress("3333", "1.2.3.3")
             )
         ).asJsonType()
@@ -303,15 +444,15 @@ class ResourceAssignmentUtilsTest {
             objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
 
         val childNode1 = JacksonUtils.objectMapper.createObjectNode()
-        childNode1.set("name", "my-ipAddress".asJsonPrimitive())
-        childNode1.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
-        childNode1.set("port", "8888".asJsonType())
-        childNode1.set("something", IpAddress("2222", "1.2.3.1").asJsonType())
+        childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
+        childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
+        childNode1.set<JsonNode>("port", "8888".asJsonType())
+        childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
         inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
 
         val childNode2 = JacksonUtils.objectMapper.createObjectNode()
-        childNode2.set("name", "my-ipAddress".asJsonPrimitive())
-        childNode2.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
+        childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
+        childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
         expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
     }
 
@@ -322,8 +463,8 @@ class ResourceAssignmentUtilsTest {
         response: Any
     ): JsonNode {
 
-        val resourceAssignment = when (sourceType) {
-            "list", "map" -> {
+        resourceAssignment = when (sourceType) {
+            "list" -> {
                 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
             }
             "string" -> {
@@ -340,7 +481,12 @@ class ResourceAssignmentUtilsTest {
 
         val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
 
-        return ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment, resourceAssignmentRuntimeService, outputKeyMapping)
+        return ResourceAssignmentUtils.parseResponseNode(
+            responseNode,
+            resourceAssignment,
+            resourceAssignmentRuntimeService,
+            outputKeyMapping
+        )
     }
 
     private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
@@ -354,7 +500,11 @@ class ResourceAssignmentUtilsTest {
         }
     }
 
-    private fun prepareRADataDictionaryCollection(dictionary_source: String, sourceType: String, schema: String): ResourceAssignment {
+    private fun prepareRADataDictionaryCollection(
+        dictionary_source: String,
+        sourceType: String,
+        schema: String
+    ): ResourceAssignment {
         return ResourceAssignment().apply {
             name = "ipAddress-list"
             dictionaryName = "sample-licenses"
@@ -368,7 +518,11 @@ class ResourceAssignmentUtilsTest {
         }
     }
 
-    private fun prepareRADataDictionaryComplexType(dictionary_source: String, sourceType: String, schema: String): ResourceAssignment {
+    private fun prepareRADataDictionaryComplexType(
+        dictionary_source: String,
+        sourceType: String,
+        schema: String
+    ): ResourceAssignment {
         return ResourceAssignment().apply {
             name = "ipAddress-complexType"
             dictionaryName = "sample-licenses"