02505acad462a7c8dc0f80d9816230acd7acf7f6
[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.assertNotNull
30
31 @RunWith(SpringRunner::class)
32 class BluePrintTemplateServiceTest {
33
34     lateinit var blueprintRuntime: BluePrintRuntimeService<*>
35
36     @BeforeTest
37     fun setup() {
38         val blueprintBasePath: String = ("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
39         blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
40     }
41
42     @Test
43     fun testVelocityGeneratedContent() {
44         runBlocking {
45             val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl")
46             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json")
47
48             val content = BluePrintVelocityTemplateService.generateContent(template, json)
49             assertNotNull(content, "failed to generate content for velocity template")
50         }
51
52     }
53
54     @Test
55     fun testJinjaGeneratedContent() {
56         runBlocking {
57             val template = JacksonUtils.getClassPathFileContent("templates/base-config-jinja-template.jinja")
58             val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-jinja.json")
59
60             var element: MutableMap<String, Any> = mutableMapOf()
61             element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1"))
62
63             val content = BluePrintJinjaTemplateService.generateContent(template, json, false, element)
64             assertNotNull(content, "failed to generate content for velocity template")
65         }
66
67     }
68
69     @Test
70     fun testVelocityGeneratedContentFromFiles() {
71         runBlocking {
72             val bluePrintTemplateService = BluePrintTemplateService()
73             val templateFile = "templates/base-config-velocity-template.vtl"
74             val jsonFile = "templates/base-config-data-velocity.json"
75
76             val content = bluePrintTemplateService.generateContentFromFiles(
77                     templateFile, BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf())
78             assertNotNull(content, "failed to generate content for velocity template")
79         }
80
81     }
82
83     @Test
84     fun testJinjaGeneratedContentFromFiles() {
85         runBlocking {
86             var element: MutableMap<String, Any> = mutableMapOf()
87             element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1"))
88
89             val bluePrintTemplateService = BluePrintTemplateService()
90
91             val templateFile = "templates/base-config-jinja-template.jinja"
92             val jsonFile = "templates/base-config-data-jinja.json"
93
94             val content = bluePrintTemplateService.generateContentFromFiles(
95                     templateFile, BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME,
96                     jsonFile, false, element)
97             assertNotNull(content, "failed to generate content for velocity template")
98         }
99     }
100 }
101