847b080188cb6670d3acb91e006fa341fb41c86e
[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
169             .getBluePrintRuntime(
170                 "123456-1000",
171                 "./../../../../components/model-" +
172                         "catalog/blueprint-model/test-blueprint/" +
173                         "remote_scripts"
174             )
175         every {
176             svc.resolveNodeTemplateArtifactDefinition(
177                 "execute-remote-python", "component-script"
178             )
179         } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
180             "execute-remote-python", "component-script"
181         )
182         every {
183             svc.setNodeTemplateAttributeValue(
184                 "execute-remote-python", "prepare-environment-logs",
185                 "prepared successfully".asJsonPrimitive()
186             )
187         } returns Unit
188         every {
189             svc.setNodeTemplateAttributeValue(
190                 "execute-remote-python",
191                 "execute-command-logs", "N/A".asJsonPrimitive()
192             )
193         } returns Unit
194         every {
195             svc.setNodeTemplateAttributeValue(
196                 "execute-remote-python",
197                 "execute-command-logs",
198                 "processed successfully".asJsonPrimitive()
199             )
200         } returns Unit
201
202         every {
203             svc.resolveDSLExpression("aai")
204         } returns """{"url" : "http://xxx.com"}""".asJsonType()
205
206         every {
207             svc.bluePrintContext()
208         } returns bluePrintRuntimeService.bluePrintContext()
209         return executionServiceInput
210     }
211 }
212
213 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
214     override suspend fun init(selector: Any) {
215     }
216
217     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
218         assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
219         assertNotNull(prepareEnvInput.packages, "failed to get packages")
220
221         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
222         every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
223         every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
224         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
225         return remoteScriptExecutionOutput
226     }
227
228     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
229         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
230
231         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
232         every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
233         every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
234         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
235         return remoteScriptExecutionOutput
236     }
237
238     override suspend fun close() {
239     }
240 }