6f961c8ed6cf2d7d2f9adc2581b9856426cdbdbd
[ccsdk/cds.git] /
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.junit.runner.RunWith
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.springframework.test.context.junit4.SpringRunner
28 import kotlin.test.BeforeTest
29 import kotlin.test.assertEquals
30 import kotlin.test.assertNotNull
31
32 @RunWith(SpringRunner::class)
33 class BluePrintTemplateServiceTest {
34
35     lateinit var blueprintRuntime: BluePrintRuntimeService<*>
36
37     @BeforeTest
38     fun setup() {
39         val blueprintBasePath: String = ("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
40         blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
41     }
42
43     @Test
44     fun testVelocityGeneratedContent() {
45         runBlocking {
46             val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl")
47             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json")
48
49             val content = BluePrintVelocityTemplateService.generateContent(template, json)
50             assertNotNull(content, "failed to generate content for velocity template")
51         }
52
53     }
54
55     @Test
56     fun testJinjaGeneratedContent() {
57         runBlocking {
58             val template = JacksonUtils.getClassPathFileContent("templates/base-config-jinja-template.jinja")
59             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-jinja.json")
60
61             var element: MutableMap<String, Any> = mutableMapOf()
62             element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1"))
63
64             val content = BluePrintJinjaTemplateService.generateContent(template, json, false, element)
65             assertNotNull(content, "failed to generate content for velocity template")
66         }
67
68     }
69
70     @Test
71     fun testVelocityGeneratedContentFromFiles() {
72         runBlocking {
73             val bluePrintTemplateService = BluePrintTemplateService()
74             val templateFile = "templates/base-config-velocity-template.vtl"
75             val jsonFile = "templates/base-config-data-velocity.json"
76
77             val content = bluePrintTemplateService.generateContentFromFiles(
78                     templateFile, BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf())
79             assertNotNull(content, "failed to generate content for velocity template")
80         }
81
82     }
83
84     @Test
85     fun testJinjaGeneratedContentFromFiles() {
86         runBlocking {
87             var element: MutableMap<String, Any> = mutableMapOf()
88             element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1"))
89
90             val bluePrintTemplateService = BluePrintTemplateService()
91
92             val templateFile = "templates/base-config-jinja-template.jinja"
93             val jsonFile = "templates/base-config-data-jinja.json"
94
95             val content = bluePrintTemplateService.generateContentFromFiles(
96                     templateFile, BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME,
97                     jsonFile, false, element)
98             assertNotNull(content, "failed to generate content for velocity template")
99         }
100     }
101
102     @Test
103     fun `no value variable should evaluate to default value - standalone template mesh test`() {
104         runBlocking {
105             val template = JacksonUtils.getClassPathFileContent("templates/default-variable-value-velocity-template.vtl")
106             val json = JacksonUtils.getClassPathFileContent("templates/default-variable-value-data.json")
107
108             val content = BluePrintVelocityTemplateService.generateContent(template, json)
109             //first line represents a variable whose value was successfully retrieved, second line contains a variable
110             // whose value could not be evaluated
111             val expected = "sample-hostname\n\${node0_backup_router_address}"
112             assertEquals(expected, content, "No value variable should use default value")
113         }
114     }
115
116     @Test
117     fun `no value variable should evaluate to default value - blueprint processing test`() {
118         runBlocking {
119             val bluePrintTemplateService = BluePrintTemplateService()
120
121             val templateFile = "templates/default-variable-value-velocity-template.vtl"
122             val jsonFile = "templates/default-variable-value-data.json"
123
124             val content = bluePrintTemplateService.generateContentFromFiles(templateFile,
125                     BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf())
126
127             //first line represents a variable whose value was successfully retrieved, second line contains a variable
128             // whose value could not be evaluated
129             val expected = "sample-hostname\n\${node0_backup_router_address}"
130             assertEquals(expected, content, "No value variable should use default value")
131         }
132
133     }
134
135 }
136