6455513f27fe11c3b911babc01c758c5c2cf49f0
[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.ArrayNode
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import io.mockk.every
23 import io.mockk.mockk
24 import kotlinx.coroutines.runBlocking
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
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.service.BluePrintContext
38 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.scripts.BluePrintScriptsServiceImpl
41 import org.springframework.beans.factory.annotation.Autowired
42 import org.springframework.test.annotation.DirtiesContext
43 import org.springframework.test.context.ContextConfiguration
44 import org.springframework.test.context.TestPropertySource
45 import org.springframework.test.context.junit4.SpringRunner
46 import kotlin.test.assertNotNull
47
48 @RunWith(SpringRunner::class)
49 @ContextConfiguration(classes = [CliExecutorConfiguration::class,
50     ExecutionServiceConfiguration::class,
51     BluePrintSshLibConfiguration::class, BluePrintScriptsServiceImpl::class,
52     BlueprintPropertyConfiguration::class, BluePrintProperties::class])
53 @DirtiesContext
54 @TestPropertySource(properties = [], locations = ["classpath:application-test.properties"])
55 class ComponentCliExecutorTest {
56
57     @Autowired
58     lateinit var componentCliExecutor: ComponentCliExecutor
59
60     @Test
61     fun `test CLI Component Instance`() {
62         runBlocking {
63             assertNotNull(componentCliExecutor, "failed to get ComponentCliExecutor instance")
64             val executionServiceInput = ExecutionServiceInput().apply {
65                 commonHeader = CommonHeader().apply {
66                     requestId = "1234"
67                 }
68                 actionIdentifiers = ActionIdentifiers().apply {
69                     actionName = "activate"
70                 }
71                 payload = JacksonUtils.jsonNode("{}") as ObjectNode
72             }
73             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
74             componentCliExecutor.bluePrintRuntimeService = bluePrintRuntime
75             componentCliExecutor.stepName = "sample-step"
76
77             val operationInputs = hashMapOf<String, JsonNode>()
78             operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive()
79             operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive()
80             operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive()
81             operationInputs[ComponentCliExecutor.SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive()
82             operationInputs[ComponentCliExecutor.SCRIPT_CLASS_REFERENCE] =
83                     "InternalSimpleCli_cba\$TestCliScriptFunction".asJsonPrimitive()
84             operationInputs[ComponentCliExecutor.INSTANCE_DEPENDENCIES] = JacksonUtils.jsonNode("[]") as ArrayNode
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             componentCliExecutor.applyNB(executionServiceInput)
106         }
107     }
108 }