Metadata for name, version, tags and type
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / utils / ResourceAssignmentUtilsTest.kt
index 728e116..a358f6a 100644 (file)
@@ -25,6 +25,7 @@ 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
@@ -63,12 +64,15 @@ 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))
 
-        //Init input map and expected values for tests
+        // Init input map and expected values for tests
         initInputMapAndExpectedValuesForPrimitiveType()
         initInputMapAndExpectedValuesForCollection()
         initInputMapAndExpectedValuesForComplexType()
@@ -93,11 +97,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
 
@@ -111,7 +115,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
 
@@ -120,35 +126,34 @@ class ResourceAssignmentUtilsTest {
 
     @Test
     fun `generateResourceDataForAssignments - positive test`() {
-        //given a valid resource assignment
+        // given a valid resource assignment
         val validResourceAssignment = createResourceAssignmentForTest("valid_value")
 
-        //and a list containing that resource assignment
+        // and a list containing that resource assignment
         val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
 
-        //when the values of the resources are evaluated
+        // when the values of the resources are evaluated
         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
 
-        //then the assignment should produce a valid result
+        // then the assignment should produce a valid result
         val expected = "{\n" + "  \"pnf-id\" : \"valid_value\"\n" + "}"
-        assertEquals(expected, outcome, "unexpected outcome generated")
+        assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
     }
 
     @Test
     fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
-        //given a valid resource assignment
+        // given a valid resource assignment
         val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
 
-        //and a list containing that resource assignment
+        // and a list containing that resource assignment
         val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
 
-        //when the values of the resources are evaluated
+        // when the values of the resources are evaluated
         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
 
-        //then the assignment should produce a valid result
+        // then the assignment should produce a valid result
         val expected = "{\n" + "  \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
-        assertEquals(expected, outcome, "unexpected outcome generated")
-
+        assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
     }
 
     private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
@@ -166,61 +171,109 @@ class ResourceAssignmentUtilsTest {
     }
 
     @Test
-    fun parseResponseNodeTestForPrimitivesTypes(){
-        var outcome = prepareResponseNodeForTest("sample-value", "string", "",
-                inputMapToTestPrimitiveTypeWithValue)
-        assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of simple String")
-
-        outcome = prepareResponseNodeForTest("sample-key-value", "string", "",
-                inputMapToTestPrimitiveTypeWithKeyValue)
-        assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of key-value String")
+    fun parseResponseNodeTestForPrimitivesTypes() {
+        var outcome = prepareResponseNodeForTest(
+            "sample-value", "string", "",
+            inputMapToTestPrimitiveTypeWithValue
+        )
+        assertEquals(
+            expectedValueToTestPrimitiveType,
+            outcome,
+            "Unexpected outcome returned for primitive type of simple String"
+        )
+
+        outcome = prepareResponseNodeForTest(
+            "sample-key-value", "string", "",
+            inputMapToTestPrimitiveTypeWithKeyValue
+        )
+        assertEquals(
+            expectedValueToTestPrimitiveType,
+            outcome,
+            "Unexpected outcome returned for primitive type of key-value String"
+        )
     }
 
     @Test
-    fun parseResponseNodeTestForCollectionsOfString(){
-        var outcome = prepareResponseNodeForTest("listOfString", "list",
-                "string", inputMapToTestCollectionOfPrimitiveType)
-        assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for list of String")
-
-        outcome = prepareResponseNodeForTest("mapOfString", "map", "string",
-                inputMapToTestCollectionOfPrimitiveType)
-        assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for map of String")
+    fun parseResponseNodeTestForCollectionsOfString() {
+        var outcome = prepareResponseNodeForTest(
+            "listOfString", "list",
+            "string", inputMapToTestCollectionOfPrimitiveType
+        )
+        assertEquals(
+            expectedValueToTesCollectionOfPrimitiveType,
+            outcome,
+            "unexpected outcome returned for list of String"
+        )
+
+        outcome = prepareResponseNodeForTest(
+            "mapOfString", "map", "string",
+            inputMapToTestCollectionOfPrimitiveType
+        )
+        assertEquals(
+            expectedValueToTesCollectionOfPrimitiveType,
+            outcome,
+            "unexpected outcome returned for map of String"
+        )
     }
 
     @Test
-    fun parseResponseNodeTestForCollectionsOfComplexType(){
-        var outcome = prepareResponseNodeForTest("listOfMyDataTypeWithOneOutputKeyMapping", "list",
-                "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping)
-        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")
+    fun parseResponseNodeTestForCollectionsOfComplexType() {
+        var outcome = prepareResponseNodeForTest(
+            "listOfMyDataTypeWithOneOutputKeyMapping", "list",
+            "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
+        )
+        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"
+        )
     }
 
     @Test
-    fun `parseResponseNodeTestForComplexType find one output key mapping`(){
-        val outcome = prepareResponseNodeForTest("complexTypeOneKeys", "host",
-                "", inputMapToTestComplexTypeWithOneOutputKeyMapping)
-        assertEquals(expectedValueToTestComplexTypeWithOneOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
+    fun `parseResponseNodeTestForComplexType find one output key mapping`() {
+        val outcome = prepareResponseNodeForTest(
+            "complexTypeOneKeys", "host",
+            "", inputMapToTestComplexTypeWithOneOutputKeyMapping
+        )
+        assertEquals(
+            expectedValueToTestComplexTypeWithOneOutputKeyMapping,
+            outcome,
+            "Unexpected outcome returned for complex type"
+        )
     }
 
     @Test
-    fun `parseResponseNodeTestForComplexType find all output key mapping`(){
-        val outcome = prepareResponseNodeForTest("complexTypeAllKeys", "host",
-                "", inputMapToTestComplexTypeWithAllOutputKeyMapping)
-        assertEquals(expectedValueToTestComplexTypeWithAllOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
+    fun `parseResponseNodeTestForComplexType find all output key mapping`() {
+        val outcome = prepareResponseNodeForTest(
+            "complexTypeAllKeys", "host",
+            "", inputMapToTestComplexTypeWithAllOutputKeyMapping
+        )
+        assertEquals(
+            expectedValueToTestComplexTypeWithAllOutputKeyMapping,
+            outcome,
+            "Unexpected outcome returned for complex type"
+        )
     }
 
     private fun initInputMapAndExpectedValuesForPrimitiveType() {
         inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
         val keyValue = mutableMapOf<String, String>()
-        keyValue["value"]= "1.2.3.1"
+        keyValue["value"] = "1.2.3.1"
         inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
         expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
     }
 
-    private fun initInputMapAndExpectedValuesForCollection(){
+    private fun initInputMapAndExpectedValuesForCollection() {
         val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
         val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
         listOfIps.forEach {
@@ -230,12 +283,15 @@ class ResourceAssignmentUtilsTest {
         }
         inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
 
-        expectedValueToTesCollectionOfPrimitiveType = arrayListOf(ExpectedResponseIp("1.2.3.1"),
-                ExpectedResponseIp( "1.2.3.2"), ExpectedResponseIp("1.2.3.3")).asJsonType()
+        expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
+            ExpectedResponseIp("1.2.3.1"),
+            ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
+        ).asJsonType()
 
-
-        val listOfIpAddresses = arrayListOf(IpAddress("1111", "1.2.3.1").asJsonType(),
-                IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType())
+        val listOfIpAddresses = arrayListOf(
+            IpAddress("1111", "1.2.3.1").asJsonType(),
+            IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
+        )
         val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
         listOfIpAddresses.forEach {
             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
@@ -259,15 +315,20 @@ class ResourceAssignmentUtilsTest {
         arrayNodeForList3.add(childNode)
         inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
 
-        expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
-                ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
-                IpAddress("3333", "1.2.3.3"))).asJsonType()
-        expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(IpAddress("1111", "1.2.3.1"),
-                IpAddress("2222", "1.2.3.2"),
-                IpAddress("3333", "1.2.3.3")).asJsonType()
+        expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
+            ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
+            ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
+                IpAddress("3333", "1.2.3.3")
+            )
+        ).asJsonType()
+        expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
+            IpAddress("1111", "1.2.3.1"),
+            IpAddress("2222", "1.2.3.2"),
+            IpAddress("3333", "1.2.3.3")
+        ).asJsonType()
     }
 
-    private fun initInputMapAndExpectedValuesForComplexType(){
+    private fun initInputMapAndExpectedValuesForComplexType() {
         val mapOfComplexType = mutableMapOf<String, JsonNode>()
         mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
         mapOfComplexType["port"] = "8888".asJsonType()
@@ -275,7 +336,8 @@ class ResourceAssignmentUtilsTest {
         inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
 
         val objectNode = JacksonUtils.objectMapper.createObjectNode()
-        expectedValueToTestComplexTypeWithOneOutputKeyMapping = objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
+        expectedValueToTestComplexTypeWithOneOutputKeyMapping =
+            objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
 
         val childNode1 = JacksonUtils.objectMapper.createObjectNode()
         childNode1.set("name", "my-ipAddress".asJsonPrimitive())
@@ -290,8 +352,12 @@ class ResourceAssignmentUtilsTest {
         expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
     }
 
-    private fun prepareResponseNodeForTest(dictionary_source: String, sourceType: String, entrySchema: String,
-                                           response: Any): JsonNode {
+    private fun prepareResponseNodeForTest(
+        dictionary_source: String,
+        sourceType: String,
+        entrySchema: String,
+        response: Any
+    ): JsonNode {
 
         val resourceAssignment = when (sourceType) {
             "list", "map" -> {
@@ -311,7 +377,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 {
@@ -325,7 +396,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"
@@ -339,7 +414,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"
@@ -355,16 +434,16 @@ class ResourceAssignmentUtilsTest {
 
         when (dictionary_source) {
             "sample-key-value", "sample-value" -> {
-                //Primary Type
-                if (dictionary_source=="sample-key-value")
+                // Primary Type
+                if (dictionary_source == "sample-key-value")
                     outputMapping["sample-ip"] = "value"
             }
             "listOfString", "mapOfString" -> {
-                //List of string
+                // List of string
                 outputMapping["ip"] = "value"
             }
             "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
-                //List or map of complex Type
+                // List or map of complex Type
                 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
                     outputMapping["ipAddress"] = "value"
                 else {
@@ -373,16 +452,15 @@ class ResourceAssignmentUtilsTest {
                 }
             }
             else -> {
-                //Complex Type
+                // Complex Type
                 if (dictionary_source == "complexTypeOneKeys")
                     outputMapping["host"] = "value"
                 else {
                     outputMapping["name"] = "name"
                     outputMapping["ipAddress"] = "ipAddress"
                 }
-
             }
         }
         return outputMapping
     }
-}
\ No newline at end of file
+}