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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.node.TextNode
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Before
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
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)
50 class ResourceAssignmentUtilsTest {
51 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
52 private lateinit var resourceAssignment: ResourceAssignment
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
71 val bluePrintContext = runBlocking {
72 BluePrintMetadataUtils.getBluePrintContext(
73 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
77 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
79 // Init input map and expected values for tests
80 initInputMapAndExpectedValuesForPrimitiveType()
81 initInputMapAndExpectedValuesForCollection()
82 initInputMapAndExpectedValuesForComplexType()
84 val propertiesDefinition1 = PropertyDefinition().apply {
89 val propertiesDefinition2 = PropertyDefinition().apply {
94 val propertiesDefinition3 = PropertyDefinition().apply {
99 val propertiesDefinition4 = PropertyDefinition().apply {
104 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
105 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
106 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
108 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
109 mapOfPropertiesHost["name"] = propertiesDefinition3
110 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
112 val myDataTypeIpaddress = DataType().apply {
114 properties = mapOfPropertiesIpAddress
117 val myDataTypeHost = DataType().apply {
119 properties = mapOfPropertiesHost
123 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
124 } returns myDataTypeIpaddress
126 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
128 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
132 fun `generateResourceDataForAssignments - positive test`() {
133 // given a valid resource assignment
134 val validResourceAssignment = createResourceAssignmentForTest("valid_value")
136 // and a list containing that resource assignment
137 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
139 // when the values of the resources are evaluated
140 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
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")
148 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
149 // given a valid resource assignment
150 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
152 // and a list containing that resource assignment
153 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
155 // when the values of the resources are evaluated
156 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
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")
165 val resourceAssignment = createResourceAssignmentForTest(null)
166 val resourceDefinition = ResourceDefinition()
167 val nodeTemplate = NodeTemplate().apply {
168 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
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")
176 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
177 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition))
181 "resolution-summary":[
187 "key-identifiers":[],
188 "dictionary-description":"pnf-id",
189 "dictionary-metadata":[
190 {"name":"aai-path","value":"//path/in/aai"}
192 "dictionary-name":"pnf-id",
193 "dictionary-source":"input",
194 "request-payload":{"mock":true},
200 """.replace("\n|\\s".toRegex(), ""), result)
203 private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
204 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
205 val resourceAssignmentForTest = ResourceAssignment().apply {
207 dictionaryName = "pnf-id"
208 dictionarySource = "input"
209 property = PropertyDefinition().apply {
214 return resourceAssignmentForTest
218 fun parseResponseNodeTestForPrimitivesTypes() {
219 var outcome = prepareResponseNodeForTest(
220 "sample-value", "string", "",
221 inputMapToTestPrimitiveTypeWithValue
224 expectedValueToTestPrimitiveType,
226 "Unexpected outcome returned for primitive type of simple String"
228 assertEquals(0, resourceAssignment.keyIdentifiers.size)
230 outcome = prepareResponseNodeForTest(
231 "sample-key-value", "string", "",
232 inputMapToTestPrimitiveTypeWithKeyValue
235 expectedValueToTestPrimitiveType,
237 "Unexpected outcome returned for primitive type of key-value String"
240 expectedValueToTestPrimitiveType,
241 resourceAssignment.keyIdentifiers[0].value
246 fun parseResponseNodeTestForCollectionsOfString() {
247 var outcome = prepareResponseNodeForTest(
248 "listOfString", "list",
249 "string", inputMapToTestCollectionOfPrimitiveType
252 expectedValueToTesCollectionOfPrimitiveType,
254 "unexpected outcome returned for list of String"
257 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
259 expectedKeyIdentifierValue,
260 resourceAssignment.keyIdentifiers[0].value
263 // FIXME("Map is not collection type, It is known complex type")
264 // outcome = prepareResponseNodeForTest(
265 // "mapOfString", "map", "string",
266 // inputMapToTestCollectionOfPrimitiveType
269 // expectedValueToTesCollectionOfPrimitiveType,
271 // "unexpected outcome returned for map of String"
276 fun parseResponseNodeTestForCollectionsOfComplexType() {
277 var outcome = prepareResponseNodeForTest(
278 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
279 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
282 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
284 "unexpected outcome returned for list of String"
287 outcome = prepareResponseNodeForTest(
288 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
289 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
292 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
294 "unexpected outcome returned for list of String"
299 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
300 val outcome = prepareResponseNodeForTest(
301 "complexTypeOneKeys", "host",
302 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
305 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
307 "Unexpected outcome returned for complex type"
310 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
311 resourceAssignment.keyIdentifiers[0].value)
315 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
316 val outcome = prepareResponseNodeForTest(
317 "complexTypeAllKeys", "host",
318 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
321 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
323 "Unexpected outcome returned for complex type"
325 assertEquals(2, resourceAssignment.keyIdentifiers.size)
327 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
328 resourceAssignment.keyIdentifiers[0].value
332 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
333 resourceAssignment.keyIdentifiers[1].value
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()
346 // Enable transform template
347 resourceAssignment.property!!.metadata =
348 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
350 ResourceAssignmentUtils
351 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
353 assertEquals("abc-vnf_private2",
354 resourceAssignment.property!!.value!!.asText())
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")
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()
369 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
370 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
371 arrayNodeForList1.add(arrayChildNode)
373 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
375 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
376 ExpectedResponseIp("1.2.3.1"),
377 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
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()
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)
390 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
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
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")
413 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
414 IpAddress("1111", "1.2.3.1"),
415 IpAddress("2222", "1.2.3.2"),
416 IpAddress("3333", "1.2.3.3")
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()
427 val objectNode = JacksonUtils.objectMapper.createObjectNode()
428 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
429 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
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
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
444 private fun prepareResponseNodeForTest(
445 dictionary_source: String,
451 resourceAssignment = when (sourceType) {
453 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
456 prepareRADataDictionaryOfPrimaryType(dictionary_source)
459 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
463 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
464 "Failed to get database query result into Json node."
467 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
469 return ResourceAssignmentUtils.parseResponseNode(
472 resourceAssignmentRuntimeService,
477 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
478 return ResourceAssignment().apply {
480 dictionaryName = "sample-ip"
481 dictionarySource = "$dictionary_source"
482 property = PropertyDefinition().apply {
488 private fun prepareRADataDictionaryCollection(
489 dictionary_source: String,
492 ): ResourceAssignment {
493 return ResourceAssignment().apply {
494 name = "ipAddress-list"
495 dictionaryName = "sample-licenses"
496 dictionarySource = "$dictionary_source"
497 property = PropertyDefinition().apply {
499 entrySchema = EntrySchema().apply {
506 private fun prepareRADataDictionaryComplexType(
507 dictionary_source: String,
510 ): ResourceAssignment {
511 return ResourceAssignment().apply {
512 name = "ipAddress-complexType"
513 dictionaryName = "sample-licenses"
514 dictionarySource = "$dictionary_source"
515 property = PropertyDefinition().apply {
521 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
522 val outputMapping = mutableMapOf<String, String>()
524 when (dictionary_source) {
525 "sample-key-value", "sample-value" -> {
527 if (dictionary_source == "sample-key-value")
528 outputMapping["sample-ip"] = "value"
530 "listOfString", "mapOfString" -> {
532 outputMapping["ip"] = "value"
534 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
535 // List or map of complex Type
536 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
537 outputMapping["ipAddress"] = "value"
539 outputMapping["port"] = "port"
540 outputMapping["ip"] = "ip"
545 if (dictionary_source == "complexTypeOneKeys")
546 outputMapping["host"] = "value"
548 outputMapping["name"] = "name"
549 outputMapping["ipAddress"] = "ipAddress"