Convert component functions IT to UT.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restconf / executor / ComponentRestconfExecutorTest.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restconf.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ObjectNode
21 import io.mockk.every
22 import io.mockk.mockk
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Test
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
29 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
30 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.springframework.context.ApplicationContext
37 import kotlin.test.assertNotNull
38
39 class ComponentRestconfExecutorTest {
40
41     @Test
42     fun `test Restconf Component Instance`() {
43         runBlocking {
44
45             val applicationContext = mockk<ApplicationContext>()
46             every { applicationContext.getBean(any()) } returns mockk()
47             val componentFunctionScriptingService = ComponentFunctionScriptingService(applicationContext, mockk())
48             val componentScriptExecutor = ComponentScriptExecutor(componentFunctionScriptingService)
49
50             assertNotNull(componentScriptExecutor, "failed to get ComponentRestconfExecutor instance")
51             val executionServiceInput = ExecutionServiceInput().apply {
52                 commonHeader = CommonHeader().apply {
53                     requestId = "1234"
54                 }
55                 actionIdentifiers = ActionIdentifiers().apply {
56                     actionName = "activate"
57                 }
58                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
59             }
60             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
61             componentScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
62             componentScriptExecutor.stepName = "sample-step"
63
64             val operationInputs = hashMapOf<String, JsonNode>()
65             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-restconf".asJsonPrimitive()
66             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
67             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
68             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
69             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] =
70                     "internal.scripts.TestRestconfConfigure".asJsonPrimitive()
71
72             val stepInputData = StepData().apply {
73                 name = "activate-restconf"
74                 properties = operationInputs
75             }
76             executionServiceInput.stepData = stepInputData
77
78             val blueprintContext = mockk<BluePrintContext>()
79             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
80             every {
81                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs("activate-restconf",
82                         "interfaceName", "operationName")
83             } returns operationInputs
84
85             val operationOutputs = hashMapOf<String, JsonNode>()
86             every {
87                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs("activate-restconf",
88                         "interfaceName", "operationName")
89             } returns operationOutputs
90
91             componentScriptExecutor.applyNB(executionServiceInput)
92         }
93     }
94 }