Add resource definition resolution service.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / 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.cds.blueprintsprocessor.functions.resource.resolution.processor
20
21 import io.mockk.coEvery
22 import io.mockk.every
23 import io.mockk.mockk
24 import kotlinx.coroutines.runBlocking
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceAssignment
29 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
30 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
31 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.PythonExecutorProperty
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
35 import org.onap.ccsdk.cds.controllerblueprints.core.logger
36 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.test.context.ContextConfiguration
44 import org.springframework.test.context.TestPropertySource
45 import org.springframework.test.context.junit4.SpringRunner
46 import kotlin.test.assertEquals
47 import kotlin.test.assertNotNull
48 import kotlin.test.assertTrue
49
50 @RunWith(SpringRunner::class)
51 @ContextConfiguration(classes = [CapabilityResourceResolutionProcessor::class, ComponentFunctionScriptingService::class,
52     BluePrintScriptsServiceImpl::class,
53     BlueprintJythonService::class, PythonExecutorProperty::class, MockCapabilityService::class])
54 @TestPropertySource(properties =
55 ["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints",
56     "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints"])
57 class CapabilityResourceResolutionProcessorTest {
58
59     @Autowired
60     lateinit var capabilityResourceResolutionProcessor: CapabilityResourceResolutionProcessor
61
62     @Test
63     fun `test kotlin capability`() {
64         runBlocking {
65             val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
66             coEvery {
67                 componentFunctionScriptingService
68                         .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
69             } returns MockCapabilityScriptRA()
70
71             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
72             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
73
74             val capabilityResourceResolutionProcessor = CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
75             capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
76
77             val resourceAssignment = BluePrintTypes.resourceAssignment(name = "test-property", dictionaryName = "ra-dict-name",
78                     dictionarySource = "capability") {
79                 property("string", true, "")
80                 sourceCapability {
81                     definedProperties {
82                         type("internal")
83                         scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
84                         keyDependencies(arrayListOf("dep-property"))
85                     }
86                 }
87             }
88             val status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment)
89             assertTrue(status, "failed to execute capability source")
90             assertEquals("assigned-data".asJsonPrimitive(), resourceAssignment.property!!.value,
91                     "assigned value miss match")
92         }
93     }
94
95     @Test
96     fun `test jython capability`() {
97         runBlocking {
98
99             val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
100                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/capability_python")
101
102             val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext)
103
104             capabilityResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService
105
106             val resourceDefinition = JacksonUtils
107                     .readValueFromClassPathFile("mapping/capability/jython-resource-definitions.json",
108                             ResourceDefinition::class.java)!!
109             val resourceDefinitions: MutableMap<String, ResourceDefinition> = mutableMapOf()
110             resourceDefinitions[resourceDefinition.name] = resourceDefinition
111             capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions
112
113             val resourceAssignment = ResourceAssignment().apply {
114                 name = "service-instance-id"
115                 dictionaryName = "service-instance-id"
116                 dictionarySource = "capability"
117                 property = PropertyDefinition().apply {
118                     type = "string"
119                 }
120             }
121
122             val processorName = capabilityResourceResolutionProcessor.processNB(resourceAssignment)
123             assertNotNull(processorName, "couldn't get Jython script resource assignment processor name")
124             println(processorName)
125         }
126     }
127 }
128
129 open class MockCapabilityService {
130
131 }
132
133 open class MockCapabilityScriptRA : ResourceAssignmentProcessor() {
134     val log = logger(MockCapabilityScriptRA::class)
135
136     override fun getName(): String {
137         return "MockCapabilityScriptRA"
138     }
139
140     override suspend fun processNB(executionRequest: ResourceAssignment) {
141         log.info("executing RA mock capability : ${executionRequest.name}")
142         executionRequest.property!!.value = "assigned-data".asJsonPrimitive()
143     }
144
145     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
146         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
147     }
148 }