Improve service template access through cache.
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / scripts / BluePrintScriptsServiceImplTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
19
20
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintDefinitions
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
26 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
27 import kotlin.script.experimental.jvm.util.classpathFromClass
28 import kotlin.script.experimental.jvm.util.classpathFromClassloader
29 import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty
30 import kotlin.test.assertNotNull
31
32 class BluePrintScriptsServiceImplTest {
33
34     private fun viewClassPathInfo() {
35
36         println(" *********** classpathFromClass  *********** ")
37         classpathFromClass(BluePrintScriptsServiceImplTest::class.java.classLoader,
38                 BluePrintScriptsServiceImplTest::class)!!
39                 .forEach(::println)
40
41         println(" *********** classpathFromClassloader  *********** ")
42         classpathFromClassloader(BluePrintScriptsServiceImplTest::class.java.classLoader)!!
43                 .forEach(::println)
44
45         println(" *********** classpathFromClasspathProperty  *********** ")
46         classpathFromClasspathProperty()!!
47                 .forEach(::println)
48     }
49
50     @Test
51     fun testCachedService() {
52         runBlocking {
53
54             val bluePrintScriptsService = BluePrintScriptsServiceImpl()
55
56             val basePath = normalizedPathName("src/test/resources/compile")
57             /** Load the Definitions */
58             val bluePrintDefinitions = bluePrintScriptsService
59                     .scriptInstance<BluePrintDefinitions>(basePath,
60                             "cba.scripts.ActivateBlueprintDefinitions", true)
61             assertNotNull(bluePrintDefinitions, "failed to get blueprint definitions")
62
63             val serviceTemplate = bluePrintDefinitions.serviceTemplate()
64             assertNotNull(serviceTemplate, "failed to get service template")
65
66             val customDataType = bluePrintDefinitions.otherDefinition<DataType>("datatype-custom-datatype")
67             assertNotNull(customDataType, "failed to get custom definitions")
68
69             val instance = bluePrintScriptsService
70                     .scriptInstance<BlueprintFunctionNode<String, String>>(basePath,
71                             "cba.scripts.SampleBlueprintFunctionNode", false)
72             assertNotNull(instance, "failed to get compiled instance")
73
74             val cachedInstance = bluePrintScriptsService
75                     .scriptInstance<BlueprintFunctionNode<String, String>>(basePath,
76                             "cba.scripts.SampleBlueprintFunctionNode", false)
77             assertNotNull(cachedInstance, "failed to get cached compile instance")
78         }
79     }
80
81 }