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