07be8c8094a214b1032d15b5506b205c01230e5b
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / scripts / AbstractComponentFunctionTest.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts;
22
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import com.fasterxml.jackson.databind.node.ObjectNode
26 import io.mockk.every
27 import io.mockk.mockk
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
35 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
36 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
37 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.nodeTypeComponentScriptExecutor
38 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
39 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
40 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
41 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
42 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
43 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
44 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
45 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
46 import org.springframework.beans.factory.annotation.Autowired
47 import org.springframework.test.context.ContextConfiguration
48 import org.springframework.test.context.junit4.SpringRunner
49 import kotlin.test.BeforeTest
50 import kotlin.test.assertEquals
51 import kotlin.test.assertNotNull
52
53 /**
54  * Unit test cases for abstract component function.
55  */
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(classes = [ComponentFunctionScriptingService::class,
58     BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
59     BlueprintJythonService::class])
60 class AbstractComponentFunctionTest {
61
62     lateinit var blueprintContext: BluePrintContext
63
64     @Autowired
65     lateinit var compSvc: ComponentFunctionScriptingService
66
67     @BeforeTest
68     fun init() {
69         blueprintContext = mockk<BluePrintContext>()
70         every { blueprintContext.rootPath } returns normalizedPathName("target")
71     }
72
73     /**
74      * Tests the abstract component functionality.
75      */
76     @Test
77     fun testAbstractComponent() {
78         runBlocking {
79             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
80             val samp = SampleComponent()
81             val comp = samp as AbstractComponentFunction
82
83             comp.bluePrintRuntimeService = bluePrintRuntime
84             comp.stepName = "sample-step"
85             assertNotNull(comp, "failed to get kotlin instance")
86
87             val input = getMockedInput(bluePrintRuntime)
88
89             val output = comp.applyNB(input)
90
91             assertEquals(output.actionIdentifiers.actionName, "activate")
92             assertEquals(output.commonHeader.requestId, "1234")
93             assertEquals(output.stepData!!.name, "activate-restconf")
94             assertEquals(output.status.message, "success")
95         }
96     }
97
98     /**
99      * Tests the abstract script component functionality.
100      */
101     @Test
102     fun testAbstractScriptComponent() {
103         runBlocking {
104             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
105             val samp = SampleRestconfComponent(compSvc)
106             val comp = samp as AbstractComponentFunction
107
108             comp.bluePrintRuntimeService = bluePrintRuntime
109             comp.stepName = "sample-step"
110             assertNotNull(comp, "failed to get kotlin instance")
111
112             val input = getMockedInput(bluePrintRuntime)
113             val inp = getMockedContext()
114
115             val output = comp.applyNB(input)
116
117             assertEquals(output.actionIdentifiers.actionName, "activate")
118             assertEquals(output.commonHeader.requestId, "1234")
119             assertEquals(output.stepData!!.name, "activate-restconf")
120             assertEquals(output.status.message, "success")
121         }
122     }
123
124     /**
125      * Mocked input for abstract function test.
126      */
127     private fun getMockedContext() {
128         val operationOutputs = hashMapOf<String, JsonNode>()
129         every {
130             blueprintContext.name()
131         } returns "SampleTest"
132         every {
133             blueprintContext.version()
134         } returns "SampleScriptComponent"
135     }
136
137     /**
138      * Mocked input for abstract function test.
139      */
140     private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
141             ExecutionServiceInput {
142
143         val mapper = ObjectMapper()
144         val rootNode = mapper.createObjectNode()
145         rootNode.put("ip-address", "0.0.0.0")
146         rootNode.put("type", "rest")
147
148         val operationInputs = hashMapOf<String, JsonNode>()
149         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
150                 "activate-restconf".asJsonPrimitive()
151         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
152                 "interfaceName".asJsonPrimitive()
153         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
154                 "operationName".asJsonPrimitive()
155         operationInputs["dynamic-properties"] = rootNode
156
157
158         val stepInputData = StepData().apply {
159             name = "activate-restconf"
160             properties = operationInputs
161         }
162         val executionServiceInput = ExecutionServiceInput().apply {
163             commonHeader = CommonHeader().apply {
164                 requestId = "1234"
165             }
166             actionIdentifiers = ActionIdentifiers().apply {
167                 actionName = "activate"
168             }
169             payload = JacksonUtils.jsonNode("{}") as ObjectNode
170         }
171         executionServiceInput.stepData = stepInputData
172
173         every {
174             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
175                     "activate-restconf", "interfaceName", "operationName")
176         } returns operationInputs
177
178         val operationOutputs = hashMapOf<String, JsonNode>()
179         every {
180             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
181                     "activate-restconf", "interfaceName", "operationName")
182         } returns operationOutputs
183         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
184
185         return executionServiceInput
186     }
187
188     @Test
189     fun testComponentScriptExecutorNodeType() {
190         val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
191         assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")
192     }
193 }
194