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.controllerblueprints.core.asJsonPrimitive
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
34 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
36 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
42 import kotlin.test.assertEquals
44 data class IpAddress(val port: String, val ip: String)
45 data class Host(val name: String, val ipAddress: IpAddress)
46 data class ExpectedResponseIp(val ip: String)
47 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
49 class ResourceAssignmentUtilsTest {
50 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
51 private lateinit var resourceAssignment: ResourceAssignment
53 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
54 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
55 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
56 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
57 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
58 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
59 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
60 private lateinit var expectedValueToTestPrimitiveType: JsonNode
61 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
62 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
63 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
64 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
65 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
70 val bluePrintContext = runBlocking {
71 BluePrintMetadataUtils.getBluePrintContext(
72 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
76 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
78 // Init input map and expected values for tests
79 initInputMapAndExpectedValuesForPrimitiveType()
80 initInputMapAndExpectedValuesForCollection()
81 initInputMapAndExpectedValuesForComplexType()
83 val propertiesDefinition1 = PropertyDefinition().apply {
88 val propertiesDefinition2 = PropertyDefinition().apply {
93 val propertiesDefinition3 = PropertyDefinition().apply {
98 val propertiesDefinition4 = PropertyDefinition().apply {
103 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
104 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
105 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
107 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
108 mapOfPropertiesHost["name"] = propertiesDefinition3
109 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
111 val myDataTypeIpaddress = DataType().apply {
113 properties = mapOfPropertiesIpAddress
116 val myDataTypeHost = DataType().apply {
118 properties = mapOfPropertiesHost
122 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
123 } returns myDataTypeIpaddress
125 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
127 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
131 fun `generateResourceDataForAssignments - positive test`() {
132 // given a valid resource assignment
133 val validResourceAssignment = createResourceAssignmentForTest("valid_value")
135 // and a list containing that resource assignment
136 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
138 // when the values of the resources are evaluated
139 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
141 // then the assignment should produce a valid result
142 val expected = "{\n" + " \"pnf-id\" : \"valid_value\"\n" + "}"
143 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
147 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
148 // given a valid resource assignment
149 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
151 // and a list containing that resource assignment
152 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
154 // when the values of the resources are evaluated
155 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
157 // then the assignment should produce a valid result
158 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
159 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
164 val resourceAssignment = createResourceAssignmentForTest(null)
165 val resourceDefinition = ResourceDefinition()
166 val nodeTemplate = NodeTemplate().apply {
167 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
169 resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
171 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
172 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition))
180 "key-identifiers":[],
181 "dictionary-name":"pnf-id",
182 "request-payload":{"mock":true},
183 "dictionary-source":"input",
187 """.replace("\n|\\s".toRegex(), ""), result)
190 private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
191 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
192 val resourceAssignmentForTest = ResourceAssignment().apply {
194 dictionaryName = "pnf-id"
195 dictionarySource = "input"
196 property = PropertyDefinition().apply {
201 return resourceAssignmentForTest
205 fun parseResponseNodeTestForPrimitivesTypes() {
206 var outcome = prepareResponseNodeForTest(
207 "sample-value", "string", "",
208 inputMapToTestPrimitiveTypeWithValue
211 expectedValueToTestPrimitiveType,
213 "Unexpected outcome returned for primitive type of simple String"
215 assertEquals(0, resourceAssignment.keyIdentifiers.size)
217 outcome = prepareResponseNodeForTest(
218 "sample-key-value", "string", "",
219 inputMapToTestPrimitiveTypeWithKeyValue
222 expectedValueToTestPrimitiveType,
224 "Unexpected outcome returned for primitive type of key-value String"
227 expectedValueToTestPrimitiveType,
228 resourceAssignment.keyIdentifiers[0].value
233 fun parseResponseNodeTestForCollectionsOfString() {
234 var outcome = prepareResponseNodeForTest(
235 "listOfString", "list",
236 "string", inputMapToTestCollectionOfPrimitiveType
239 expectedValueToTesCollectionOfPrimitiveType,
241 "unexpected outcome returned for list of String"
244 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
246 expectedKeyIdentifierValue,
247 resourceAssignment.keyIdentifiers[0].value
250 // FIXME("Map is not collection type, It is known complex type")
251 // outcome = prepareResponseNodeForTest(
252 // "mapOfString", "map", "string",
253 // inputMapToTestCollectionOfPrimitiveType
256 // expectedValueToTesCollectionOfPrimitiveType,
258 // "unexpected outcome returned for map of String"
263 fun parseResponseNodeTestForCollectionsOfComplexType() {
264 var outcome = prepareResponseNodeForTest(
265 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
266 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
269 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
271 "unexpected outcome returned for list of String"
274 outcome = prepareResponseNodeForTest(
275 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
276 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
279 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
281 "unexpected outcome returned for list of String"
286 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
287 val outcome = prepareResponseNodeForTest(
288 "complexTypeOneKeys", "host",
289 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
292 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
294 "Unexpected outcome returned for complex type"
297 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
298 resourceAssignment.keyIdentifiers[0].value)
302 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
303 val outcome = prepareResponseNodeForTest(
304 "complexTypeAllKeys", "host",
305 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
308 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
310 "Unexpected outcome returned for complex type"
312 assertEquals(2, resourceAssignment.keyIdentifiers.size)
314 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
315 resourceAssignment.keyIdentifiers[0].value
319 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
320 resourceAssignment.keyIdentifiers[1].value
324 private fun initInputMapAndExpectedValuesForPrimitiveType() {
325 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
326 val keyValue = mutableMapOf<String, String>()
327 keyValue["value"] = "1.2.3.1"
328 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
329 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
332 private fun initInputMapAndExpectedValuesForCollection() {
333 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
334 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
336 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
337 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
338 arrayNodeForList1.add(arrayChildNode)
340 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
342 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
343 ExpectedResponseIp("1.2.3.1"),
344 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
347 val listOfIpAddresses = arrayListOf(
348 IpAddress("1111", "1.2.3.1").asJsonType(),
349 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
351 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
352 listOfIpAddresses.forEach {
353 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
354 arrayChildNode.set<JsonNode>("value", it.asJsonType())
355 arrayNodeForList2.add(arrayChildNode)
357 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
359 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
360 var childNode = JacksonUtils.objectMapper.createObjectNode()
361 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
362 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
363 arrayNodeForList3.add(childNode)
364 childNode = JacksonUtils.objectMapper.createObjectNode()
365 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
366 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
367 arrayNodeForList3.add(childNode)
368 childNode = JacksonUtils.objectMapper.createObjectNode()
369 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
370 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
371 arrayNodeForList3.add(childNode)
372 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
374 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
375 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
376 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
377 IpAddress("3333", "1.2.3.3")
380 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
381 IpAddress("1111", "1.2.3.1"),
382 IpAddress("2222", "1.2.3.2"),
383 IpAddress("3333", "1.2.3.3")
387 private fun initInputMapAndExpectedValuesForComplexType() {
388 val mapOfComplexType = mutableMapOf<String, JsonNode>()
389 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
390 mapOfComplexType["port"] = "8888".asJsonType()
391 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
392 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
394 val objectNode = JacksonUtils.objectMapper.createObjectNode()
395 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
396 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
398 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
399 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
400 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
401 childNode1.set<JsonNode>("port", "8888".asJsonType())
402 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
403 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
405 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
406 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
407 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
408 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
411 private fun prepareResponseNodeForTest(
412 dictionary_source: String,
418 resourceAssignment = when (sourceType) {
420 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
423 prepareRADataDictionaryOfPrimaryType(dictionary_source)
426 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
430 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
431 "Failed to get database query result into Json node."
434 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
436 return ResourceAssignmentUtils.parseResponseNode(
439 resourceAssignmentRuntimeService,
444 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
445 return ResourceAssignment().apply {
447 dictionaryName = "sample-ip"
448 dictionarySource = "$dictionary_source"
449 property = PropertyDefinition().apply {
455 private fun prepareRADataDictionaryCollection(
456 dictionary_source: String,
459 ): ResourceAssignment {
460 return ResourceAssignment().apply {
461 name = "ipAddress-list"
462 dictionaryName = "sample-licenses"
463 dictionarySource = "$dictionary_source"
464 property = PropertyDefinition().apply {
466 entrySchema = EntrySchema().apply {
473 private fun prepareRADataDictionaryComplexType(
474 dictionary_source: String,
477 ): ResourceAssignment {
478 return ResourceAssignment().apply {
479 name = "ipAddress-complexType"
480 dictionaryName = "sample-licenses"
481 dictionarySource = "$dictionary_source"
482 property = PropertyDefinition().apply {
488 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
489 val outputMapping = mutableMapOf<String, String>()
491 when (dictionary_source) {
492 "sample-key-value", "sample-value" -> {
494 if (dictionary_source == "sample-key-value")
495 outputMapping["sample-ip"] = "value"
497 "listOfString", "mapOfString" -> {
499 outputMapping["ip"] = "value"
501 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
502 // List or map of complex Type
503 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
504 outputMapping["ipAddress"] = "value"
506 outputMapping["port"] = "port"
507 outputMapping["ip"] = "ip"
512 if (dictionary_source == "complexTypeOneKeys")
513 outputMapping["host"] = "value"
515 outputMapping["name"] = "name"
516 outputMapping["ipAddress"] = "ipAddress"