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