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
115 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address") } returns myDataTypeIpaddress
117 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
119 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
123 fun `generateResourceDataForAssignments - positive test`() {
124 // given a valid resource assignment
125 val validResourceAssignment = createResourceAssignmentForTest("valid_value")
127 // and a list containing that resource assignment
128 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
130 // when the values of the resources are evaluated
131 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
133 // then the assignment should produce a valid result
134 val expected = "{\n" + " \"pnf-id\" : \"valid_value\"\n" + "}"
135 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
139 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
140 // given a valid resource assignment
141 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
143 // and a list containing that resource assignment
144 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
146 // when the values of the resources are evaluated
147 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
149 // then the assignment should produce a valid result
150 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
151 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
154 private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
155 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
156 val resourceAssignmentForTest = ResourceAssignment().apply {
158 dictionaryName = "pnf-id"
159 dictionarySource = "input"
160 property = PropertyDefinition().apply {
165 return resourceAssignmentForTest
169 fun parseResponseNodeTestForPrimitivesTypes() {
170 var outcome = prepareResponseNodeForTest(
171 "sample-value", "string", "",
172 inputMapToTestPrimitiveTypeWithValue
174 assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of simple String")
176 outcome = prepareResponseNodeForTest(
177 "sample-key-value", "string", "",
178 inputMapToTestPrimitiveTypeWithKeyValue
180 assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of key-value String")
184 fun parseResponseNodeTestForCollectionsOfString() {
185 var outcome = prepareResponseNodeForTest(
186 "listOfString", "list",
187 "string", inputMapToTestCollectionOfPrimitiveType
189 assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for list of String")
191 outcome = prepareResponseNodeForTest(
192 "mapOfString", "map", "string",
193 inputMapToTestCollectionOfPrimitiveType
195 assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for map of String")
199 fun parseResponseNodeTestForCollectionsOfComplexType() {
200 var outcome = prepareResponseNodeForTest(
201 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
202 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
204 assertEquals(expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
206 outcome = prepareResponseNodeForTest(
207 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
208 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
210 assertEquals(expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
214 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
215 val outcome = prepareResponseNodeForTest(
216 "complexTypeOneKeys", "host",
217 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
219 assertEquals(expectedValueToTestComplexTypeWithOneOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
223 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
224 val outcome = prepareResponseNodeForTest(
225 "complexTypeAllKeys", "host",
226 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
228 assertEquals(expectedValueToTestComplexTypeWithAllOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
231 private fun initInputMapAndExpectedValuesForPrimitiveType() {
232 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
233 val keyValue = mutableMapOf<String, String>()
234 keyValue["value"] = "1.2.3.1"
235 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
236 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
239 private fun initInputMapAndExpectedValuesForCollection() {
240 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
241 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
243 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
244 arrayChildNode.set("value", it.asJsonPrimitive())
245 arrayNodeForList1.add(arrayChildNode)
247 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
249 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
250 ExpectedResponseIp("1.2.3.1"),
251 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
254 val listOfIpAddresses = arrayListOf(
255 IpAddress("1111", "1.2.3.1").asJsonType(),
256 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
258 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
259 listOfIpAddresses.forEach {
260 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
261 arrayChildNode.set("value", it.asJsonType())
262 arrayNodeForList2.add(arrayChildNode)
264 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
266 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
267 var childNode = JacksonUtils.objectMapper.createObjectNode()
268 childNode.set("port", "1111".asJsonPrimitive())
269 childNode.set("ip", "1.2.3.1".asJsonPrimitive())
270 arrayNodeForList3.add(childNode)
271 childNode = JacksonUtils.objectMapper.createObjectNode()
272 childNode.set("port", "2222".asJsonPrimitive())
273 childNode.set("ip", "1.2.3.2".asJsonPrimitive())
274 arrayNodeForList3.add(childNode)
275 childNode = JacksonUtils.objectMapper.createObjectNode()
276 childNode.set("port", "3333".asJsonPrimitive())
277 childNode.set("ip", "1.2.3.3".asJsonPrimitive())
278 arrayNodeForList3.add(childNode)
279 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
281 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
282 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
283 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
284 IpAddress("3333", "1.2.3.3")
287 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
288 IpAddress("1111", "1.2.3.1"),
289 IpAddress("2222", "1.2.3.2"),
290 IpAddress("3333", "1.2.3.3")
294 private fun initInputMapAndExpectedValuesForComplexType() {
295 val mapOfComplexType = mutableMapOf<String, JsonNode>()
296 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
297 mapOfComplexType["port"] = "8888".asJsonType()
298 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
299 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
301 val objectNode = JacksonUtils.objectMapper.createObjectNode()
302 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
303 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
305 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
306 childNode1.set("name", "my-ipAddress".asJsonPrimitive())
307 childNode1.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
308 childNode1.set("port", "8888".asJsonType())
309 childNode1.set("something", IpAddress("2222", "1.2.3.1").asJsonType())
310 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
312 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
313 childNode2.set("name", "my-ipAddress".asJsonPrimitive())
314 childNode2.set("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
315 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
318 private fun prepareResponseNodeForTest(
319 dictionary_source: String,
325 val resourceAssignment = when (sourceType) {
327 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
330 prepareRADataDictionaryOfPrimaryType(dictionary_source)
333 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
337 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
338 "Failed to get database query result into Json node."
341 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
343 return ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment, resourceAssignmentRuntimeService, outputKeyMapping)
346 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
347 return ResourceAssignment().apply {
349 dictionaryName = "sample-ip"
350 dictionarySource = "$dictionary_source"
351 property = PropertyDefinition().apply {
357 private fun prepareRADataDictionaryCollection(dictionary_source: String, sourceType: String, schema: String): ResourceAssignment {
358 return ResourceAssignment().apply {
359 name = "ipAddress-list"
360 dictionaryName = "sample-licenses"
361 dictionarySource = "$dictionary_source"
362 property = PropertyDefinition().apply {
364 entrySchema = EntrySchema().apply {
371 private fun prepareRADataDictionaryComplexType(dictionary_source: String, sourceType: String, schema: String): ResourceAssignment {
372 return ResourceAssignment().apply {
373 name = "ipAddress-complexType"
374 dictionaryName = "sample-licenses"
375 dictionarySource = "$dictionary_source"
376 property = PropertyDefinition().apply {
382 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
383 val outputMapping = mutableMapOf<String, String>()
385 when (dictionary_source) {
386 "sample-key-value", "sample-value" -> {
388 if (dictionary_source == "sample-key-value")
389 outputMapping["sample-ip"] = "value"
391 "listOfString", "mapOfString" -> {
393 outputMapping["ip"] = "value"
395 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
396 // List or map of complex Type
397 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
398 outputMapping["ipAddress"] = "value"
400 outputMapping["port"] = "port"
401 outputMapping["ip"] = "ip"
406 if (dictionary_source == "complexTypeOneKeys")
407 outputMapping["host"] = "value"
409 outputMapping["name"] = "name"
410 outputMapping["ipAddress"] = "ipAddress"