Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-scripts / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / scripts / BlueprintScriptingHostTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.scripts
18
19
20 import org.apache.commons.io.FileUtils
21 import org.junit.Test
22 import java.io.File
23 import kotlin.script.experimental.jvm.util.classpathFromClass
24 import kotlin.script.experimental.jvm.util.classpathFromClassloader
25 import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty
26 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
27
28 class BlueprintScriptingHostTest {
29
30     private fun viewClassPathInfo() {
31
32         println(" *********** classpathFromClass  *********** ")
33         classpathFromClass(BlueprintScriptingHostTest::class.java.classLoader,
34                 BlueprintScriptingHostTest::class)!!
35                 .forEach(::println)
36
37         println(" *********** classpathFromClassloader  *********** ")
38         classpathFromClassloader(BlueprintScriptingHostTest::class.java.classLoader)!!
39                 .forEach(::println)
40
41         println(" *********** classpathFromClasspathProperty  *********** ")
42         classpathFromClasspathProperty()!!
43                 .forEach(::println)
44     }
45
46     @Test
47     fun `test same script two folders`() {
48
49         FileUtils.forceMkdir(File("target/scripts1/"))
50         FileUtils.forceMkdir(File("target/scripts2/"))
51
52         val scriptSource1 = BluePrintSourceCode()
53         scriptSource1.moduleName = "blueprint-test-script"
54
55         scriptSource1.targetJarFile = File("target/scripts1/blueprint-script-generated.jar")
56         val sources1: MutableList<String> = arrayListOf()
57         sources1.add("src/test/resources/scripts1")
58         scriptSource1.blueprintKotlinSources = sources1
59
60         val scriptClassName = "Simple_cba\$SampleComponentFunction"
61
62         val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
63
64         val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
65
66         val scriptSource2 = BluePrintSourceCode()
67         scriptSource2.moduleName = "blueprint-test-script"
68
69         scriptSource2.targetJarFile = File("target/scripts2/blueprint-script-generated.jar")
70         val sources2: MutableList<String> = arrayListOf()
71         sources2.add("src/test/resources/scripts2")
72         scriptSource2.blueprintKotlinSources = sources2
73
74         for (i in 1..2) {
75             val evalResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource1, compilationConfiguration,
76                     null)
77         }
78
79         for (i in 1..2) {
80             val evalResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource2, compilationConfiguration,
81                     null)
82         }
83     }
84 }