Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / utils / ResourceAssignmentUtilsTest.kt
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils
23
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.node.TextNode
26 import io.mockk.every
27 import io.mockk.spyk
28 import org.junit.Before
29 import org.junit.Test
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
40
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)
45
46 class ResourceAssignmentUtilsTest {
47     private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
48
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
62
63     @Before
64     fun setup() {
65
66         val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
67             "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
68         )
69
70         resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
71
72         // Init input map and expected values for tests
73         initInputMapAndExpectedValuesForPrimitiveType()
74         initInputMapAndExpectedValuesForCollection()
75         initInputMapAndExpectedValuesForComplexType()
76
77         val propertiesDefinition1 = PropertyDefinition().apply {
78             type = "string"
79             id = "port"
80         }
81
82         val propertiesDefinition2 = PropertyDefinition().apply {
83             type = "string"
84             id = "ip"
85         }
86
87         val propertiesDefinition3 = PropertyDefinition().apply {
88             type = "string"
89             id = "name"
90         }
91
92         val propertiesDefinition4 = PropertyDefinition().apply {
93             type = "ip-address"
94             id = "ipAddress"
95         }
96
97         var mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
98         mapOfPropertiesIpAddress["port"] = propertiesDefinition1
99         mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
100
101         var mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
102         mapOfPropertiesHost["name"] = propertiesDefinition3
103         mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
104
105         val myDataTypeIpaddress = DataType().apply {
106             id = "ip-address"
107             properties = mapOfPropertiesIpAddress
108         }
109
110         val myDataTypeHost = DataType().apply {
111             id = "host"
112             properties = mapOfPropertiesHost
113         }
114
115         every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address") } returns myDataTypeIpaddress
116
117         every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
118
119         every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
120     }
121
122     @Test
123     fun `generateResourceDataForAssignments - positive test`() {
124         // given a valid resource assignment
125         val validResourceAssignment = createResourceAssignmentForTest("valid_value")
126
127         // and a list containing that resource assignment
128         val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
129
130         // when the values of the resources are evaluated
131         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
132
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")
136     }
137
138     @Test
139     fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
140         // given a valid resource assignment
141         val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
142
143         // and a list containing that resource assignment
144         val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
145
146         // when the values of the resources are evaluated
147         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
148
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")
152     }
153
154     private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
155         val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
156         val resourceAssignmentForTest = ResourceAssignment().apply {
157             name = "pnf-id"
158             dictionaryName = "pnf-id"
159             dictionarySource = "input"
160             property = PropertyDefinition().apply {
161                 type = "string"
162                 value = valueForTest
163             }
164         }
165         return resourceAssignmentForTest
166     }
167
168     @Test
169     fun parseResponseNodeTestForPrimitivesTypes() {
170         var outcome = prepareResponseNodeForTest(
171             "sample-value", "string", "",
172             inputMapToTestPrimitiveTypeWithValue
173         )
174         assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of simple String")
175
176         outcome = prepareResponseNodeForTest(
177             "sample-key-value", "string", "",
178             inputMapToTestPrimitiveTypeWithKeyValue
179         )
180         assertEquals(expectedValueToTestPrimitiveType, outcome, "Unexpected outcome returned for primitive type of key-value String")
181     }
182
183     @Test
184     fun parseResponseNodeTestForCollectionsOfString() {
185         var outcome = prepareResponseNodeForTest(
186             "listOfString", "list",
187             "string", inputMapToTestCollectionOfPrimitiveType
188         )
189         assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for list of String")
190
191         outcome = prepareResponseNodeForTest(
192             "mapOfString", "map", "string",
193             inputMapToTestCollectionOfPrimitiveType
194         )
195         assertEquals(expectedValueToTesCollectionOfPrimitiveType, outcome, "unexpected outcome returned for map of String")
196     }
197
198     @Test
199     fun parseResponseNodeTestForCollectionsOfComplexType() {
200         var outcome = prepareResponseNodeForTest(
201             "listOfMyDataTypeWithOneOutputKeyMapping", "list",
202             "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
203         )
204         assertEquals(expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
205
206         outcome = prepareResponseNodeForTest(
207             "listOfMyDataTypeWithAllOutputKeyMapping", "list",
208             "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
209         )
210         assertEquals(expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping, outcome, "unexpected outcome returned for list of String")
211     }
212
213     @Test
214     fun `parseResponseNodeTestForComplexType find one output key mapping`() {
215         val outcome = prepareResponseNodeForTest(
216             "complexTypeOneKeys", "host",
217             "", inputMapToTestComplexTypeWithOneOutputKeyMapping
218         )
219         assertEquals(expectedValueToTestComplexTypeWithOneOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
220     }
221
222     @Test
223     fun `parseResponseNodeTestForComplexType find all output key mapping`() {
224         val outcome = prepareResponseNodeForTest(
225             "complexTypeAllKeys", "host",
226             "", inputMapToTestComplexTypeWithAllOutputKeyMapping
227         )
228         assertEquals(expectedValueToTestComplexTypeWithAllOutputKeyMapping, outcome, "Unexpected outcome returned for complex type")
229     }
230
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")
237     }
238
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()
242         listOfIps.forEach {
243             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
244             arrayChildNode.set("value", it.asJsonPrimitive())
245             arrayNodeForList1.add(arrayChildNode)
246         }
247         inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
248
249         expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
250             ExpectedResponseIp("1.2.3.1"),
251             ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
252         ).asJsonType()
253
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()
257         )
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)
263         }
264         inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
265
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
280
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")
285             )
286         ).asJsonType()
287         expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
288             IpAddress("1111", "1.2.3.1"),
289             IpAddress("2222", "1.2.3.2"),
290             IpAddress("3333", "1.2.3.3")
291         ).asJsonType()
292     }
293
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()
300
301         val objectNode = JacksonUtils.objectMapper.createObjectNode()
302         expectedValueToTestComplexTypeWithOneOutputKeyMapping =
303             objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
304
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
311
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
316     }
317
318     private fun prepareResponseNodeForTest(
319         dictionary_source: String,
320         sourceType: String,
321         entrySchema: String,
322         response: Any
323     ): JsonNode {
324
325         val resourceAssignment = when (sourceType) {
326             "list", "map" -> {
327                 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
328             }
329             "string" -> {
330                 prepareRADataDictionaryOfPrimaryType(dictionary_source)
331             }
332             else -> {
333                 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
334             }
335         }
336
337         val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
338             "Failed to get database query result into Json node."
339         }
340
341         val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
342
343         return ResourceAssignmentUtils.parseResponseNode(responseNode, resourceAssignment, resourceAssignmentRuntimeService, outputKeyMapping)
344     }
345
346     private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
347         return ResourceAssignment().apply {
348             name = "ipAddress"
349             dictionaryName = "sample-ip"
350             dictionarySource = "$dictionary_source"
351             property = PropertyDefinition().apply {
352                 type = "string"
353             }
354         }
355     }
356
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 {
363                 type = "$sourceType"
364                 entrySchema = EntrySchema().apply {
365                     type = "$schema"
366                 }
367             }
368         }
369     }
370
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 {
377                 type = "$sourceType"
378             }
379         }
380     }
381
382     private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
383         val outputMapping = mutableMapOf<String, String>()
384
385         when (dictionary_source) {
386             "sample-key-value", "sample-value" -> {
387                 // Primary Type
388                 if (dictionary_source == "sample-key-value")
389                     outputMapping["sample-ip"] = "value"
390             }
391             "listOfString", "mapOfString" -> {
392                 // List of string
393                 outputMapping["ip"] = "value"
394             }
395             "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
396                 // List or map of complex Type
397                 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
398                     outputMapping["ipAddress"] = "value"
399                 else {
400                     outputMapping["port"] = "port"
401                     outputMapping["ip"] = "ip"
402                 }
403             }
404             else -> {
405                 // Complex Type
406                 if (dictionary_source == "complexTypeOneKeys")
407                     outputMapping["host"] = "value"
408                 else {
409                     outputMapping["name"] = "name"
410                     outputMapping["ipAddress"] = "ipAddress"
411                 }
412             }
413         }
414         return outputMapping
415     }
416 }