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, DeprecatedBlueprintJythonService::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")
74 blueprintContext.nodeTemplateOperationImplementation(
77 } returns Implementation()
81 fun testAbstractComponent() {
83 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
84 val samp = SampleComponent()
85 val comp = samp as AbstractComponentFunction
87 comp.bluePrintRuntimeService = bluePrintRuntime
88 comp.stepName = "sample-step"
89 assertNotNull(comp, "failed to get kotlin instance")
91 val input = getMockedInput(bluePrintRuntime)
93 val output = comp.applyNB(input)
95 assertEquals(output.actionIdentifiers.actionName, "activate")
96 assertEquals(output.commonHeader.requestId, "1234")
97 assertEquals(output.stepData!!.name, "activate-restconf")
98 assertEquals(output.status.message, "success")
103 fun testComponentFunctionPayload() {
104 val sampleComponent = SampleComponent()
105 sampleComponent.workflowName = "sample-action"
106 sampleComponent.executionServiceInput = JacksonUtils.readValueFromClassPathFile(
107 "payload/requests/sample-execution-request.json", ExecutionServiceInput::class.java
109 val payload = sampleComponent.requestPayload()
110 assertNotNull(payload, "failed to get payload")
111 val data = sampleComponent.requestPayloadActionProperty("data")?.first()
112 assertNotNull(data, "failed to get payload request action data")
116 fun testAbstractScriptComponent() {
118 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
119 val samp = SampleRestconfComponent(compSvc)
120 val comp = samp as AbstractComponentFunction
122 comp.bluePrintRuntimeService = bluePrintRuntime
123 comp.stepName = "sample-step"
124 assertNotNull(comp, "failed to get kotlin instance")
126 val input = getMockedInput(bluePrintRuntime)
127 val inp = getMockedContext()
129 val output = comp.applyNB(input)
131 assertEquals(output.actionIdentifiers.actionName, "activate")
132 assertEquals(output.commonHeader.requestId, "1234")
133 assertEquals(output.stepData!!.name, "activate-restconf")
134 assertEquals(output.status.message, "success")
139 * Mocked input for abstract function test.
141 private fun getMockedContext() {
142 val operationOutputs = hashMapOf<String, JsonNode>()
144 blueprintContext.name()
145 } returns "SampleTest"
147 blueprintContext.version()
148 } returns "SampleScriptComponent"
152 * Mocked input for abstract function test.
154 private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
155 ExecutionServiceInput {
157 val mapper = ObjectMapper()
158 val rootNode = mapper.createObjectNode()
159 rootNode.put("ip-address", "0.0.0.0")
160 rootNode.put("type", "rest")
162 val operationInputs = hashMapOf<String, JsonNode>()
163 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
164 "activate-restconf".asJsonPrimitive()
165 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
166 "interfaceName".asJsonPrimitive()
167 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
168 "operationName".asJsonPrimitive()
169 operationInputs["dynamic-properties"] = rootNode
171 val stepInputData = StepData().apply {
172 name = "activate-restconf"
173 properties = operationInputs
175 val executionServiceInput = ExecutionServiceInput().apply {
176 commonHeader = CommonHeader().apply {
179 actionIdentifiers = ActionIdentifiers().apply {
180 actionName = "activate"
182 payload = JacksonUtils.jsonNode("{}") as ObjectNode
184 executionServiceInput.stepData = stepInputData
187 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
188 "activate-restconf", "interfaceName", "operationName"
190 } returns operationInputs
192 val operationOutputs = hashMapOf<String, JsonNode>()
194 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
195 "activate-restconf", "interfaceName", "operationName"
197 } returns operationOutputs
198 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
200 return executionServiceInput
204 fun testComponentScriptExecutorNodeType() {
205 val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
206 assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")