Generate dependency list
[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.data.Implementation
42 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
43 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
44 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
45 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
46 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
47 import org.springframework.beans.factory.annotation.Autowired
48 import org.springframework.test.context.ContextConfiguration
49 import org.springframework.test.context.junit4.SpringRunner
50 import kotlin.test.BeforeTest
51 import kotlin.test.assertEquals
52 import kotlin.test.assertNotNull
53
54 /**
55  * Unit test cases for abstract component function.
56  */
57 @RunWith(SpringRunner::class)
58 @ContextConfiguration(
59     classes = [ComponentFunctionScriptingService::class,
60         BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
61         BlueprintJythonService::class]
62 )
63 class AbstractComponentFunctionTest {
64
65     lateinit var blueprintContext: BluePrintContext
66
67     @Autowired
68     lateinit var compSvc: ComponentFunctionScriptingService
69
70     @BeforeTest
71     fun init() {
72         blueprintContext = mockk<BluePrintContext>()
73         every { blueprintContext.rootPath } returns normalizedPathName("target")
74         every {
75             blueprintContext.nodeTemplateOperationImplementation(
76                 any(), any(), any()
77             )
78         } returns Implementation()
79     }
80
81     @Test
82     fun testAbstractComponent() {
83         runBlocking {
84             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
85             val samp = SampleComponent()
86             val comp = samp as AbstractComponentFunction
87
88             comp.bluePrintRuntimeService = bluePrintRuntime
89             comp.stepName = "sample-step"
90             assertNotNull(comp, "failed to get kotlin instance")
91
92             val input = getMockedInput(bluePrintRuntime)
93
94             val output = comp.applyNB(input)
95
96             assertEquals(output.actionIdentifiers.actionName, "activate")
97             assertEquals(output.commonHeader.requestId, "1234")
98             assertEquals(output.stepData!!.name, "activate-restconf")
99             assertEquals(output.status.message, "success")
100         }
101     }
102
103     @Test
104     fun testComponentFunctionPayload() {
105         val sampleComponent = SampleComponent()
106         sampleComponent.workflowName = "sample-action"
107         sampleComponent.executionServiceInput = JacksonUtils.readValueFromClassPathFile(
108             "payload/requests/sample-execution-request.json", ExecutionServiceInput::class.java
109         )!!
110         val payload = sampleComponent.requestPayload()
111         assertNotNull(payload, "failed to get payload")
112         val data = sampleComponent.requestPayloadActionProperty("data")?.first()
113         assertNotNull(data, "failed to get payload request action data")
114     }
115
116     @Test
117     fun testAbstractScriptComponent() {
118         runBlocking {
119             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
120             val samp = SampleRestconfComponent(compSvc)
121             val comp = samp as AbstractComponentFunction
122
123             comp.bluePrintRuntimeService = bluePrintRuntime
124             comp.stepName = "sample-step"
125             assertNotNull(comp, "failed to get kotlin instance")
126
127             val input = getMockedInput(bluePrintRuntime)
128             val inp = getMockedContext()
129
130             val output = comp.applyNB(input)
131
132             assertEquals(output.actionIdentifiers.actionName, "activate")
133             assertEquals(output.commonHeader.requestId, "1234")
134             assertEquals(output.stepData!!.name, "activate-restconf")
135             assertEquals(output.status.message, "success")
136         }
137     }
138
139     /**
140      * Mocked input for abstract function test.
141      */
142     private fun getMockedContext() {
143         val operationOutputs = hashMapOf<String, JsonNode>()
144         every {
145             blueprintContext.name()
146         } returns "SampleTest"
147         every {
148             blueprintContext.version()
149         } returns "SampleScriptComponent"
150     }
151
152     /**
153      * Mocked input for abstract function test.
154      */
155     private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
156         ExecutionServiceInput {
157
158         val mapper = ObjectMapper()
159         val rootNode = mapper.createObjectNode()
160         rootNode.put("ip-address", "0.0.0.0")
161         rootNode.put("type", "rest")
162
163         val operationInputs = hashMapOf<String, JsonNode>()
164         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
165             "activate-restconf".asJsonPrimitive()
166         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
167             "interfaceName".asJsonPrimitive()
168         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
169             "operationName".asJsonPrimitive()
170         operationInputs["dynamic-properties"] = rootNode
171
172         val stepInputData = StepData().apply {
173             name = "activate-restconf"
174             properties = operationInputs
175         }
176         val executionServiceInput = ExecutionServiceInput().apply {
177             commonHeader = CommonHeader().apply {
178                 requestId = "1234"
179             }
180             actionIdentifiers = ActionIdentifiers().apply {
181                 actionName = "activate"
182             }
183             payload = JacksonUtils.jsonNode("{}") as ObjectNode
184         }
185         executionServiceInput.stepData = stepInputData
186
187         every {
188             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
189                 "activate-restconf", "interfaceName", "operationName"
190             )
191         } returns operationInputs
192
193         val operationOutputs = hashMapOf<String, JsonNode>()
194         every {
195             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
196                 "activate-restconf", "interfaceName", "operationName"
197             )
198         } returns operationOutputs
199         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
200
201         return executionServiceInput
202     }
203
204     @Test
205     fun testComponentScriptExecutorNodeType() {
206         val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
207         assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")
208     }
209 }