Renaming Files having BluePrint to have Blueprint
[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 = [
53         CliExecutorConfiguration::class,
54         ExecutionServiceConfiguration::class,
55         BlueprintSshLibConfiguration::class, BlueprintScriptsServiceImpl::class,
56         BlueprintPropertyConfiguration::class, BlueprintPropertiesService::class, BlueprintDependencyService::class
57     ]
58 )
59 @DirtiesContext
60 @TestPropertySource(properties = [], locations = ["classpath:application-test.properties"])
61 class ComponentCliExecutorTest {
62
63     @Autowired
64     lateinit var componentScriptExecutor: ComponentScriptExecutor
65
66     @Test
67     fun `test CLI Component Instance`() {
68         runBlocking {
69             assertNotNull(componentScriptExecutor, "failed to get ComponentCliExecutor instance")
70             val executionServiceInput = ExecutionServiceInput().apply {
71                 commonHeader = CommonHeader().apply {
72                     requestId = "1234"
73                 }
74                 actionIdentifiers = ActionIdentifiers().apply {
75                     actionName = "activate"
76                 }
77                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
78             }
79             val bluePrintRuntime = mockk<DefaultBlueprintRuntimeService>("1234")
80             componentScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
81             componentScriptExecutor.stepName = "sample-step"
82
83             val operationInputs = hashMapOf<String, JsonNode>()
84             operationInputs[BlueprintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive()
85             operationInputs[BlueprintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
86             operationInputs[BlueprintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
87             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BlueprintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
88             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] =
89                 "internal.scripts.TestCliScriptFunction".asJsonPrimitive()
90
91             val stepInputData = StepData().apply {
92                 name = "activate-cli"
93                 properties = operationInputs
94             }
95             executionServiceInput.stepData = stepInputData
96
97             val blueprintContext = mockk<BlueprintContext>()
98             every {
99                 blueprintContext.nodeTemplateOperationImplementation(
100                     any(), any(), any()
101                 )
102             } returns Implementation()
103
104             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
105             every {
106                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
107                     "activate-cli",
108                     "interfaceName", "operationName"
109                 )
110             } returns operationInputs
111
112             val operationOutputs = hashMapOf<String, JsonNode>()
113             every {
114                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
115                     "activate-cli",
116                     "interfaceName", "operationName"
117                 )
118             } returns operationOutputs
119
120             componentScriptExecutor.applyNB(executionServiceInput)
121         }
122     }
123 }