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