1f070d46dfa3b7774d4814fb753b1e09313ec0fd
[ccsdk/cds.git] /
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.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(
51     classes = [CliExecutorConfiguration::class,
52         ExecutionServiceConfiguration::class,
53         BluePrintSshLibConfiguration::class, BluePrintScriptsServiceImpl::class,
54         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDependencyService::class]
55 )
56 @DirtiesContext
57 @TestPropertySource(properties = [], locations = ["classpath:application-test.properties"])
58 class ComponentCliExecutorTest {
59
60     @Autowired
61     lateinit var componentScriptExecutor: ComponentScriptExecutor
62
63     @Test
64     fun `test CLI Component Instance`() {
65         runBlocking {
66             assertNotNull(componentScriptExecutor, "failed to get ComponentCliExecutor instance")
67             val executionServiceInput = ExecutionServiceInput().apply {
68                 commonHeader = CommonHeader().apply {
69                     requestId = "1234"
70                 }
71                 actionIdentifiers = ActionIdentifiers().apply {
72                     actionName = "activate"
73                 }
74                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
75             }
76             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
77             componentScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
78             componentScriptExecutor.stepName = "sample-step"
79
80             val operationInputs = hashMapOf<String, JsonNode>()
81             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive()
82             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
83             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
84             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
85             operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] =
86                 "internal.scripts.TestCliScriptFunction".asJsonPrimitive()
87
88             val stepInputData = StepData().apply {
89                 name = "activate-cli"
90                 properties = operationInputs
91             }
92             executionServiceInput.stepData = stepInputData
93
94             val blueprintContext = mockk<BluePrintContext>()
95             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
96             every {
97                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
98                     "activate-cli",
99                     "interfaceName", "operationName"
100                 )
101             } returns operationInputs
102
103             val operationOutputs = hashMapOf<String, JsonNode>()
104             every {
105                 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
106                     "activate-cli",
107                     "interfaceName", "operationName"
108                 )
109             } returns operationOutputs
110
111             componentScriptExecutor.applyNB(executionServiceInput)
112         }
113     }
114 }