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 validResourceAssignment1 = createResourceAssignmentForTest("valid_value", "pnf-id")
135 val validResourceAssignment2 = createResourceAssignmentForTest("also_valid", "a1")
137 // and a list containing that resource assignment
138 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment1, validResourceAssignment2)
140 // when the values of the resources are evaluated
141 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
143 // then the assignment should produce a valid result
147 "pnf-id" : "valid_value"
150 assertEquals(expected, outcome.trimIndent(), "unexpected outcome generated")
154 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
155 // given a valid resource assignment
156 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
158 // and a list containing that resource assignment
159 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
161 // when the values of the resources are evaluated
162 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
164 // then the assignment should produce a valid result
165 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
166 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
170 fun generateResolutionSummaryDataTest() {
171 val resourceAssignment = createResourceAssignmentForTest(null)
172 val resourceDefinition = ResourceDefinition()
173 val nodeTemplate = NodeTemplate().apply {
174 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
176 resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
177 resourceDefinition.property = PropertyDefinition().apply {
178 this.description = "pnf-id"
179 this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
182 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
183 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition))
187 "resolution-summary":[
193 "key-identifiers":[],
194 "dictionary-description":"pnf-id",
195 "dictionary-metadata":[
196 {"name":"aai-path","value":"//path/in/aai"}
198 "dictionary-name":"pnf-id",
199 "dictionary-source":"input",
200 "request-payload":{"mock":true},
206 """.replace("\n|\\s".toRegex(), ""), result)
209 private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
210 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
211 val resourceAssignmentForTest = ResourceAssignment().apply {
213 dictionaryName = "pnf-id"
214 dictionarySource = "input"
215 property = PropertyDefinition().apply {
220 return resourceAssignmentForTest
224 fun parseResponseNodeTestForPrimitivesTypes() {
225 var outcome = prepareResponseNodeForTest(
226 "sample-value", "string", "",
227 inputMapToTestPrimitiveTypeWithValue
230 expectedValueToTestPrimitiveType,
232 "Unexpected outcome returned for primitive type of simple String"
234 assertEquals(0, resourceAssignment.keyIdentifiers.size)
236 outcome = prepareResponseNodeForTest(
237 "sample-key-value", "string", "",
238 inputMapToTestPrimitiveTypeWithKeyValue
241 expectedValueToTestPrimitiveType,
243 "Unexpected outcome returned for primitive type of key-value String"
246 expectedValueToTestPrimitiveType,
247 resourceAssignment.keyIdentifiers[0].value
252 fun parseResponseNodeTestForCollectionsOfString() {
253 var outcome = prepareResponseNodeForTest(
254 "listOfString", "list",
255 "string", inputMapToTestCollectionOfPrimitiveType
258 expectedValueToTesCollectionOfPrimitiveType,
260 "unexpected outcome returned for list of String"
263 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
265 expectedKeyIdentifierValue,
266 resourceAssignment.keyIdentifiers[0].value
269 // FIXME("Map is not collection type, It is known complex type")
270 // outcome = prepareResponseNodeForTest(
271 // "mapOfString", "map", "string",
272 // inputMapToTestCollectionOfPrimitiveType
275 // expectedValueToTesCollectionOfPrimitiveType,
277 // "unexpected outcome returned for map of String"
282 fun parseResponseNodeTestForCollectionsOfComplexType() {
283 var outcome = prepareResponseNodeForTest(
284 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
285 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
288 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
290 "unexpected outcome returned for list of String"
293 outcome = prepareResponseNodeForTest(
294 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
295 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
298 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
300 "unexpected outcome returned for list of String"
305 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
306 val outcome = prepareResponseNodeForTest(
307 "complexTypeOneKeys", "host",
308 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
311 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
313 "Unexpected outcome returned for complex type"
316 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
317 resourceAssignment.keyIdentifiers[0].value)
321 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
322 val outcome = prepareResponseNodeForTest(
323 "complexTypeAllKeys", "host",
324 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
327 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
329 "Unexpected outcome returned for complex type"
331 assertEquals(2, resourceAssignment.keyIdentifiers.size)
333 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
334 resourceAssignment.keyIdentifiers[0].value
338 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
339 resourceAssignment.keyIdentifiers[1].value
344 fun `transform resolved value with inline template`() {
345 resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
346 resourceAssignment = ResourceAssignment()
347 resourceAssignment.name = "int_pktgen_private_net_id"
348 resourceAssignment.property = PropertyDefinition()
349 resourceAssignment.property!!.type = "string"
350 val value = "".asJsonType()
352 // Enable transform template
353 resourceAssignment.property!!.metadata =
354 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
356 ResourceAssignmentUtils
357 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
359 assertEquals("abc-vnf_private2",
360 resourceAssignment.property!!.value!!.asText())
363 private fun initInputMapAndExpectedValuesForPrimitiveType() {
364 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
365 val keyValue = mutableMapOf<String, String>()
366 keyValue["value"] = "1.2.3.1"
367 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
368 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
371 private fun initInputMapAndExpectedValuesForCollection() {
372 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
373 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
375 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
376 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
377 arrayNodeForList1.add(arrayChildNode)
379 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
381 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
382 ExpectedResponseIp("1.2.3.1"),
383 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
386 val listOfIpAddresses = arrayListOf(
387 IpAddress("1111", "1.2.3.1").asJsonType(),
388 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
390 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
391 listOfIpAddresses.forEach {
392 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
393 arrayChildNode.set<JsonNode>("value", it.asJsonType())
394 arrayNodeForList2.add(arrayChildNode)
396 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
398 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
399 var childNode = JacksonUtils.objectMapper.createObjectNode()
400 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
401 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
402 arrayNodeForList3.add(childNode)
403 childNode = JacksonUtils.objectMapper.createObjectNode()
404 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
405 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
406 arrayNodeForList3.add(childNode)
407 childNode = JacksonUtils.objectMapper.createObjectNode()
408 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
409 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
410 arrayNodeForList3.add(childNode)
411 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
413 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
414 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
415 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
416 IpAddress("3333", "1.2.3.3")
419 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
420 IpAddress("1111", "1.2.3.1"),
421 IpAddress("2222", "1.2.3.2"),
422 IpAddress("3333", "1.2.3.3")
426 private fun initInputMapAndExpectedValuesForComplexType() {
427 val mapOfComplexType = mutableMapOf<String, JsonNode>()
428 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
429 mapOfComplexType["port"] = "8888".asJsonType()
430 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
431 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
433 val objectNode = JacksonUtils.objectMapper.createObjectNode()
434 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
435 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
437 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
438 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
439 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
440 childNode1.set<JsonNode>("port", "8888".asJsonType())
441 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
442 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
444 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
445 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
446 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
447 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
450 private fun prepareResponseNodeForTest(
451 dictionary_source: String,
457 resourceAssignment = when (sourceType) {
459 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
462 prepareRADataDictionaryOfPrimaryType(dictionary_source)
465 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
469 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
470 "Failed to get database query result into Json node."
473 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
475 return ResourceAssignmentUtils.parseResponseNode(
478 resourceAssignmentRuntimeService,
483 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
484 return ResourceAssignment().apply {
486 dictionaryName = "sample-ip"
487 dictionarySource = "$dictionary_source"
488 property = PropertyDefinition().apply {
494 private fun prepareRADataDictionaryCollection(
495 dictionary_source: String,
498 ): ResourceAssignment {
499 return ResourceAssignment().apply {
500 name = "ipAddress-list"
501 dictionaryName = "sample-licenses"
502 dictionarySource = "$dictionary_source"
503 property = PropertyDefinition().apply {
505 entrySchema = EntrySchema().apply {
512 private fun prepareRADataDictionaryComplexType(
513 dictionary_source: String,
516 ): ResourceAssignment {
517 return ResourceAssignment().apply {
518 name = "ipAddress-complexType"
519 dictionaryName = "sample-licenses"
520 dictionarySource = "$dictionary_source"
521 property = PropertyDefinition().apply {
527 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
528 val outputMapping = mutableMapOf<String, String>()
530 when (dictionary_source) {
531 "sample-key-value", "sample-value" -> {
533 if (dictionary_source == "sample-key-value")
534 outputMapping["sample-ip"] = "value"
536 "listOfString", "mapOfString" -> {
538 outputMapping["ip"] = "value"
540 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
541 // List or map of complex Type
542 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
543 outputMapping["ipAddress"] = "value"
545 outputMapping["port"] = "port"
546 outputMapping["ip"] = "ip"
551 if (dictionary_source == "complexTypeOneKeys")
552 outputMapping["host"] = "value"
554 outputMapping["name"] = "name"
555 outputMapping["ipAddress"] = "ipAddress"