Metadata for name, version, tags and type
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / python-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / python / executor / ComponentRemotePythonExecutorTest.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.python.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import io.mockk.every
22 import io.mockk.mockk
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Test
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
31 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import kotlin.test.assertEquals
40 import kotlin.test.assertNotNull
41
42 class ComponentRemotePythonExecutorTest {
43
44     @Test
45     fun testComponentRemotePythonExecutor() {
46         runBlocking {
47             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
48
49             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
50
51             val executionServiceInput =
52                 JacksonUtils.readValueFromClassPathFile(
53                     "payload/requests/sample-remote-python-request.json",
54                     ExecutionServiceInput::class.java
55                 )!!
56
57             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
58                 "123456-1000",
59                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
60             )
61
62             /** Load Workflow Inputs */
63             bluePrintRuntimeService.assignWorkflowInputs(
64                 "execute-remote-python",
65                 executionServiceInput.payload.get("execute-remote-python-request")
66             )
67
68             val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
69             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
70             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
71             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
72             componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
73             val stepInputData = StepData().apply {
74                 name = "execute-remote-python"
75                 properties = stepMetaData
76             }
77             executionServiceInput.stepData = stepInputData
78             componentRemotePythonExecutor.applyNB(executionServiceInput)
79         }
80     }
81
82     /**
83      * Test cases for python executor to work with the process NB of remote
84      * executor.
85      */
86     @Test
87     fun testComponentRemotePythonExecutorProcessNB() {
88         runBlocking {
89             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
90             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
91             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
92
93             every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
94
95             val input = getMockedOutput(bluePrintRuntime)
96             componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
97             componentRemotePythonExecutor.applyNB(input)
98         }
99     }
100
101     /**
102      * Mocked input information for remote python executor.
103      */
104     fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
105         ExecutionServiceInput {
106         val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
107
108         stepMetaData.putJsonElement(
109             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
110             "execute-remote-python"
111         )
112         stepMetaData.putJsonElement(
113             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
114             "ComponentRemotePythonExecutor"
115         )
116         stepMetaData.putJsonElement(
117             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
118         )
119
120         val mapper = ObjectMapper()
121         val rootNode = mapper.createObjectNode()
122         rootNode.put("ip-address", "0.0.0.0")
123         rootNode.put("type", "rest")
124
125         val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
126         operationalInputs.putJsonElement(
127             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
128             "execute-remote-python"
129         )
130         operationalInputs.putJsonElement(
131             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
132             "ComponentRemotePythonExecutor"
133         )
134         operationalInputs.putJsonElement(
135             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
136         )
137         operationalInputs.putJsonElement("endpoint-selector", "aai")
138         operationalInputs.putJsonElement("dynamic-properties", rootNode)
139         operationalInputs.putJsonElement("command", "./run.sh")
140         operationalInputs.putJsonElement("packages", "py")
141
142         every {
143             svc.resolveNodeTemplateInterfaceOperationInputs(
144                 "execute-remote-python",
145                 "ComponentRemotePythonExecutor", "process"
146             )
147         } returns operationalInputs
148
149         val stepInputData = StepData().apply {
150             name = "execute-remote-python"
151             properties = stepMetaData
152         }
153
154         val executionServiceInput = JacksonUtils
155             .readValueFromClassPathFile(
156                 "payload/requests/sample-remote-python-request.json",
157                 ExecutionServiceInput::class.java
158             )!!
159         executionServiceInput.stepData = stepInputData
160
161         val operationOutputs = hashMapOf<String, JsonNode>()
162         every {
163             svc.resolveNodeTemplateInterfaceOperationOutputs(
164                 "execute-remote-python",
165                 "ComponentRemotePythonExecutor", "process"
166             )
167         } returns operationOutputs
168         val bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
169             "123456-1000",
170             "./../../../../components/model-" +
171                 "catalog/blueprint-model/test-blueprint/" +
172                 "remote_scripts"
173         )
174         every {
175             svc.resolveNodeTemplateArtifactDefinition(
176                 "execute-remote-python", "component-script"
177             )
178         } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
179             "execute-remote-python", "component-script"
180         )
181         every {
182             svc.setNodeTemplateAttributeValue(
183                 "execute-remote-python", "prepare-environment-logs",
184                 "prepared successfully".asJsonPrimitive()
185             )
186         } returns Unit
187         every {
188             svc.setNodeTemplateAttributeValue(
189                 "execute-remote-python",
190                 "execute-command-logs", "N/A".asJsonPrimitive()
191             )
192         } returns Unit
193         every {
194             svc.setNodeTemplateAttributeValue(
195                 "execute-remote-python",
196                 "execute-command-logs",
197                 "processed successfully".asJsonPrimitive()
198             )
199         } returns Unit
200
201         every {
202             svc.resolveDSLExpression("aai")
203         } returns """{"url" : "http://xxx.com"}""".asJsonType()
204
205         every {
206             svc.bluePrintContext()
207         } returns bluePrintRuntimeService.bluePrintContext()
208         return executionServiceInput
209     }
210 }
211
212 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
213     override suspend fun init(selector: Any) {
214     }
215
216     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
217         assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
218         assertNotNull(prepareEnvInput.packages, "failed to get packages")
219
220         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
221         every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
222         every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
223         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
224         return remoteScriptExecutionOutput
225     }
226
227     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
228         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
229
230         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
231         every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
232         every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
233         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
234         return remoteScriptExecutionOutput
235     }
236
237     override suspend fun close() {
238     }
239 }