2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts;
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import com.fasterxml.jackson.databind.node.ObjectNode
28 import kotlinx.coroutines.runBlocking
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
54 * Unit test cases for abstract component function.
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(classes = [ComponentFunctionScriptingService::class,
58 BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
59 BlueprintJythonService::class])
60 class AbstractComponentFunctionTest {
62 lateinit var blueprintContext: BluePrintContext
65 lateinit var compSvc: ComponentFunctionScriptingService
69 blueprintContext = mockk<BluePrintContext>()
70 every { blueprintContext.rootPath } returns normalizedPathName("target")
74 fun testAbstractComponent() {
76 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
77 val samp = SampleComponent()
78 val comp = samp as AbstractComponentFunction
80 comp.bluePrintRuntimeService = bluePrintRuntime
81 comp.stepName = "sample-step"
82 assertNotNull(comp, "failed to get kotlin instance")
84 val input = getMockedInput(bluePrintRuntime)
86 val output = comp.applyNB(input)
88 assertEquals(output.actionIdentifiers.actionName, "activate")
89 assertEquals(output.commonHeader.requestId, "1234")
90 assertEquals(output.stepData!!.name, "activate-restconf")
91 assertEquals(output.status.message, "success")
96 fun testComponentFunctionPayload() {
97 val sampleComponent = SampleComponent()
98 sampleComponent.workflowName = "sample-action"
99 sampleComponent.executionServiceInput = JacksonUtils.readValueFromClassPathFile(
100 "payload/requests/sample-execution-request.json", ExecutionServiceInput::class.java)!!
101 val payload = sampleComponent.requestPayload()
102 assertNotNull(payload, "failed to get payload")
103 val data = sampleComponent.requestPayloadActionProperty("data")?.first()
104 assertNotNull(data, "failed to get payload request action data")
108 fun testAbstractScriptComponent() {
110 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
111 val samp = SampleRestconfComponent(compSvc)
112 val comp = samp as AbstractComponentFunction
114 comp.bluePrintRuntimeService = bluePrintRuntime
115 comp.stepName = "sample-step"
116 assertNotNull(comp, "failed to get kotlin instance")
118 val input = getMockedInput(bluePrintRuntime)
119 val inp = getMockedContext()
121 val output = comp.applyNB(input)
123 assertEquals(output.actionIdentifiers.actionName, "activate")
124 assertEquals(output.commonHeader.requestId, "1234")
125 assertEquals(output.stepData!!.name, "activate-restconf")
126 assertEquals(output.status.message, "success")
131 * Mocked input for abstract function test.
133 private fun getMockedContext() {
134 val operationOutputs = hashMapOf<String, JsonNode>()
136 blueprintContext.name()
137 } returns "SampleTest"
139 blueprintContext.version()
140 } returns "SampleScriptComponent"
144 * Mocked input for abstract function test.
146 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
147 ExecutionServiceInput {
149 val mapper = ObjectMapper()
150 val rootNode = mapper.createObjectNode()
151 rootNode.put("ip-address", "0.0.0.0")
152 rootNode.put("type", "rest")
154 val operationInputs = hashMapOf<String, JsonNode>()
155 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
156 "activate-restconf".asJsonPrimitive()
157 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
158 "interfaceName".asJsonPrimitive()
159 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
160 "operationName".asJsonPrimitive()
161 operationInputs["dynamic-properties"] = rootNode
164 val stepInputData = StepData().apply {
165 name = "activate-restconf"
166 properties = operationInputs
168 val executionServiceInput = ExecutionServiceInput().apply {
169 commonHeader = CommonHeader().apply {
172 actionIdentifiers = ActionIdentifiers().apply {
173 actionName = "activate"
175 payload = JacksonUtils.jsonNode("{}") as ObjectNode
177 executionServiceInput.stepData = stepInputData
180 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
181 "activate-restconf", "interfaceName", "operationName")
182 } returns operationInputs
184 val operationOutputs = hashMapOf<String, JsonNode>()
186 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
187 "activate-restconf", "interfaceName", "operationName")
188 } returns operationOutputs
189 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
191 return executionServiceInput
195 fun testComponentScriptExecutorNodeType() {
196 val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
197 assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")