Updating README.md
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / cli-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / cli / executor / ComponentCliExecutorTest.kt
1 /*
2  *  Copyright © 2019 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.cli.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.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
32 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor
33 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceConfiguration
34 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BluePrintSshLibConfiguration
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
38 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.springframework.beans.factory.annotation.Autowired
44 import org.springframework.test.annotation.DirtiesContext
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.TestPropertySource
47 import org.springframework.test.context.junit4.SpringRunner
48 import kotlin.test.assertNotNull
49
50 @RunWith(SpringRunner::class)
51 @ContextConfiguration(
52     classes = [CliExecutorConfiguration::class,
53         ExecutionServiceConfiguration::class,
54         BluePrintSshLibConfiguration::class, BluePrintScriptsServiceImpl::class,
55         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDependencyService::class]
56 )
57 @DirtiesContext
58 @TestPropertySource(properties = [], locations = ["classpath:application-test.properties"])
59 class ComponentCliExecutorTest {
60
61     @Autowired
62     lateinit var componentScriptExecutor: ComponentScriptExecutor
63
64     @Test
65     fun `test CLI Component Instance`() {
66         runBlocking {
67             assertNotNull(componentScriptExecutor, "failed to get ComponentCliExecutor instance")
68             val executionServiceInput = ExecutionServiceInput().apply {
69                 commonHeader = CommonHeader().apply {
70                     requestId = "1234"
71                 }
72                 actionIdentifiers = ActionIdentifiers().apply {
73                     actionName = "activate"
74                 }
75                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
76             }
77             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
78             componentScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
79             componentScriptExecutor.stepName = "sample-step"
80
81             val operationInputs = hashMapOf<String, JsonNode>()
82             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive()
83             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
84             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
85             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
86             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] =
87                 "internal.scripts.TestCliScriptFunction".asJsonPrimitive()
88
89             val stepInputData = StepData().apply {
90                 name = "activate-cli"
91                 properties = operationInputs
92             }
93             executionServiceInput.stepData = stepInputData
94
95             val blueprintContext = mockk<BluePrintContext>()
96             every {
97                 blueprintContext.nodeTemplateOperationImplementation(
98                     any(), any(), any()
99                 )
100             } returns Implementation()
101
102             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
103             every {
104                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
105                     "activate-cli",
106                     "interfaceName", "operationName"
107                 )
108             } returns operationInputs
109
110             val operationOutputs = hashMapOf<String, JsonNode>()
111             every {
112                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
113                     "activate-cli",
114                     "interfaceName", "operationName"
115                 )
116             } returns operationOutputs
117
118             componentScriptExecutor.applyNB(executionServiceInput)
119         }
120     }
121 }