Enhancing BluePrintJinjaTemplateService
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintTemplateServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Modifications Copyright © 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
19 package org.onap.ccsdk.cds.controllerblueprints.core.service
20
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.TestConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
26 import kotlin.test.BeforeTest
27 import kotlin.test.assertEquals
28 import kotlin.test.assertNotNull
29
30 class BluePrintTemplateServiceTest {
31
32     lateinit var blueprintRuntime: BluePrintRuntimeService<*>
33
34     @BeforeTest
35     fun setup() {
36         val blueprintBasePath = TestConstants.PATH_TEST_BLUEPRINTS_BASECONFIG
37         blueprintRuntime = BluePrintMetadataUtils.bluePrintRuntime("1234", blueprintBasePath)
38     }
39
40     @Test
41     fun testVelocityGeneratedContent() {
42         runBlocking {
43             val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl")
44             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json")
45
46             val content = BluePrintVelocityTemplateService.generateContent(template, json)
47             assertNotNull(content, "failed to generate content for velocity template")
48         }
49     }
50
51     @Test
52     fun testJinjaGeneratedContent() {
53         runBlocking {
54             val template = JacksonUtils.getClassPathFileContent("templates/master.jinja")
55             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-jinja.json")
56
57             val element: MutableMap<String, Any> = mutableMapOf()
58             element["additional_array"] = arrayListOf(
59                 hashMapOf("name" to "Element1", "location" to "Region0"),
60                 hashMapOf("name" to "Element2", "location" to "Region1")
61             )
62
63             val content = BluePrintJinjaTemplateService.generateContent(template, json, false, element)
64             assertNotNull(content, "failed to generate content for jinja template")
65         }
66     }
67
68     @Test
69     fun `Unresolved variable should be kept as-is - standalone velocity template mesh test`() {
70         runBlocking {
71             val template =
72                 JacksonUtils.getClassPathFileContent("templates/default-variable-value-velocity-template.vtl")
73             val json = JacksonUtils.getClassPathFileContent("templates/default-variable-value-data.json")
74
75             val content = BluePrintVelocityTemplateService.generateContent(template, json)
76             // first line represents a variable whose value was successfully retrieved,
77             // second line contains a variable whose value could not be evaluated
78             val expected = "sample-node0_hostname\n\${node0_backup_router_address}"
79             assertEquals(expected, content, "No value variable should use default value")
80         }
81     }
82
83     @Test
84     fun `Unresolved variable should be kept as-is - standalone jinja template mesh test`() {
85         runBlocking {
86             val context = JacksonUtils.getClassPathFileContent("templates/default-variable-value-data.json")
87             val jinjaTemplate =
88                 JacksonUtils.getClassPathFileContent("templates/default-variable-jinja-template.jinja")
89             val renderedContent =
90                 BluePrintJinjaTemplateService.generateContent(jinjaTemplate, context, false, mutableMapOf())
91             val expectedContent =
92                 JacksonUtils.getClassPathFileContent("templates/default-variable-jinja-template-resolved.jinja")
93             assertEquals(expectedContent, renderedContent, "No value variable should use default value")
94         }
95     }
96 }