Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / functions / resource / resolution / processor / CapabilityResourceResolutionProcessorTest.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.apps.blueprintsprocessor.functions.resource.resolution.processor
20
21 import org.junit.Ignore
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
25 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
26 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
27 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.PythonExecutorProperty
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
32 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
33 import org.onap.ccsdk.apps.controllerblueprints.scripts.BluePrintScriptsServiceImpl
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import kotlin.test.assertNotNull
39
40 @RunWith(SpringRunner::class)
41 @ContextConfiguration(classes = [CapabilityResourceResolutionProcessor::class, ComponentFunctionScriptingService::class,
42     BluePrintScriptsServiceImpl::class,
43     BlueprintJythonService::class, PythonExecutorProperty::class, MockCapabilityService::class])
44 @TestPropertySource(properties =
45 ["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints",
46     "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints"])
47 class CapabilityResourceResolutionProcessorTest {
48
49     @Autowired
50     lateinit var capabilityResourceResolutionProcessor: CapabilityResourceResolutionProcessor
51
52     @Ignore
53     @Test
54     fun `test kotlin capability`() {
55
56         val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
57                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
58
59         val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext)
60
61         capabilityResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService
62         capabilityResourceResolutionProcessor.resourceDictionaries = hashMapOf()
63
64
65         val scriptPropertyInstances: MutableMap<String, Any> = mutableMapOf()
66         scriptPropertyInstances["mock-service1"] = MockCapabilityService()
67         scriptPropertyInstances["mock-service2"] = MockCapabilityService()
68
69         val instanceDependencies: List<String> = listOf()
70
71         val resourceAssignmentProcessor = capabilityResourceResolutionProcessor
72                 .scriptInstance("kotlin",
73                         "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", instanceDependencies)
74
75         assertNotNull(resourceAssignmentProcessor, "couldn't get kotlin script resource assignment processor")
76
77         val resourceAssignment = ResourceAssignment().apply {
78             name = "ra-name"
79             dictionaryName = "ra-dict-name"
80             dictionarySource = "capability"
81             property = PropertyDefinition().apply {
82                 type = "string"
83             }
84         }
85
86         val processorName = resourceAssignmentProcessor.apply(resourceAssignment)
87         assertNotNull(processorName, "couldn't get kotlin script resource assignment processor name")
88         println(processorName)
89     }
90
91     @Test
92     fun `test jython capability`() {
93
94         val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
95                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/capability_python")
96
97         val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext)
98
99         capabilityResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService
100
101         val resourceDefinition = JacksonUtils
102                 .readValueFromClassPathFile("mapping/capability/jython-resource-definitions.json",
103                         ResourceDefinition::class.java)!!
104         val resourceDefinitions: MutableMap<String, ResourceDefinition> = mutableMapOf()
105         resourceDefinitions[resourceDefinition.name] = resourceDefinition
106         capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions
107
108         val resourceAssignment = ResourceAssignment().apply {
109             name = "service-instance-id"
110             dictionaryName = "service-instance-id"
111             dictionarySource = "capability"
112             property = PropertyDefinition().apply {
113                 type = "string"
114             }
115         }
116
117         val processorName = capabilityResourceResolutionProcessor.apply(resourceAssignment)
118         assertNotNull(processorName, "couldn't get Jython script resource assignment processor name")
119
120     }
121
122 }
123
124 open class MockCapabilityService {
125
126 }