9c0258db0d9ac28f098bf2633a6d6e9e6cc12ed5
[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.BluePrintProperties
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.scripts.BluePrintScriptsServiceImpl
38 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.test.annotation.DirtiesContext
44 import org.springframework.test.context.ContextConfiguration
45 import org.springframework.test.context.TestPropertySource
46 import org.springframework.test.context.junit4.SpringRunner
47 import kotlin.test.assertNotNull
48
49 @RunWith(SpringRunner::class)
50 @ContextConfiguration(classes = [CliExecutorConfiguration::class,
51     ExecutionServiceConfiguration::class,
52     BluePrintSshLibConfiguration::class, BluePrintScriptsServiceImpl::class,
53     BlueprintPropertyConfiguration::class, BluePrintProperties::class, BluePrintDependencyService::class])
54 @DirtiesContext
55 @TestPropertySource(properties = [], locations = ["classpath:application-test.properties"])
56 class ComponentCliExecutorTest {
57
58     @Autowired
59     lateinit var componentScriptExecutor: ComponentScriptExecutor
60
61     @Test
62     fun `test CLI Component Instance`() {
63         runBlocking {
64             assertNotNull(componentScriptExecutor, "failed to get ComponentCliExecutor instance")
65             val executionServiceInput = ExecutionServiceInput().apply {
66                 commonHeader = CommonHeader().apply {
67                     requestId = "1234"
68                 }
69                 actionIdentifiers = ActionIdentifiers().apply {
70                     actionName = "activate"
71                 }
72                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
73             }
74             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
75             componentScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
76             componentScriptExecutor.stepName = "sample-step"
77
78             val operationInputs = hashMapOf<String, JsonNode>()
79             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive()
80             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
81             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
82             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
83             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] =
84                     "internal.scripts.TestCliScriptFunction".asJsonPrimitive()
85
86             val stepInputData = StepData().apply {
87                 name = "activate-cli"
88                 properties = operationInputs
89             }
90             executionServiceInput.stepData = stepInputData
91
92             val blueprintContext = mockk<BluePrintContext>()
93             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
94             every {
95                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs("activate-cli",
96                         "interfaceName", "operationName")
97             } returns operationInputs
98
99             val operationOutputs = hashMapOf<String, JsonNode>()
100             every {
101                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs("activate-cli",
102                         "interfaceName", "operationName")
103             } returns operationOutputs
104
105             componentScriptExecutor.applyNB(executionServiceInput)
106         }
107     }
108 }