Merge "Merge live repo with design changes ccsdk-2309"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / utils / ResourceAssignmentUtilsTest.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * Modifications Copyright (c) 2019 IBM, Bell Canada.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils
23
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.node.TextNode
26 import io.mockk.every
27 import io.mockk.spyk
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Before
30 import org.junit.Test
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
36 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
43 import kotlin.test.assertEquals
44
45 data class IpAddress(val port: String, val ip: String)
46 data class Host(val name: String, val ipAddress: IpAddress)
47 data class ExpectedResponseIp(val ip: String)
48 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
49
50 class ResourceAssignmentUtilsTest {
51     private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
52     private lateinit var resourceAssignment: ResourceAssignment
53
54     private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
55     private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
56     private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
57     private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
58     private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
59     private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
60     private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
61     private lateinit var expectedValueToTestPrimitiveType: JsonNode
62     private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
63     private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
64     private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
65     private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
66     private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
67
68     @Before
69     fun setup() {
70
71         val bluePrintContext = runBlocking {
72             BluePrintMetadataUtils.getBluePrintContext(
73                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
74             )
75         }
76
77         resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
78
79         // Init input map and expected values for tests
80         initInputMapAndExpectedValuesForPrimitiveType()
81         initInputMapAndExpectedValuesForCollection()
82         initInputMapAndExpectedValuesForComplexType()
83
84         val propertiesDefinition1 = PropertyDefinition().apply {
85             type = "string"
86             id = "port"
87         }
88
89         val propertiesDefinition2 = PropertyDefinition().apply {
90             type = "string"
91             id = "ip"
92         }
93
94         val propertiesDefinition3 = PropertyDefinition().apply {
95             type = "string"
96             id = "name"
97         }
98
99         val propertiesDefinition4 = PropertyDefinition().apply {
100             type = "ip-address"
101             id = "ipAddress"
102         }
103
104         val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
105         mapOfPropertiesIpAddress["port"] = propertiesDefinition1
106         mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
107
108         val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
109         mapOfPropertiesHost["name"] = propertiesDefinition3
110         mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
111
112         val myDataTypeIpaddress = DataType().apply {
113             id = "ip-address"
114             properties = mapOfPropertiesIpAddress
115         }
116
117         val myDataTypeHost = DataType().apply {
118             id = "host"
119             properties = mapOfPropertiesHost
120         }
121
122         every {
123             resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
124         } returns myDataTypeIpaddress
125
126         every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
127
128         every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
129     }
130
131     @Test
132     fun `generateResourceDataForAssignments - positive test`() {
133         // given a valid resource assignment
134         val validResourceAssignment = createResourceAssignmentForTest("valid_value")
135
136         // and a list containing that resource assignment
137         val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
138
139         // when the values of the resources are evaluated
140         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
141
142         // then the assignment should produce a valid result
143         val expected = "{\n" + "  \"pnf-id\" : \"valid_value\"\n" + "}"
144         assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
145     }
146
147     @Test
148     fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
149         // given a valid resource assignment
150         val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
151
152         // and a list containing that resource assignment
153         val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
154
155         // when the values of the resources are evaluated
156         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
157
158         // then the assignment should produce a valid result
159         val expected = "{\n" + "  \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
160         assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
161     }
162
163     @Test
164     fun generate() {
165         val resourceAssignment = createResourceAssignmentForTest(null)
166         val resourceDefinition = ResourceDefinition()
167         val nodeTemplate = NodeTemplate().apply {
168             properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
169         }
170         resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
171         resourceDefinition.property = PropertyDefinition().apply {
172             this.description = "pnf-id"
173             this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
174         }
175
176         val result = ResourceAssignmentUtils.generateResolutionSummaryData(
177                 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition))
178
179         assertEquals("""
180             {
181                 "resolution-summary":[
182                     {
183                         "name":"pnf-id",
184                         "value":"",
185                         "required":false,
186                         "type":"string",
187                         "key-identifiers":[],
188                         "dictionary-description":"pnf-id",
189                         "dictionary-metadata":[
190                             {"name":"aai-path","value":"//path/in/aai"}
191                         ],
192                         "dictionary-name":"pnf-id",
193                         "dictionary-source":"input",
194                         "request-payload":{"mock":true},
195                         "status":"",
196                         "message":""
197                     }
198                 ]
199             }
200         """.replace("\n|\\s".toRegex(), ""), result)
201     }
202
203     private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
204         val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
205         val resourceAssignmentForTest = ResourceAssignment().apply {
206             name = "pnf-id"
207             dictionaryName = "pnf-id"
208             dictionarySource = "input"
209             property = PropertyDefinition().apply {
210                 type = "string"
211                 value = valueForTest
212             }
213         }
214         return resourceAssignmentForTest
215     }
216
217     @Test
218     fun parseResponseNodeTestForPrimitivesTypes() {
219         var outcome = prepareResponseNodeForTest(
220             "sample-value", "string", "",
221             inputMapToTestPrimitiveTypeWithValue
222         )
223         assertEquals(
224             expectedValueToTestPrimitiveType,
225             outcome,
226             "Unexpected outcome returned for primitive type of simple String"
227         )
228         assertEquals(0, resourceAssignment.keyIdentifiers.size)
229
230         outcome = prepareResponseNodeForTest(
231             "sample-key-value", "string", "",
232             inputMapToTestPrimitiveTypeWithKeyValue
233         )
234         assertEquals(
235             expectedValueToTestPrimitiveType,
236             outcome,
237             "Unexpected outcome returned for primitive type of key-value String"
238         )
239         assertEquals(
240                 expectedValueToTestPrimitiveType,
241                 resourceAssignment.keyIdentifiers[0].value
242         )
243     }
244
245     @Test
246     fun parseResponseNodeTestForCollectionsOfString() {
247         var outcome = prepareResponseNodeForTest(
248             "listOfString", "list",
249             "string", inputMapToTestCollectionOfPrimitiveType
250         )
251         assertEquals(
252             expectedValueToTesCollectionOfPrimitiveType,
253             outcome,
254             "unexpected outcome returned for list of String"
255         )
256
257         val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
258         assertEquals(
259                 expectedKeyIdentifierValue,
260                 resourceAssignment.keyIdentifiers[0].value
261         )
262
263         // FIXME("Map is not collection type, It is known complex type")
264         // outcome = prepareResponseNodeForTest(
265         //     "mapOfString", "map", "string",
266         //     inputMapToTestCollectionOfPrimitiveType
267         // )
268         // assertEquals(
269         //     expectedValueToTesCollectionOfPrimitiveType,
270         //     outcome,
271         //     "unexpected outcome returned for map of String"
272         // )
273     }
274
275     @Test
276     fun parseResponseNodeTestForCollectionsOfComplexType() {
277         var outcome = prepareResponseNodeForTest(
278             "listOfMyDataTypeWithOneOutputKeyMapping", "list",
279             "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
280         )
281         assertEquals(
282             expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
283             outcome,
284             "unexpected outcome returned for list of String"
285         )
286
287         outcome = prepareResponseNodeForTest(
288             "listOfMyDataTypeWithAllOutputKeyMapping", "list",
289             "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
290         )
291         assertEquals(
292             expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
293             outcome,
294             "unexpected outcome returned for list of String"
295         )
296     }
297
298     @Test
299     fun `parseResponseNodeTestForComplexType find one output key mapping`() {
300         val outcome = prepareResponseNodeForTest(
301             "complexTypeOneKeys", "host",
302             "", inputMapToTestComplexTypeWithOneOutputKeyMapping
303         )
304         assertEquals(
305             expectedValueToTestComplexTypeWithOneOutputKeyMapping,
306             outcome,
307             "Unexpected outcome returned for complex type"
308         )
309         assertEquals(
310                 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
311                 resourceAssignment.keyIdentifiers[0].value)
312     }
313
314     @Test
315     fun `parseResponseNodeTestForComplexType find all output key mapping`() {
316         val outcome = prepareResponseNodeForTest(
317             "complexTypeAllKeys", "host",
318             "", inputMapToTestComplexTypeWithAllOutputKeyMapping
319         )
320         assertEquals(
321             expectedValueToTestComplexTypeWithAllOutputKeyMapping,
322             outcome,
323             "Unexpected outcome returned for complex type"
324         )
325         assertEquals(2, resourceAssignment.keyIdentifiers.size)
326         assertEquals(
327                 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
328                 resourceAssignment.keyIdentifiers[0].value
329         )
330
331         assertEquals(
332                 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
333                 resourceAssignment.keyIdentifiers[1].value
334         )
335     }
336
337     @Test
338     fun `transform resolved value with inline template`() {
339         resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
340         resourceAssignment = ResourceAssignment()
341         resourceAssignment.name = "int_pktgen_private_net_id"
342         resourceAssignment.property = PropertyDefinition()
343         resourceAssignment.property!!.type = "string"
344         val value = "".asJsonType()
345
346         // Enable transform template
347         resourceAssignment.property!!.metadata =
348                 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
349
350         ResourceAssignmentUtils
351                 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
352
353         assertEquals("abc-vnf_private2",
354                 resourceAssignment.property!!.value!!.asText())
355     }
356
357     private fun initInputMapAndExpectedValuesForPrimitiveType() {
358         inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
359         val keyValue = mutableMapOf<String, String>()
360         keyValue["value"] = "1.2.3.1"
361         inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
362         expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
363     }
364
365     private fun initInputMapAndExpectedValuesForCollection() {
366         val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
367         val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
368         listOfIps.forEach {
369             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
370             arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
371             arrayNodeForList1.add(arrayChildNode)
372         }
373         inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
374
375         expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
376             ExpectedResponseIp("1.2.3.1"),
377             ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
378         ).asJsonType()
379
380         val listOfIpAddresses = arrayListOf(
381             IpAddress("1111", "1.2.3.1").asJsonType(),
382             IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
383         )
384         val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
385         listOfIpAddresses.forEach {
386             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
387             arrayChildNode.set<JsonNode>("value", it.asJsonType())
388             arrayNodeForList2.add(arrayChildNode)
389         }
390         inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
391
392         val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
393         var childNode = JacksonUtils.objectMapper.createObjectNode()
394         childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
395         childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
396         arrayNodeForList3.add(childNode)
397         childNode = JacksonUtils.objectMapper.createObjectNode()
398         childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
399         childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
400         arrayNodeForList3.add(childNode)
401         childNode = JacksonUtils.objectMapper.createObjectNode()
402         childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
403         childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
404         arrayNodeForList3.add(childNode)
405         inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
406
407         expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
408             ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
409             ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
410                 IpAddress("3333", "1.2.3.3")
411             )
412         ).asJsonType()
413         expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
414             IpAddress("1111", "1.2.3.1"),
415             IpAddress("2222", "1.2.3.2"),
416             IpAddress("3333", "1.2.3.3")
417         ).asJsonType()
418     }
419
420     private fun initInputMapAndExpectedValuesForComplexType() {
421         val mapOfComplexType = mutableMapOf<String, JsonNode>()
422         mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
423         mapOfComplexType["port"] = "8888".asJsonType()
424         mapOfComplexType["something"] = "1.2.3.2".asJsonType()
425         inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
426
427         val objectNode = JacksonUtils.objectMapper.createObjectNode()
428         expectedValueToTestComplexTypeWithOneOutputKeyMapping =
429             objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
430
431         val childNode1 = JacksonUtils.objectMapper.createObjectNode()
432         childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
433         childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
434         childNode1.set<JsonNode>("port", "8888".asJsonType())
435         childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
436         inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
437
438         val childNode2 = JacksonUtils.objectMapper.createObjectNode()
439         childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
440         childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
441         expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
442     }
443
444     private fun prepareResponseNodeForTest(
445         dictionary_source: String,
446         sourceType: String,
447         entrySchema: String,
448         response: Any
449     ): JsonNode {
450
451         resourceAssignment = when (sourceType) {
452             "list" -> {
453                 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
454             }
455             "string" -> {
456                 prepareRADataDictionaryOfPrimaryType(dictionary_source)
457             }
458             else -> {
459                 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
460             }
461         }
462
463         val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
464             "Failed to get database query result into Json node."
465         }
466
467         val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
468
469         return ResourceAssignmentUtils.parseResponseNode(
470             responseNode,
471             resourceAssignment,
472             resourceAssignmentRuntimeService,
473             outputKeyMapping
474         )
475     }
476
477     private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
478         return ResourceAssignment().apply {
479             name = "ipAddress"
480             dictionaryName = "sample-ip"
481             dictionarySource = "$dictionary_source"
482             property = PropertyDefinition().apply {
483                 type = "string"
484             }
485         }
486     }
487
488     private fun prepareRADataDictionaryCollection(
489         dictionary_source: String,
490         sourceType: String,
491         schema: String
492     ): ResourceAssignment {
493         return ResourceAssignment().apply {
494             name = "ipAddress-list"
495             dictionaryName = "sample-licenses"
496             dictionarySource = "$dictionary_source"
497             property = PropertyDefinition().apply {
498                 type = "$sourceType"
499                 entrySchema = EntrySchema().apply {
500                     type = "$schema"
501                 }
502             }
503         }
504     }
505
506     private fun prepareRADataDictionaryComplexType(
507         dictionary_source: String,
508         sourceType: String,
509         schema: String
510     ): ResourceAssignment {
511         return ResourceAssignment().apply {
512             name = "ipAddress-complexType"
513             dictionaryName = "sample-licenses"
514             dictionarySource = "$dictionary_source"
515             property = PropertyDefinition().apply {
516                 type = "$sourceType"
517             }
518         }
519     }
520
521     private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
522         val outputMapping = mutableMapOf<String, String>()
523
524         when (dictionary_source) {
525             "sample-key-value", "sample-value" -> {
526                 // Primary Type
527                 if (dictionary_source == "sample-key-value")
528                     outputMapping["sample-ip"] = "value"
529             }
530             "listOfString", "mapOfString" -> {
531                 // List of string
532                 outputMapping["ip"] = "value"
533             }
534             "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
535                 // List or map of complex Type
536                 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
537                     outputMapping["ipAddress"] = "value"
538                 else {
539                     outputMapping["port"] = "port"
540                     outputMapping["ip"] = "ip"
541                 }
542             }
543             else -> {
544                 // Complex Type
545                 if (dictionary_source == "complexTypeOneKeys")
546                     outputMapping["host"] = "value"
547                 else {
548                     outputMapping["name"] = "name"
549                     outputMapping["ipAddress"] = "ipAddress"
550                 }
551             }
552         }
553         return outputMapping
554     }
555 }