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