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 org.junit.Before
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
33 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
34 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
38 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
39 import kotlin.test.assertEquals
41 data class IpAddress(val port: String, val ip: String)
42 data class Host(val name: String, val ipAddress: IpAddress)
43 data class ExpectedResponseIp(val ip: String)
44 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
46 class ResourceAssignmentUtilsTest {
47 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
49 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
50 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
51 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
52 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
53 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
54 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
55 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
56 private lateinit var expectedValueToTestPrimitiveType: JsonNode
57 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
58 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
59 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
60 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
61 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
66 val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
67 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
70 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
72 // Init input map and expected values for tests
73 initInputMapAndExpectedValuesForPrimitiveType()
74 initInputMapAndExpectedValuesForCollection()
75 initInputMapAndExpectedValuesForComplexType()
77 val propertiesDefinition1 = PropertyDefinition().apply {
82 val propertiesDefinition2 = PropertyDefinition().apply {
87 val propertiesDefinition3 = PropertyDefinition().apply {
92 val propertiesDefinition4 = PropertyDefinition().apply {
97 var mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
98 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
99 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
101 var mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
102 mapOfPropertiesHost["name"] = propertiesDefinition3
103 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
105 val myDataTypeIpaddress = DataType().apply {
107 properties = mapOfPropertiesIpAddress
110 val myDataTypeHost = DataType().apply {
112 properties = mapOfPropertiesHost
116 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
117 } returns myDataTypeIpaddress
119 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
121 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
125 fun `generateResourceDataForAssignments - positive test`() {
126 // given a valid resource assignment
127 val validResourceAssignment = createResourceAssignmentForTest("valid_value")
129 // and a list containing that resource assignment
130 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
132 // when the values of the resources are evaluated
133 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
135 // then the assignment should produce a valid result
136 val expected = "{\n" + " \"pnf-id\" : \"valid_value\"\n" + "}"
137 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
141 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
142 // given a valid resource assignment
143 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
145 // and a list containing that resource assignment
146 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
148 // when the values of the resources are evaluated
149 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
151 // then the assignment should produce a valid result
152 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
153 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
156 private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
157 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
158 val resourceAssignmentForTest = ResourceAssignment().apply {
160 dictionaryName = "pnf-id"
161 dictionarySource = "input"
162 property = PropertyDefinition().apply {
167 return resourceAssignmentForTest
171 fun parseResponseNodeTestForPrimitivesTypes() {
172 var outcome = prepareResponseNodeForTest(
173 "sample-value", "string", "",
174 inputMapToTestPrimitiveTypeWithValue
177 expectedValueToTestPrimitiveType,
179 "Unexpected outcome returned for primitive type of simple String"
182 outcome = prepareResponseNodeForTest(
183 "sample-key-value", "string", "",
184 inputMapToTestPrimitiveTypeWithKeyValue
187 expectedValueToTestPrimitiveType,
189 "Unexpected outcome returned for primitive type of key-value String"
194 fun parseResponseNodeTestForCollectionsOfString() {
195 var outcome = prepareResponseNodeForTest(
196 "listOfString", "list",
197 "string", inputMapToTestCollectionOfPrimitiveType
200 expectedValueToTesCollectionOfPrimitiveType,
202 "unexpected outcome returned for list of String"
205 outcome = prepareResponseNodeForTest(
206 "mapOfString", "map", "string",
207 inputMapToTestCollectionOfPrimitiveType
210 expectedValueToTesCollectionOfPrimitiveType,
212 "unexpected outcome returned for map of String"
217 fun parseResponseNodeTestForCollectionsOfComplexType() {
218 var outcome = prepareResponseNodeForTest(
219 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
220 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
223 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
225 "unexpected outcome returned for list of String"
228 outcome = prepareResponseNodeForTest(
229 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
230 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
233 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
235 "unexpected outcome returned for list of String"
240 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
241 val outcome = prepareResponseNodeForTest(
242 "complexTypeOneKeys", "host",
243 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
246 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
248 "Unexpected outcome returned for complex type"
253 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
254 val outcome = prepareResponseNodeForTest(
255 "complexTypeAllKeys", "host",
256 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
259 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
261 "Unexpected outcome returned for complex type"
265 private fun initInputMapAndExpectedValuesForPrimitiveType() {
266 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
267 val keyValue = mutableMapOf<String, String>()
268 keyValue["value"] = "1.2.3.1"
269 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
270 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
273 private fun initInputMapAndExpectedValuesForCollection() {
274 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
275 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
277 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
278 arrayChildNode.set("value", it.asJsonPrimitive())
279 arrayNodeForList1.add(arrayChildNode)
281 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
283 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
284 ExpectedResponseIp("1.2.3.1"),
285 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
288 val listOfIpAddresses = arrayListOf(
289 IpAddress("1111", "1.2.3.1").asJsonType(),
290 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
292 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
293 listOfIpAddresses.forEach {
294 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
295 arrayChildNode.set("value", it.asJsonType())
296 arrayNodeForList2.add(arrayChildNode)
298 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
300 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
301 var childNode = JacksonUtils.objectMapper.createObjectNode()
302 childNode.set("port", "1111".asJsonPrimitive())
303 childNode.set("ip", "1.2.3.1".asJsonPrimitive())
304 arrayNodeForList3.add(childNode)
305 childNode = JacksonUtils.objectMapper.createObjectNode()
306 childNode.set("port", "2222".asJsonPrimitive())
307 childNode.set("ip", "1.2.3.2".asJsonPrimitive())
308 arrayNodeForList3.add(childNode)
309 childNode = JacksonUtils.objectMapper.createObjectNode()
310 childNode.set("port", "3333".asJsonPrimitive())
311 childNode.set("ip", "1.2.3.3".asJsonPrimitive())
312 arrayNodeForList3.add(childNode)
313 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
315 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
316 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
317 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
318 IpAddress("3333", "1.2.3.3")
321 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
322 IpAddress("1111", "1.2.3.1"),
323 IpAddress("2222", "1.2.3.2"),
324 IpAddress("3333", "1.2.3.3")
328 private fun initInputMapAndExpectedValuesForComplexType() {
329 val mapOfComplexType = mutableMapOf<String, JsonNode>()
330 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
331 mapOfComplexType["port"] = "8888".asJsonType()
332 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
333 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
335 val objectNode = JacksonUtils.objectMapper.createObjectNode()
336 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
337 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
339 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
340 childNode1.set("name", "my-ipAddress".asJsonPrimitive())
341 childNode1.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
342 childNode1.set("port", "8888".asJsonType())
343 childNode1.set("something", IpAddress("2222", "1.2.3.1").asJsonType())
344 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
346 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
347 childNode2.set("name", "my-ipAddress".asJsonPrimitive())
348 childNode2.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
349 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
352 private fun prepareResponseNodeForTest(
353 dictionary_source: String,
359 val resourceAssignment = when (sourceType) {
361 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
364 prepareRADataDictionaryOfPrimaryType(dictionary_source)
367 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
371 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
372 "Failed to get database query result into Json node."
375 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
377 return ResourceAssignmentUtils.parseResponseNode(
380 resourceAssignmentRuntimeService,
385 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
386 return ResourceAssignment().apply {
388 dictionaryName = "sample-ip"
389 dictionarySource = "$dictionary_source"
390 property = PropertyDefinition().apply {
396 private fun prepareRADataDictionaryCollection(
397 dictionary_source: String,
400 ): ResourceAssignment {
401 return ResourceAssignment().apply {
402 name = "ipAddress-list"
403 dictionaryName = "sample-licenses"
404 dictionarySource = "$dictionary_source"
405 property = PropertyDefinition().apply {
407 entrySchema = EntrySchema().apply {
414 private fun prepareRADataDictionaryComplexType(
415 dictionary_source: String,
418 ): ResourceAssignment {
419 return ResourceAssignment().apply {
420 name = "ipAddress-complexType"
421 dictionaryName = "sample-licenses"
422 dictionarySource = "$dictionary_source"
423 property = PropertyDefinition().apply {
429 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
430 val outputMapping = mutableMapOf<String, String>()
432 when (dictionary_source) {
433 "sample-key-value", "sample-value" -> {
435 if (dictionary_source == "sample-key-value")
436 outputMapping["sample-ip"] = "value"
438 "listOfString", "mapOfString" -> {
440 outputMapping["ip"] = "value"
442 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
443 // List or map of complex Type
444 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
445 outputMapping["ipAddress"] = "value"
447 outputMapping["port"] = "port"
448 outputMapping["ip"] = "ip"
453 if (dictionary_source == "complexTypeOneKeys")
454 outputMapping["host"] = "value"
456 outputMapping["name"] = "name"
457 outputMapping["ipAddress"] = "ipAddress"