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.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
55 * Unit test cases for abstract component function.
57 @RunWith(SpringRunner::class)
58 @ContextConfiguration(
59 classes = [ComponentFunctionScriptingService::class,
60 BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
61 BlueprintJythonService::class]
63 class AbstractComponentFunctionTest {
65 lateinit var blueprintContext: BluePrintContext
68 lateinit var compSvc: ComponentFunctionScriptingService
72 blueprintContext = mockk<BluePrintContext>()
73 every { blueprintContext.rootPath } returns normalizedPathName("target")
75 blueprintContext.nodeTemplateOperationImplementation(
78 } returns Implementation()
82 fun testAbstractComponent() {
84 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
85 val samp = SampleComponent()
86 val comp = samp as AbstractComponentFunction
88 comp.bluePrintRuntimeService = bluePrintRuntime
89 comp.stepName = "sample-step"
90 assertNotNull(comp, "failed to get kotlin instance")
92 val input = getMockedInput(bluePrintRuntime)
94 val output = comp.applyNB(input)
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")
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
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")
117 fun testAbstractScriptComponent() {
119 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
120 val samp = SampleRestconfComponent(compSvc)
121 val comp = samp as AbstractComponentFunction
123 comp.bluePrintRuntimeService = bluePrintRuntime
124 comp.stepName = "sample-step"
125 assertNotNull(comp, "failed to get kotlin instance")
127 val input = getMockedInput(bluePrintRuntime)
128 val inp = getMockedContext()
130 val output = comp.applyNB(input)
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")
140 * Mocked input for abstract function test.
142 private fun getMockedContext() {
143 val operationOutputs = hashMapOf<String, JsonNode>()
145 blueprintContext.name()
146 } returns "SampleTest"
148 blueprintContext.version()
149 } returns "SampleScriptComponent"
153 * Mocked input for abstract function test.
155 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
156 ExecutionServiceInput {
158 val mapper = ObjectMapper()
159 val rootNode = mapper.createObjectNode()
160 rootNode.put("ip-address", "0.0.0.0")
161 rootNode.put("type", "rest")
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
172 val stepInputData = StepData().apply {
173 name = "activate-restconf"
174 properties = operationInputs
176 val executionServiceInput = ExecutionServiceInput().apply {
177 commonHeader = CommonHeader().apply {
180 actionIdentifiers = ActionIdentifiers().apply {
181 actionName = "activate"
183 payload = JacksonUtils.jsonNode("{}") as ObjectNode
185 executionServiceInput.stepData = stepInputData
188 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
189 "activate-restconf", "interfaceName", "operationName"
191 } returns operationInputs
193 val operationOutputs = hashMapOf<String, JsonNode>()
195 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
196 "activate-restconf", "interfaceName", "operationName"
198 } returns operationOutputs
199 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
201 return executionServiceInput
205 fun testComponentScriptExecutorNodeType() {
206 val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
207 assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")