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