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.controllerblueprints.core.BluePrintConstants
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
39 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.onap.ccsdk.cds.controllerblueprints.scripts.BluePrintScriptsServiceImpl
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.junit4.SpringRunner
47 import kotlin.test.BeforeTest
48 import kotlin.test.assertEquals
49 import kotlin.test.assertNotNull
52 * Unit test cases for abstract component function.
54 @RunWith(SpringRunner::class)
55 @ContextConfiguration(classes = [ComponentFunctionScriptingService::class,
56 BluePrintScriptsServiceImpl::class,PythonExecutorProperty::class,
57 BlueprintJythonService::class])
58 class AbstractComponentFunctionTest {
60 lateinit var blueprintContext: BluePrintContext
63 lateinit var compSvc: ComponentFunctionScriptingService
67 blueprintContext = mockk<BluePrintContext>()
68 every { blueprintContext.rootPath } returns normalizedPathName("target")
72 * Tests the abstract component functionality.
75 fun testAbstractComponent() {
77 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
78 val samp = SampleComponent()
79 val comp = samp as AbstractComponentFunction
81 comp.bluePrintRuntimeService = bluePrintRuntime
82 comp.stepName = "sample-step"
83 assertNotNull(comp, "failed to get kotlin instance")
85 val input = getMockedInput(bluePrintRuntime)
87 val output = comp.applyNB(input)
89 assertEquals(output.actionIdentifiers.actionName, "activate")
90 assertEquals(output.commonHeader.requestId, "1234")
91 assertEquals(output.stepData!!.name, "activate-restconf")
92 assertEquals(output.status.message, "success")
97 * Tests the abstract script component functionality.
100 fun testAbstractScriptComponent() {
102 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
103 val samp = SampleRestconfComponent(compSvc)
104 val comp = samp as AbstractComponentFunction
106 comp.bluePrintRuntimeService = bluePrintRuntime
107 comp.stepName = "sample-step"
108 assertNotNull(comp, "failed to get kotlin instance")
110 val input = getMockedInput(bluePrintRuntime)
111 val inp = getMockedContext()
113 val output = comp.applyNB(input)
115 assertEquals(output.actionIdentifiers.actionName, "activate")
116 assertEquals(output.commonHeader.requestId, "1234")
117 assertEquals(output.stepData!!.name, "activate-restconf")
118 assertEquals(output.status.message, "success")
123 * Mocked input for abstract function test.
125 private fun getMockedContext() {
126 val operationOutputs = hashMapOf<String, JsonNode>()
128 blueprintContext.name()
129 } returns "SampleTest"
131 blueprintContext.version()
132 } returns "SampleScriptComponent"
136 * Mocked input for abstract function test.
138 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
139 ExecutionServiceInput {
141 val mapper = ObjectMapper()
142 val rootNode = mapper.createObjectNode()
143 rootNode.put("ip-address", "0.0.0.0")
144 rootNode.put("type", "rest")
146 val operationInputs = hashMapOf<String, JsonNode>()
147 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
148 "activate-restconf".asJsonPrimitive()
149 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
150 "interfaceName".asJsonPrimitive()
151 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
152 "operationName".asJsonPrimitive()
153 operationInputs["dynamic-properties"] = rootNode
156 val stepInputData = StepData().apply {
157 name = "activate-restconf"
158 properties = operationInputs
160 val executionServiceInput = ExecutionServiceInput().apply {
161 commonHeader = CommonHeader().apply {
164 actionIdentifiers = ActionIdentifiers().apply {
165 actionName = "activate"
167 payload = JacksonUtils.jsonNode("{}") as ObjectNode
169 executionServiceInput.stepData = stepInputData
172 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
173 "activate-restconf", "interfaceName", "operationName")
174 } returns operationInputs
176 val operationOutputs = hashMapOf<String, JsonNode>()
178 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
179 "activate-restconf", "interfaceName", "operationName")
180 } returns operationOutputs
181 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
183 return executionServiceInput