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.PropertyDefinition
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
40 import kotlin.test.assertEquals
42 data class IpAddress(val port: String, val ip: String)
43 data class Host(val name: String, val ipAddress: IpAddress)
44 data class ExpectedResponseIp(val ip: String)
45 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
47 class ResourceAssignmentUtilsTest {
48 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
50 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
51 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
52 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
53 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
54 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
55 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
56 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
57 private lateinit var expectedValueToTestPrimitiveType: JsonNode
58 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
59 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
60 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
61 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
62 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
67 val bluePrintContext = runBlocking {
68 BluePrintMetadataUtils.getBluePrintContext(
69 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
73 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
75 // Init input map and expected values for tests
76 initInputMapAndExpectedValuesForPrimitiveType()
77 initInputMapAndExpectedValuesForCollection()
78 initInputMapAndExpectedValuesForComplexType()
80 val propertiesDefinition1 = PropertyDefinition().apply {
85 val propertiesDefinition2 = PropertyDefinition().apply {
90 val propertiesDefinition3 = PropertyDefinition().apply {
95 val propertiesDefinition4 = PropertyDefinition().apply {
100 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
101 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
102 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
104 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
105 mapOfPropertiesHost["name"] = propertiesDefinition3
106 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
108 val myDataTypeIpaddress = DataType().apply {
110 properties = mapOfPropertiesIpAddress
113 val myDataTypeHost = DataType().apply {
115 properties = mapOfPropertiesHost
119 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
120 } returns myDataTypeIpaddress
122 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
124 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
128 fun `generateResourceDataForAssignments - positive test`() {
129 // given a valid resource assignment
130 val validResourceAssignment = createResourceAssignmentForTest("valid_value")
132 // and a list containing that resource assignment
133 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
135 // when the values of the resources are evaluated
136 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
138 // then the assignment should produce a valid result
139 val expected = "{\n" + " \"pnf-id\" : \"valid_value\"\n" + "}"
140 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
144 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
145 // given a valid resource assignment
146 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
148 // and a list containing that resource assignment
149 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
151 // when the values of the resources are evaluated
152 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
154 // then the assignment should produce a valid result
155 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
156 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
159 private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
160 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
161 val resourceAssignmentForTest = ResourceAssignment().apply {
163 dictionaryName = "pnf-id"
164 dictionarySource = "input"
165 property = PropertyDefinition().apply {
170 return resourceAssignmentForTest
174 fun parseResponseNodeTestForPrimitivesTypes() {
175 var outcome = prepareResponseNodeForTest(
176 "sample-value", "string", "",
177 inputMapToTestPrimitiveTypeWithValue
180 expectedValueToTestPrimitiveType,
182 "Unexpected outcome returned for primitive type of simple String"
185 outcome = prepareResponseNodeForTest(
186 "sample-key-value", "string", "",
187 inputMapToTestPrimitiveTypeWithKeyValue
190 expectedValueToTestPrimitiveType,
192 "Unexpected outcome returned for primitive type of key-value String"
197 fun parseResponseNodeTestForCollectionsOfString() {
198 var outcome = prepareResponseNodeForTest(
199 "listOfString", "list",
200 "string", inputMapToTestCollectionOfPrimitiveType
203 expectedValueToTesCollectionOfPrimitiveType,
205 "unexpected outcome returned for list of String"
208 outcome = prepareResponseNodeForTest(
209 "mapOfString", "map", "string",
210 inputMapToTestCollectionOfPrimitiveType
213 expectedValueToTesCollectionOfPrimitiveType,
215 "unexpected outcome returned for map of String"
220 fun parseResponseNodeTestForCollectionsOfComplexType() {
221 var outcome = prepareResponseNodeForTest(
222 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
223 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
226 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
228 "unexpected outcome returned for list of String"
231 outcome = prepareResponseNodeForTest(
232 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
233 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
236 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
238 "unexpected outcome returned for list of String"
243 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
244 val outcome = prepareResponseNodeForTest(
245 "complexTypeOneKeys", "host",
246 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
249 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
251 "Unexpected outcome returned for complex type"
256 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
257 val outcome = prepareResponseNodeForTest(
258 "complexTypeAllKeys", "host",
259 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
262 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
264 "Unexpected outcome returned for complex type"
268 private fun initInputMapAndExpectedValuesForPrimitiveType() {
269 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
270 val keyValue = mutableMapOf<String, String>()
271 keyValue["value"] = "1.2.3.1"
272 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
273 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
276 private fun initInputMapAndExpectedValuesForCollection() {
277 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
278 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
280 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
281 arrayChildNode.set("value", it.asJsonPrimitive())
282 arrayNodeForList1.add(arrayChildNode)
284 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
286 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
287 ExpectedResponseIp("1.2.3.1"),
288 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
291 val listOfIpAddresses = arrayListOf(
292 IpAddress("1111", "1.2.3.1").asJsonType(),
293 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
295 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
296 listOfIpAddresses.forEach {
297 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
298 arrayChildNode.set("value", it.asJsonType())
299 arrayNodeForList2.add(arrayChildNode)
301 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
303 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
304 var childNode = JacksonUtils.objectMapper.createObjectNode()
305 childNode.set("port", "1111".asJsonPrimitive())
306 childNode.set("ip", "1.2.3.1".asJsonPrimitive())
307 arrayNodeForList3.add(childNode)
308 childNode = JacksonUtils.objectMapper.createObjectNode()
309 childNode.set("port", "2222".asJsonPrimitive())
310 childNode.set("ip", "1.2.3.2".asJsonPrimitive())
311 arrayNodeForList3.add(childNode)
312 childNode = JacksonUtils.objectMapper.createObjectNode()
313 childNode.set("port", "3333".asJsonPrimitive())
314 childNode.set("ip", "1.2.3.3".asJsonPrimitive())
315 arrayNodeForList3.add(childNode)
316 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
318 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
319 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
320 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
321 IpAddress("3333", "1.2.3.3")
324 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
325 IpAddress("1111", "1.2.3.1"),
326 IpAddress("2222", "1.2.3.2"),
327 IpAddress("3333", "1.2.3.3")
331 private fun initInputMapAndExpectedValuesForComplexType() {
332 val mapOfComplexType = mutableMapOf<String, JsonNode>()
333 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
334 mapOfComplexType["port"] = "8888".asJsonType()
335 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
336 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
338 val objectNode = JacksonUtils.objectMapper.createObjectNode()
339 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
340 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
342 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
343 childNode1.set("name", "my-ipAddress".asJsonPrimitive())
344 childNode1.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
345 childNode1.set("port", "8888".asJsonType())
346 childNode1.set("something", IpAddress("2222", "1.2.3.1").asJsonType())
347 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
349 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
350 childNode2.set("name", "my-ipAddress".asJsonPrimitive())
351 childNode2.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
352 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
355 private fun prepareResponseNodeForTest(
356 dictionary_source: String,
362 val resourceAssignment = when (sourceType) {
364 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
367 prepareRADataDictionaryOfPrimaryType(dictionary_source)
370 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
374 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
375 "Failed to get database query result into Json node."
378 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
380 return ResourceAssignmentUtils.parseResponseNode(
383 resourceAssignmentRuntimeService,
388 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
389 return ResourceAssignment().apply {
391 dictionaryName = "sample-ip"
392 dictionarySource = "$dictionary_source"
393 property = PropertyDefinition().apply {
399 private fun prepareRADataDictionaryCollection(
400 dictionary_source: String,
403 ): ResourceAssignment {
404 return ResourceAssignment().apply {
405 name = "ipAddress-list"
406 dictionaryName = "sample-licenses"
407 dictionarySource = "$dictionary_source"
408 property = PropertyDefinition().apply {
410 entrySchema = EntrySchema().apply {
417 private fun prepareRADataDictionaryComplexType(
418 dictionary_source: String,
421 ): ResourceAssignment {
422 return ResourceAssignment().apply {
423 name = "ipAddress-complexType"
424 dictionaryName = "sample-licenses"
425 dictionarySource = "$dictionary_source"
426 property = PropertyDefinition().apply {
432 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
433 val outputMapping = mutableMapOf<String, String>()
435 when (dictionary_source) {
436 "sample-key-value", "sample-value" -> {
438 if (dictionary_source == "sample-key-value")
439 outputMapping["sample-ip"] = "value"
441 "listOfString", "mapOfString" -> {
443 outputMapping["ip"] = "value"
445 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
446 // List or map of complex Type
447 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
448 outputMapping["ipAddress"] = "value"
450 outputMapping["port"] = "port"
451 outputMapping["ip"] = "ip"
456 if (dictionary_source == "complexTypeOneKeys")
457 outputMapping["host"] = "value"
459 outputMapping["name"] = "name"
460 outputMapping["ipAddress"] = "ipAddress"