Add remote scripts cba
[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 io.mockk.every
21 import io.mockk.mockk
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Test
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
25 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import kotlin.test.assertEquals
31 import kotlin.test.assertNotNull
32
33
34 class ComponentRemotePythonExecutorTest {
35
36     @Test
37     fun testComponentRemotePythonExecutor() {
38         runBlocking {
39             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
40
41             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
42
43             val executionServiceInput = JacksonUtils.readValueFromClassPathFile("payload/requests/sample-activate-request.json",
44                     ExecutionServiceInput::class.java)!!
45
46             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
47                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts")
48
49             val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
50             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
51             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
52             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
53             componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
54             val stepInputData = StepData().apply {
55                 name = "execute-remote-python"
56                 properties = stepMetaData
57             }
58             executionServiceInput.stepData = stepInputData
59             componentRemotePythonExecutor.applyNB(executionServiceInput)
60         }
61
62     }
63 }
64
65 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
66     override suspend fun init(selector: String) {
67     }
68
69     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
70         assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
71         assertEquals(prepareEnvInput.remoteScriptType, RemoteScriptType.PYTHON, "failed to match script type")
72         assertNotNull(prepareEnvInput.packages, "failed to get packages")
73
74         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
75         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
76         return remoteScriptExecutionOutput
77     }
78
79     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
80         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
81         assertEquals(remoteExecutionInput.remoteScriptType, RemoteScriptType.PYTHON, "failed to match script type")
82
83         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
84         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
85         return remoteScriptExecutionOutput
86     }
87
88     override suspend fun close() {
89
90     }
91 }