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 {
52 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
53 private lateinit var resourceAssignment: ResourceAssignment
55 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
56 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
57 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
58 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
59 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
60 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
61 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
62 private lateinit var expectedValueToTestPrimitiveType: JsonNode
63 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
64 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
65 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
66 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
67 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
72 val bluePrintContext = runBlocking {
73 BluePrintMetadataUtils.getBluePrintContext(
74 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
78 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
80 // Init input map and expected values for tests
81 initInputMapAndExpectedValuesForPrimitiveType()
82 initInputMapAndExpectedValuesForCollection()
83 initInputMapAndExpectedValuesForComplexType()
85 val propertiesDefinition1 = PropertyDefinition().apply {
90 val propertiesDefinition2 = PropertyDefinition().apply {
95 val propertiesDefinition3 = PropertyDefinition().apply {
100 val propertiesDefinition4 = PropertyDefinition().apply {
105 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
106 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
107 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
109 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
110 mapOfPropertiesHost["name"] = propertiesDefinition3
111 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
113 val myDataTypeIpaddress = DataType().apply {
115 properties = mapOfPropertiesIpAddress
118 val myDataTypeHost = DataType().apply {
120 properties = mapOfPropertiesHost
124 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
125 } returns myDataTypeIpaddress
127 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
129 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
133 fun `generateResourceDataForAssignments - positive test`() {
134 // given a valid resource assignment
135 val validResourceAssignment1 = createResourceAssignmentForTest("valid_value", "pnf-id")
136 val validResourceAssignment2 = createResourceAssignmentForTest("also_valid", "a1")
138 // and a list containing that resource assignment
139 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment1, validResourceAssignment2)
141 // when the values of the resources are evaluated
142 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
144 // then the assignment should produce a valid result
148 "pnf-id" : "valid_value"
151 assertEquals(expected, outcome.trimIndent(), "unexpected outcome generated")
155 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
156 // given a valid resource assignment
157 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
159 // and a list containing that resource assignment
160 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
162 // when the values of the resources are evaluated
163 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
165 // then the assignment should produce a valid result
166 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
167 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
171 fun generateResolutionSummaryDataTest() {
172 val resourceAssignment = createResourceAssignmentForTest(null)
173 val resourceDefinition = ResourceDefinition()
174 val nodeTemplate = NodeTemplate().apply {
175 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
177 resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
178 resourceDefinition.property = PropertyDefinition().apply {
179 this.description = "pnf-id"
180 this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
183 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
184 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition)
190 "resolution-summary":[
196 "key-identifiers":[],
197 "dictionary-description":"pnf-id",
198 "dictionary-metadata":[
199 {"name":"aai-path","value":"//path/in/aai"}
201 "dictionary-name":"pnf-id",
202 "dictionary-source":"input",
203 "request-payload":{"mock":true},
209 """.replace("\n|\\s".toRegex(), ""),
214 private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
215 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
216 val resourceAssignmentForTest = ResourceAssignment().apply {
218 dictionaryName = "pnf-id"
219 dictionarySource = "input"
220 property = PropertyDefinition().apply {
225 return resourceAssignmentForTest
229 fun parseResponseNodeTestForPrimitivesTypes() {
230 var outcome = prepareResponseNodeForTest(
231 "sample-value", "string", "",
232 inputMapToTestPrimitiveTypeWithValue
235 expectedValueToTestPrimitiveType,
237 "Unexpected outcome returned for primitive type of simple String"
239 assertEquals(0, resourceAssignment.keyIdentifiers.size)
241 outcome = prepareResponseNodeForTest(
242 "sample-key-value", "string", "",
243 inputMapToTestPrimitiveTypeWithKeyValue
246 expectedValueToTestPrimitiveType,
248 "Unexpected outcome returned for primitive type of key-value String"
251 expectedValueToTestPrimitiveType,
252 resourceAssignment.keyIdentifiers[0].value
257 fun parseResponseNodeTestForCollectionsOfString() {
258 var outcome = prepareResponseNodeForTest(
259 "listOfString", "list",
260 "string", inputMapToTestCollectionOfPrimitiveType
263 expectedValueToTesCollectionOfPrimitiveType,
265 "unexpected outcome returned for list of String"
268 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
270 expectedKeyIdentifierValue,
271 resourceAssignment.keyIdentifiers[0].value
274 // FIXME("Map is not collection type, It is known complex type")
275 // outcome = prepareResponseNodeForTest(
276 // "mapOfString", "map", "string",
277 // inputMapToTestCollectionOfPrimitiveType
280 // expectedValueToTesCollectionOfPrimitiveType,
282 // "unexpected outcome returned for map of String"
287 fun parseResponseNodeTestForCollectionsOfComplexType() {
288 var outcome = prepareResponseNodeForTest(
289 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
290 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
293 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
295 "unexpected outcome returned for list of String"
298 outcome = prepareResponseNodeForTest(
299 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
300 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
303 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
305 "unexpected outcome returned for list of String"
310 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
311 val outcome = prepareResponseNodeForTest(
312 "complexTypeOneKeys", "host",
313 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
316 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
318 "Unexpected outcome returned for complex type"
321 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
322 resourceAssignment.keyIdentifiers[0].value
327 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
328 val outcome = prepareResponseNodeForTest(
329 "complexTypeAllKeys", "host",
330 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
333 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
335 "Unexpected outcome returned for complex type"
337 assertEquals(2, resourceAssignment.keyIdentifiers.size)
339 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
340 resourceAssignment.keyIdentifiers[0].value
344 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
345 resourceAssignment.keyIdentifiers[1].value
350 fun `transform resolved value with inline template`() {
351 resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
352 resourceAssignment = ResourceAssignment()
353 resourceAssignment.name = "int_pktgen_private_net_id"
354 resourceAssignment.property = PropertyDefinition()
355 resourceAssignment.property!!.type = "string"
356 val value = "".asJsonType()
358 // Enable transform template
359 resourceAssignment.property!!.metadata =
360 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
362 ResourceAssignmentUtils
363 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
367 resourceAssignment.property!!.value!!.asText()
371 private fun initInputMapAndExpectedValuesForPrimitiveType() {
372 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
373 val keyValue = mutableMapOf<String, String>()
374 keyValue["value"] = "1.2.3.1"
375 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
376 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
379 private fun initInputMapAndExpectedValuesForCollection() {
380 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
381 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
383 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
384 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
385 arrayNodeForList1.add(arrayChildNode)
387 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
389 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
390 ExpectedResponseIp("1.2.3.1"),
391 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
394 val listOfIpAddresses = arrayListOf(
395 IpAddress("1111", "1.2.3.1").asJsonType(),
396 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
398 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
399 listOfIpAddresses.forEach {
400 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
401 arrayChildNode.set<JsonNode>("value", it.asJsonType())
402 arrayNodeForList2.add(arrayChildNode)
404 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
406 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
407 var childNode = JacksonUtils.objectMapper.createObjectNode()
408 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
409 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
410 arrayNodeForList3.add(childNode)
411 childNode = JacksonUtils.objectMapper.createObjectNode()
412 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
413 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
414 arrayNodeForList3.add(childNode)
415 childNode = JacksonUtils.objectMapper.createObjectNode()
416 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
417 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
418 arrayNodeForList3.add(childNode)
419 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
421 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
422 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
423 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")),
424 ExpectedResponseIpAddress(
425 IpAddress("3333", "1.2.3.3")
428 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
429 IpAddress("1111", "1.2.3.1"),
430 IpAddress("2222", "1.2.3.2"),
431 IpAddress("3333", "1.2.3.3")
435 private fun initInputMapAndExpectedValuesForComplexType() {
436 val mapOfComplexType = mutableMapOf<String, JsonNode>()
437 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
438 mapOfComplexType["port"] = "8888".asJsonType()
439 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
440 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
442 val objectNode = JacksonUtils.objectMapper.createObjectNode()
443 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
444 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
446 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
447 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
448 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
449 childNode1.set<JsonNode>("port", "8888".asJsonType())
450 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
451 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
453 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
454 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
455 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
456 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
459 private fun prepareResponseNodeForTest(
460 dictionary_source: String,
466 resourceAssignment = when (sourceType) {
468 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
471 prepareRADataDictionaryOfPrimaryType(dictionary_source)
474 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
478 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
479 "Failed to get database query result into Json node."
482 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
484 return ResourceAssignmentUtils.parseResponseNode(
487 resourceAssignmentRuntimeService,
492 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
493 return ResourceAssignment().apply {
495 dictionaryName = "sample-ip"
496 dictionarySource = "$dictionary_source"
497 property = PropertyDefinition().apply {
503 private fun prepareRADataDictionaryCollection(
504 dictionary_source: String,
507 ): ResourceAssignment {
508 return ResourceAssignment().apply {
509 name = "ipAddress-list"
510 dictionaryName = "sample-licenses"
511 dictionarySource = "$dictionary_source"
512 property = PropertyDefinition().apply {
514 entrySchema = EntrySchema().apply {
521 private fun prepareRADataDictionaryComplexType(
522 dictionary_source: String,
525 ): ResourceAssignment {
526 return ResourceAssignment().apply {
527 name = "ipAddress-complexType"
528 dictionaryName = "sample-licenses"
529 dictionarySource = "$dictionary_source"
530 property = PropertyDefinition().apply {
536 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
537 val outputMapping = mutableMapOf<String, String>()
539 when (dictionary_source) {
540 "sample-key-value", "sample-value" -> {
542 if (dictionary_source == "sample-key-value")
543 outputMapping["sample-ip"] = "value"
545 "listOfString", "mapOfString" -> {
547 outputMapping["ip"] = "value"
549 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
550 // List or map of complex Type
551 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
552 outputMapping["ipAddress"] = "value"
554 outputMapping["port"] = "port"
555 outputMapping["ip"] = "ip"
560 if (dictionary_source == "complexTypeOneKeys")
561 outputMapping["host"] = "value"
563 outputMapping["name"] = "name"
564 outputMapping["ipAddress"] = "ipAddress"