Optimize spring data JPA UT.
[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 {
116             resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
117         } returns myDataTypeIpaddress
118
119         every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
120
121         every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
122     }
123
124     @Test
125     fun `generateResourceDataForAssignments - positive test`() {
126         // given a valid resource assignment
127         val validResourceAssignment = createResourceAssignmentForTest("valid_value")
128
129         // and a list containing that resource assignment
130         val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment)
131
132         // when the values of the resources are evaluated
133         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
134
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")
138     }
139
140     @Test
141     fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
142         // given a valid resource assignment
143         val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
144
145         // and a list containing that resource assignment
146         val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
147
148         // when the values of the resources are evaluated
149         val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
150
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")
154     }
155
156     private fun createResourceAssignmentForTest(resourceValue: String?): ResourceAssignment {
157         val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
158         val resourceAssignmentForTest = ResourceAssignment().apply {
159             name = "pnf-id"
160             dictionaryName = "pnf-id"
161             dictionarySource = "input"
162             property = PropertyDefinition().apply {
163                 type = "string"
164                 value = valueForTest
165             }
166         }
167         return resourceAssignmentForTest
168     }
169
170     @Test
171     fun parseResponseNodeTestForPrimitivesTypes() {
172         var outcome = prepareResponseNodeForTest(
173             "sample-value", "string", "",
174             inputMapToTestPrimitiveTypeWithValue
175         )
176         assertEquals(
177             expectedValueToTestPrimitiveType,
178             outcome,
179             "Unexpected outcome returned for primitive type of simple String"
180         )
181
182         outcome = prepareResponseNodeForTest(
183             "sample-key-value", "string", "",
184             inputMapToTestPrimitiveTypeWithKeyValue
185         )
186         assertEquals(
187             expectedValueToTestPrimitiveType,
188             outcome,
189             "Unexpected outcome returned for primitive type of key-value String"
190         )
191     }
192
193     @Test
194     fun parseResponseNodeTestForCollectionsOfString() {
195         var outcome = prepareResponseNodeForTest(
196             "listOfString", "list",
197             "string", inputMapToTestCollectionOfPrimitiveType
198         )
199         assertEquals(
200             expectedValueToTesCollectionOfPrimitiveType,
201             outcome,
202             "unexpected outcome returned for list of String"
203         )
204
205         outcome = prepareResponseNodeForTest(
206             "mapOfString", "map", "string",
207             inputMapToTestCollectionOfPrimitiveType
208         )
209         assertEquals(
210             expectedValueToTesCollectionOfPrimitiveType,
211             outcome,
212             "unexpected outcome returned for map of String"
213         )
214     }
215
216     @Test
217     fun parseResponseNodeTestForCollectionsOfComplexType() {
218         var outcome = prepareResponseNodeForTest(
219             "listOfMyDataTypeWithOneOutputKeyMapping", "list",
220             "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
221         )
222         assertEquals(
223             expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
224             outcome,
225             "unexpected outcome returned for list of String"
226         )
227
228         outcome = prepareResponseNodeForTest(
229             "listOfMyDataTypeWithAllOutputKeyMapping", "list",
230             "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
231         )
232         assertEquals(
233             expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
234             outcome,
235             "unexpected outcome returned for list of String"
236         )
237     }
238
239     @Test
240     fun `parseResponseNodeTestForComplexType find one output key mapping`() {
241         val outcome = prepareResponseNodeForTest(
242             "complexTypeOneKeys", "host",
243             "", inputMapToTestComplexTypeWithOneOutputKeyMapping
244         )
245         assertEquals(
246             expectedValueToTestComplexTypeWithOneOutputKeyMapping,
247             outcome,
248             "Unexpected outcome returned for complex type"
249         )
250     }
251
252     @Test
253     fun `parseResponseNodeTestForComplexType find all output key mapping`() {
254         val outcome = prepareResponseNodeForTest(
255             "complexTypeAllKeys", "host",
256             "", inputMapToTestComplexTypeWithAllOutputKeyMapping
257         )
258         assertEquals(
259             expectedValueToTestComplexTypeWithAllOutputKeyMapping,
260             outcome,
261             "Unexpected outcome returned for complex type"
262         )
263     }
264
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")
271     }
272
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()
276         listOfIps.forEach {
277             val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
278             arrayChildNode.set("value", it.asJsonPrimitive())
279             arrayNodeForList1.add(arrayChildNode)
280         }
281         inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
282
283         expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
284             ExpectedResponseIp("1.2.3.1"),
285             ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
286         ).asJsonType()
287
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()
291         )
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)
297         }
298         inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
299
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
314
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")
319             )
320         ).asJsonType()
321         expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
322             IpAddress("1111", "1.2.3.1"),
323             IpAddress("2222", "1.2.3.2"),
324             IpAddress("3333", "1.2.3.3")
325         ).asJsonType()
326     }
327
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()
334
335         val objectNode = JacksonUtils.objectMapper.createObjectNode()
336         expectedValueToTestComplexTypeWithOneOutputKeyMapping =
337             objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
338
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
345
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
350     }
351
352     private fun prepareResponseNodeForTest(
353         dictionary_source: String,
354         sourceType: String,
355         entrySchema: String,
356         response: Any
357     ): JsonNode {
358
359         val resourceAssignment = when (sourceType) {
360             "list", "map" -> {
361                 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
362             }
363             "string" -> {
364                 prepareRADataDictionaryOfPrimaryType(dictionary_source)
365             }
366             else -> {
367                 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
368             }
369         }
370
371         val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
372             "Failed to get database query result into Json node."
373         }
374
375         val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
376
377         return ResourceAssignmentUtils.parseResponseNode(
378             responseNode,
379             resourceAssignment,
380             resourceAssignmentRuntimeService,
381             outputKeyMapping
382         )
383     }
384
385     private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
386         return ResourceAssignment().apply {
387             name = "ipAddress"
388             dictionaryName = "sample-ip"
389             dictionarySource = "$dictionary_source"
390             property = PropertyDefinition().apply {
391                 type = "string"
392             }
393         }
394     }
395
396     private fun prepareRADataDictionaryCollection(
397         dictionary_source: String,
398         sourceType: String,
399         schema: String
400     ): ResourceAssignment {
401         return ResourceAssignment().apply {
402             name = "ipAddress-list"
403             dictionaryName = "sample-licenses"
404             dictionarySource = "$dictionary_source"
405             property = PropertyDefinition().apply {
406                 type = "$sourceType"
407                 entrySchema = EntrySchema().apply {
408                     type = "$schema"
409                 }
410             }
411         }
412     }
413
414     private fun prepareRADataDictionaryComplexType(
415         dictionary_source: String,
416         sourceType: String,
417         schema: String
418     ): ResourceAssignment {
419         return ResourceAssignment().apply {
420             name = "ipAddress-complexType"
421             dictionaryName = "sample-licenses"
422             dictionarySource = "$dictionary_source"
423             property = PropertyDefinition().apply {
424                 type = "$sourceType"
425             }
426         }
427     }
428
429     private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
430         val outputMapping = mutableMapOf<String, String>()
431
432         when (dictionary_source) {
433             "sample-key-value", "sample-value" -> {
434                 // Primary Type
435                 if (dictionary_source == "sample-key-value")
436                     outputMapping["sample-ip"] = "value"
437             }
438             "listOfString", "mapOfString" -> {
439                 // List of string
440                 outputMapping["ip"] = "value"
441             }
442             "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
443                 // List or map of complex Type
444                 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
445                     outputMapping["ipAddress"] = "value"
446                 else {
447                     outputMapping["port"] = "port"
448                     outputMapping["ip"] = "ip"
449                 }
450             }
451             else -> {
452                 // Complex Type
453                 if (dictionary_source == "complexTypeOneKeys")
454                     outputMapping["host"] = "value"
455                 else {
456                     outputMapping["name"] = "name"
457                     outputMapping["ipAddress"] = "ipAddress"
458                 }
459             }
460         }
461         return outputMapping
462     }
463 }