AbstractComponentFunction Payload API
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / scripts / AbstractComponentFunctionTest.kt
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.blueprintsprocessor.services.execution.nodeTypeComponentScriptExecutor
38 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
39 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
40 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
41 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
42 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
43 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
44 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
45 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
46 import org.springframework.beans.factory.annotation.Autowired
47 import org.springframework.test.context.ContextConfiguration
48 import org.springframework.test.context.junit4.SpringRunner
49 import kotlin.test.BeforeTest
50 import kotlin.test.assertEquals
51 import kotlin.test.assertNotNull
52
53 /**
54  * Unit test cases for abstract component function.
55  */
56 @RunWith(SpringRunner::class)
57 @ContextConfiguration(classes = [ComponentFunctionScriptingService::class,
58     BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class,
59     BlueprintJythonService::class])
60 class AbstractComponentFunctionTest {
61
62     lateinit var blueprintContext: BluePrintContext
63
64     @Autowired
65     lateinit var compSvc: ComponentFunctionScriptingService
66
67     @BeforeTest
68     fun init() {
69         blueprintContext = mockk<BluePrintContext>()
70         every { blueprintContext.rootPath } returns normalizedPathName("target")
71     }
72
73     @Test
74     fun testAbstractComponent() {
75         runBlocking {
76             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
77             val samp = SampleComponent()
78             val comp = samp as AbstractComponentFunction
79
80             comp.bluePrintRuntimeService = bluePrintRuntime
81             comp.stepName = "sample-step"
82             assertNotNull(comp, "failed to get kotlin instance")
83
84             val input = getMockedInput(bluePrintRuntime)
85
86             val output = comp.applyNB(input)
87
88             assertEquals(output.actionIdentifiers.actionName, "activate")
89             assertEquals(output.commonHeader.requestId, "1234")
90             assertEquals(output.stepData!!.name, "activate-restconf")
91             assertEquals(output.status.message, "success")
92         }
93     }
94
95     @Test
96     fun testComponentFunctionPayload() {
97         val sampleComponent = SampleComponent()
98         sampleComponent.operationName = "sample-action"
99         sampleComponent.executionServiceInput = JacksonUtils.readValueFromClassPathFile(
100             "payload/requests/sample-execution-request.json", ExecutionServiceInput::class.java)!!
101         val payload = sampleComponent.requestPayload()
102         assertNotNull(payload, "failed to get payload")
103         val data = sampleComponent.requestPayloadActionProperty("data")?.first()
104         assertNotNull(data, "failed to get payload request action data")
105     }
106
107     @Test
108     fun testAbstractScriptComponent() {
109         runBlocking {
110             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
111             val samp = SampleRestconfComponent(compSvc)
112             val comp = samp as AbstractComponentFunction
113
114             comp.bluePrintRuntimeService = bluePrintRuntime
115             comp.stepName = "sample-step"
116             assertNotNull(comp, "failed to get kotlin instance")
117
118             val input = getMockedInput(bluePrintRuntime)
119             val inp = getMockedContext()
120
121             val output = comp.applyNB(input)
122
123             assertEquals(output.actionIdentifiers.actionName, "activate")
124             assertEquals(output.commonHeader.requestId, "1234")
125             assertEquals(output.stepData!!.name, "activate-restconf")
126             assertEquals(output.status.message, "success")
127         }
128     }
129
130     /**
131      * Mocked input for abstract function test.
132      */
133     private fun getMockedContext() {
134         val operationOutputs = hashMapOf<String, JsonNode>()
135         every {
136             blueprintContext.name()
137         } returns "SampleTest"
138         every {
139             blueprintContext.version()
140         } returns "SampleScriptComponent"
141     }
142
143     /**
144      * Mocked input for abstract function test.
145      */
146     private fun getMockedInput(bluePrintRuntime: DefaultBluePrintRuntimeService):
147             ExecutionServiceInput {
148
149         val mapper = ObjectMapper()
150         val rootNode = mapper.createObjectNode()
151         rootNode.put("ip-address", "0.0.0.0")
152         rootNode.put("type", "rest")
153
154         val operationInputs = hashMapOf<String, JsonNode>()
155         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] =
156                 "activate-restconf".asJsonPrimitive()
157         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
158                 "interfaceName".asJsonPrimitive()
159         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] =
160                 "operationName".asJsonPrimitive()
161         operationInputs["dynamic-properties"] = rootNode
162
163
164         val stepInputData = StepData().apply {
165             name = "activate-restconf"
166             properties = operationInputs
167         }
168         val executionServiceInput = ExecutionServiceInput().apply {
169             commonHeader = CommonHeader().apply {
170                 requestId = "1234"
171             }
172             actionIdentifiers = ActionIdentifiers().apply {
173                 actionName = "activate"
174             }
175             payload = JacksonUtils.jsonNode("{}") as ObjectNode
176         }
177         executionServiceInput.stepData = stepInputData
178
179         every {
180             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
181                     "activate-restconf", "interfaceName", "operationName")
182         } returns operationInputs
183
184         val operationOutputs = hashMapOf<String, JsonNode>()
185         every {
186             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
187                     "activate-restconf", "interfaceName", "operationName")
188         } returns operationOutputs
189         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
190
191         return executionServiceInput
192     }
193
194     @Test
195     fun testComponentScriptExecutorNodeType() {
196         val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor()
197         assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations")
198     }
199
200 }
201