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(
58 classes = [ComponentFunctionScriptingService::class,
59 BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
60 BlueprintJythonService::class]
62 class AbstractComponentFunctionTest {
64 lateinit var blueprintContext: BluePrintContext
67 lateinit var compSvc: ComponentFunctionScriptingService
71 blueprintContext = mockk<BluePrintContext>()
72 every { blueprintContext.rootPath } returns normalizedPathName("target")
76 fun testAbstractComponent() {
78 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
79 val samp = SampleComponent()
80 val comp = samp as AbstractComponentFunction
82 comp.bluePrintRuntimeService = bluePrintRuntime
83 comp.stepName = "sample-step"
84 assertNotNull(comp, "failed to get kotlin instance")
86 val input = getMockedInput(bluePrintRuntime)
88 val output = comp.applyNB(input)
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")
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
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")
111 fun testAbstractScriptComponent() {
113 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
114 val samp = SampleRestconfComponent(compSvc)
115 val comp = samp as AbstractComponentFunction
117 comp.bluePrintRuntimeService = bluePrintRuntime
118 comp.stepName = "sample-step"
119 assertNotNull(comp, "failed to get kotlin instance")
121 val input = getMockedInput(bluePrintRuntime)
122 val inp = getMockedContext()
124 val output = comp.applyNB(input)
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")
134 * Mocked input for abstract function test.
136 private fun getMockedContext() {
137 val operationOutputs = hashMapOf<String, JsonNode>()
139 blueprintContext.name()
140 } returns "SampleTest"
142 blueprintContext.version()
143 } returns "SampleScriptComponent"
147 * Mocked input for abstract function test.
149 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
150 ExecutionServiceInput {
152 val mapper = ObjectMapper()
153 val rootNode = mapper.createObjectNode()
154 rootNode.put("ip-address", "0.0.0.0")
155 rootNode.put("type", "rest")
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
166 val stepInputData = StepData().apply {
167 name = "activate-restconf"
168 properties = operationInputs
170 val executionServiceInput = ExecutionServiceInput().apply {
171 commonHeader = CommonHeader().apply {
174 actionIdentifiers = ActionIdentifiers().apply {
175 actionName = "activate"
177 payload = JacksonUtils.jsonNode("{}") as ObjectNode
179 executionServiceInput.stepData = stepInputData
182 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
183 "activate-restconf", "interfaceName", "operationName"
185 } returns operationInputs
187 val operationOutputs = hashMapOf<String, JsonNode>()
189 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
190 "activate-restconf", "interfaceName", "operationName"
192 } returns operationOutputs
193 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
195 return executionServiceInput
199 fun testComponentScriptExecutorNodeType() {
200 val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
201 assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")