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 * Tests the abstract component functionality.
77 fun testAbstractComponent() {
79 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
80 val samp = SampleComponent()
81 val comp = samp as AbstractComponentFunction
83 comp.bluePrintRuntimeService = bluePrintRuntime
84 comp.stepName = "sample-step"
85 assertNotNull(comp, "failed to get kotlin instance")
87 val input = getMockedInput(bluePrintRuntime)
89 val output = comp.applyNB(input)
91 assertEquals(output.actionIdentifiers.actionName, "activate")
92 assertEquals(output.commonHeader.requestId, "1234")
93 assertEquals(output.stepData!!.name, "activate-restconf")
94 assertEquals(output.status.message, "success")
99 * Tests the abstract script component functionality.
102 fun testAbstractScriptComponent() {
104 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
105 val samp = SampleRestconfComponent(compSvc)
106 val comp = samp as AbstractComponentFunction
108 comp.bluePrintRuntimeService = bluePrintRuntime
109 comp.stepName = "sample-step"
110 assertNotNull(comp, "failed to get kotlin instance")
112 val input = getMockedInput(bluePrintRuntime)
113 val inp = getMockedContext()
115 val output = comp.applyNB(input)
117 assertEquals(output.actionIdentifiers.actionName, "activate")
118 assertEquals(output.commonHeader.requestId, "1234")
119 assertEquals(output.stepData!!.name, "activate-restconf")
120 assertEquals(output.status.message, "success")
125 * Mocked input for abstract function test.
127 private fun getMockedContext() {
128 val operationOutputs = hashMapOf<String, JsonNode>()
130 blueprintContext.name()
131 } returns "SampleTest"
133 blueprintContext.version()
134 } returns "SampleScriptComponent"
138 * Mocked input for abstract function test.
140 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
141 ExecutionServiceInput {
143 val mapper = ObjectMapper()
144 val rootNode = mapper.createObjectNode()
145 rootNode.put("ip-address", "0.0.0.0")
146 rootNode.put("type", "rest")
148 val operationInputs = hashMapOf<String, JsonNode>()
149 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
150 "activate-restconf".asJsonPrimitive()
151 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
152 "interfaceName".asJsonPrimitive()
153 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
154 "operationName".asJsonPrimitive()
155 operationInputs["dynamic-properties"] = rootNode
158 val stepInputData = StepData().apply {
159 name = "activate-restconf"
160 properties = operationInputs
162 val executionServiceInput = ExecutionServiceInput().apply {
163 commonHeader = CommonHeader().apply {
166 actionIdentifiers = ActionIdentifiers().apply {
167 actionName = "activate"
169 payload = JacksonUtils.jsonNode("{}") as ObjectNode
171 executionServiceInput.stepData = stepInputData
174 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
175 "activate-restconf", "interfaceName", "operationName")
176 } returns operationInputs
178 val operationOutputs = hashMapOf<String, JsonNode>()
180 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
181 "activate-restconf", "interfaceName", "operationName")
182 } returns operationOutputs
183 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
185 return executionServiceInput
189 fun testComponentScriptExecutorNodeType() {
190 val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
191 assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")