b404fbed67c967b8b89bfe8c2e6bcee3c1a61390
[ccsdk/cds.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts;
22
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import com.fasterxml.jackson.databind.node.ObjectNode
26 import io.mockk.every
27 import io.mockk.mockk
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
35 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
36 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
37 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
39 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.onap.ccsdk.cds.controllerblueprints.scripts.BluePrintScriptsServiceImpl
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.junit4.SpringRunner
47 import kotlin.test.BeforeTest
48 import kotlin.test.assertEquals
49 import kotlin.test.assertNotNull
50
51 /**
52  * Unit test cases for abstract component function.
53  */
54 @RunWith(SpringRunner::class)
55 @ContextConfiguration(classes = [ComponentFunctionScriptingService::class,
56     BluePrintScriptsServiceImpl::class,PythonExecutorProperty::class,
57     BlueprintJythonService::class])
58 class AbstractComponentFunctionTest {
59
60     lateinit var blueprintContext: BluePrintContext
61
62     @Autowired
63     lateinit var compSvc: ComponentFunctionScriptingService
64
65     @BeforeTest
66     fun init() {
67         blueprintContext = mockk<BluePrintContext>()
68         every { blueprintContext.rootPath } returns normalizedPathName("target")
69     }
70
71     /**
72      * Tests the abstract component functionality.
73      */
74     @Test
75     fun testAbstractComponent() {
76         runBlocking {
77             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
78             val samp = SampleComponent()
79             val comp = samp as AbstractComponentFunction
80
81             comp.bluePrintRuntimeService = bluePrintRuntime
82             comp.stepName = "sample-step"
83             assertNotNull(comp, "failed to get kotlin instance")
84
85             val input = getMockedInput(bluePrintRuntime)
86
87             val output = comp.applyNB(input)
88
89             assertEquals(output.actionIdentifiers.actionName, "activate")
90             assertEquals(output.commonHeader.requestId, "1234")
91             assertEquals(output.stepData!!.name, "activate-restconf")
92             assertEquals(output.status.message, "success")
93         }
94     }
95
96     /**
97      * Tests the abstract script component functionality.
98      */
99     @Test
100     fun testAbstractScriptComponent() {
101         runBlocking {
102             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
103             val samp = SampleRestconfComponent(compSvc)
104             val comp = samp as AbstractComponentFunction
105
106             comp.bluePrintRuntimeService = bluePrintRuntime
107             comp.stepName = "sample-step"
108             assertNotNull(comp, "failed to get kotlin instance")
109
110             val input = getMockedInput(bluePrintRuntime)
111             val inp = getMockedContext()
112
113             val output = comp.applyNB(input)
114
115             assertEquals(output.actionIdentifiers.actionName, "activate")
116             assertEquals(output.commonHeader.requestId, "1234")
117             assertEquals(output.stepData!!.name, "activate-restconf")
118             assertEquals(output.status.message, "success")
119         }
120     }
121
122     /**
123      * Mocked input for abstract function test.
124      */
125     private fun getMockedContext() {
126         val operationOutputs = hashMapOf<String, JsonNode>()
127         every {
128             blueprintContext.name()
129         } returns "SampleTest"
130         every {
131             blueprintContext.version()
132         } returns "SampleScriptComponent"
133     }
134
135     /**
136      * Mocked input for abstract function test.
137      */
138     private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
139             ExecutionServiceInput {
140
141         val mapper = ObjectMapper()
142         val rootNode = mapper.createObjectNode()
143         rootNode.put("ip-address", "0.0.0.0")
144         rootNode.put("type", "rest")
145
146         val operationInputs = hashMapOf<String, JsonNode>()
147         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
148                 "activate-restconf".asJsonPrimitive()
149         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
150                 "interfaceName".asJsonPrimitive()
151         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
152                 "operationName".asJsonPrimitive()
153         operationInputs["dynamic-properties"] = rootNode
154
155
156         val stepInputData = StepData().apply {
157             name = "activate-restconf"
158             properties = operationInputs
159         }
160         val executionServiceInput = ExecutionServiceInput().apply {
161             commonHeader = CommonHeader().apply {
162                 requestId = "1234"
163             }
164             actionIdentifiers = ActionIdentifiers().apply {
165                 actionName = "activate"
166             }
167             payload = JacksonUtils.jsonNode("{}") as ObjectNode
168         }
169         executionServiceInput.stepData = stepInputData
170
171         every {
172             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
173                     "activate-restconf", "interfaceName", "operationName")
174         } returns operationInputs
175
176         val operationOutputs = hashMapOf<String, JsonNode>()
177         every {
178             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
179                     "activate-restconf", "interfaceName", "operationName")
180         } returns operationOutputs
181         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
182
183         return executionServiceInput
184     }
185
186 }
187