Merge "Title section correction in BP MS"
[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.putJsonElement
35 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
38 import kotlin.test.assertEquals
39 import kotlin.test.assertNotNull
40
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("payload/requests/sample-activate-request.json",
53                     ExecutionServiceInput::class.java)!!
54
55             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
56                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts")
57
58             val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
59             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
60             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
61             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
62             componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
63             val stepInputData = StepData().apply {
64                 name = "execute-remote-python"
65                 properties = stepMetaData
66             }
67             executionServiceInput.stepData = stepInputData
68             componentRemotePythonExecutor.applyNB(executionServiceInput)
69         }
70     }
71
72     /**
73      * Test cases for python executor to work with the process NB of remote
74      * executor.
75      */
76     @Test
77     fun testComponentRemotePythonExecutorProcessNB() {
78         runBlocking {
79             val remoteScriptExecutionService =
80                 MockRemoteScriptExecutionService()
81             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
82                 remoteScriptExecutionService)
83             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>(
84                 "123456-1000")
85
86             every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
87
88             val input = getMockedOutput(bluePrintRuntime)
89             componentRemotePythonExecutor.bluePrintRuntimeService =
90                 bluePrintRuntime
91             componentRemotePythonExecutor.applyNB(input)
92         }
93     }
94
95     /**
96      * Mocked input information for remote python executor.
97      */
98     fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
99             ExecutionServiceInput {
100         val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
101
102         stepMetaData.putJsonElement(
103             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
104             "execute-remote-python")
105         stepMetaData.putJsonElement(
106             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
107             "ComponentRemotePythonExecutor")
108         stepMetaData.putJsonElement(
109             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
110
111         val mapper = ObjectMapper()
112         val rootNode = mapper.createObjectNode()
113         rootNode.put("ip-address", "0.0.0.0")
114         rootNode.put("type", "rest")
115
116         val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
117         operationalInputs.putJsonElement(
118             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
119             "execute-remote-python")
120         operationalInputs.putJsonElement(
121             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
122             "ComponentRemotePythonExecutor")
123         operationalInputs.putJsonElement(
124             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
125         operationalInputs.putJsonElement("endpoint-selector", "aai")
126         operationalInputs.putJsonElement("dynamic-properties", rootNode)
127         operationalInputs.putJsonElement("command", "./run.sh")
128         operationalInputs.putJsonElement("packages", "py")
129
130         every {
131             svc.resolveNodeTemplateInterfaceOperationInputs(
132                 "execute-remote-python",
133                 "ComponentRemotePythonExecutor", "process")
134         } returns operationalInputs
135
136         val stepInputData = StepData().apply {
137             name = "execute-remote-python"
138             properties = stepMetaData
139         }
140
141         val executionServiceInput = JacksonUtils
142             .readValueFromClassPathFile(
143                 "payload/requests/sample-remote-python-request.json",
144                 ExecutionServiceInput::class.java)!!
145         executionServiceInput.stepData = stepInputData
146
147         val operationOutputs = hashMapOf<String, JsonNode>()
148         every {
149             svc.resolveNodeTemplateInterfaceOperationOutputs(
150                 "execute-remote-python",
151                 "ComponentRemotePythonExecutor", "process")
152         } returns operationOutputs
153         val bluePrintRuntimeService = BluePrintMetadataUtils
154             .getBluePrintRuntime("123456-1000",
155                 "./../../../../components/model-" +
156                         "catalog/blueprint-model/test-blueprint/" +
157                         "remote_scripts")
158         every {
159             svc.resolveNodeTemplateArtifactDefinition(
160                 "execute-remote-python", "component-script")
161         } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
162             "execute-remote-python", "component-script")
163         every {
164             svc.setNodeTemplateAttributeValue(
165                 "execute-remote-python", "prepare-environment-logs",
166                 "prepared successfully".asJsonPrimitive())
167         } returns Unit
168         every {
169             svc.setNodeTemplateAttributeValue(
170                 "execute-remote-python",
171                 "execute-command-logs", "N/A".asJsonPrimitive())
172         } returns Unit
173         every {
174             svc.setNodeTemplateAttributeValue(
175                 "execute-remote-python",
176                 "execute-command-logs",
177                 "processed successfully".asJsonPrimitive())
178         } returns Unit
179
180         every {
181             svc.bluePrintContext()
182         } returns bluePrintRuntimeService.bluePrintContext()
183         return executionServiceInput
184     }
185 }
186
187 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
188     override suspend fun init(selector: Any) {
189     }
190
191     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
192         assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
193         assertNotNull(prepareEnvInput.packages, "failed to get packages")
194
195         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
196         every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
197         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
198         return remoteScriptExecutionOutput
199     }
200
201     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
202         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
203
204         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
205         every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
206         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
207         return remoteScriptExecutionOutput
208     }
209
210     override suspend fun close() {
211
212     }
213 }