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
index 0c5a947..b81f05c 100644 (file)
 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
 
 import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
 import io.mockk.every
 import io.mockk.mockk
 import kotlinx.coroutines.runBlocking
 import org.junit.Test
-import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
+import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptUploadBlueprintInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptUploadBlueprintOutput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
+import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
+import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import kotlin.test.assertEquals
 import kotlin.test.assertNotNull
 
-
 class ComponentRemotePythonExecutorTest {
 
     @Test
@@ -38,13 +51,28 @@ class ComponentRemotePythonExecutorTest {
         runBlocking {
             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
 
-            val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
+            val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
+                remoteScriptExecutionService,
+                mockk<BluePrintPropertiesService>(),
+                mockk<BlueprintModelRepository>()
+            )
+
+            val executionServiceInput =
+                JacksonUtils.readValueFromClassPathFile(
+                    "payload/requests/sample-remote-python-request.json",
+                    ExecutionServiceInput::class.java
+                )!!
 
-            val executionServiceInput = JacksonUtils.readValueFromClassPathFile("payload/requests/sample-activate-request.json",
-                    ExecutionServiceInput::class.java)!!
+            val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
+                "123456-1000",
+                "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
+            )
 
-            val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
-                    "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts")
+            /** Load Workflow Inputs */
+            bluePrintRuntimeService.assignWorkflowInputs(
+                "execute-remote-python",
+                executionServiceInput.payload.get("execute-remote-python-request")
+            )
 
             val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
@@ -58,12 +86,155 @@ class ComponentRemotePythonExecutorTest {
             executionServiceInput.stepData = stepInputData
             componentRemotePythonExecutor.applyNB(executionServiceInput)
         }
+    }
+
+    /**
+     * Test cases for python executor to work with the process NB of remote
+     * executor.
+     */
+    @Test
+    fun testComponentRemotePythonExecutorProcessNB() {
+        runBlocking {
+            val remoteScriptExecutionService = MockRemoteScriptExecutionService()
+            val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
+                remoteScriptExecutionService,
+                mockk<BluePrintPropertiesService>(),
+                mockk<BlueprintModelRepository>()
+            )
+            val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
+
+            every { bluePrintRuntime.getBluePrintError() } answers { BluePrintError() } // successful case.
+            every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
 
+            val input = getMockedOutput(bluePrintRuntime)
+            componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
+            componentRemotePythonExecutor.applyNB(input)
+        }
     }
+
+    /**
+     * Mocked input information for remote python executor.
+     */
+    fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
+        ExecutionServiceInput {
+            val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
+
+            stepMetaData.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
+                "execute-remote-python"
+            )
+            stepMetaData.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
+                "ComponentRemotePythonExecutor"
+            )
+            stepMetaData.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
+            )
+
+            val mapper = ObjectMapper()
+            val rootNode = mapper.createObjectNode()
+            rootNode.put("ip-address", "0.0.0.0")
+            rootNode.put("type", "rest")
+
+            val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
+            operationalInputs.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
+                "execute-remote-python"
+            )
+            operationalInputs.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
+                "ComponentRemotePythonExecutor"
+            )
+            operationalInputs.putJsonElement(
+                BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
+            )
+            operationalInputs.putJsonElement("endpoint-selector", "aai")
+            operationalInputs.putJsonElement("dynamic-properties", rootNode)
+            operationalInputs.putJsonElement("command", "./run.sh")
+            operationalInputs.putJsonElement("packages", "py")
+
+            every {
+                svc.resolveNodeTemplateInterfaceOperationInputs(
+                    "execute-remote-python",
+                    "ComponentRemotePythonExecutor", "process"
+                )
+            } returns operationalInputs
+
+            val stepInputData = StepData().apply {
+                name = "execute-remote-python"
+                properties = stepMetaData
+            }
+
+            val executionServiceInput = JacksonUtils
+                .readValueFromClassPathFile(
+                    "payload/requests/sample-remote-python-request.json",
+                    ExecutionServiceInput::class.java
+                )!!
+            executionServiceInput.stepData = stepInputData
+
+            val operationOutputs = hashMapOf<String, JsonNode>()
+            every {
+                svc.resolveNodeTemplateInterfaceOperationOutputs(
+                    "execute-remote-python",
+                    "ComponentRemotePythonExecutor", "process"
+                )
+            } returns operationOutputs
+            val bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
+                "123456-1000",
+                "./../../../../components/model-" +
+                    "catalog/blueprint-model/test-blueprint/" +
+                    "remote_scripts"
+            )
+            every {
+                svc.resolveNodeTemplateArtifactDefinition(
+                    "execute-remote-python", "component-script"
+                )
+            } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
+                "execute-remote-python", "component-script"
+            )
+            every {
+                svc.setNodeTemplateAttributeValue(
+                    "execute-remote-python", "prepare-environment-logs",
+                    "prepared successfully".asJsonPrimitive()
+                )
+            } returns Unit
+            every {
+                svc.setNodeTemplateAttributeValue(
+                    "execute-remote-python",
+                    "execute-command-logs", "N/A".asJsonPrimitive()
+                )
+            } returns Unit
+            every {
+                svc.setNodeTemplateAttributeValue(
+                    "execute-remote-python",
+                    "execute-command-logs",
+                    "processed successfully".asJsonPrimitive()
+                )
+            } returns Unit
+
+            every {
+                svc.resolveDSLExpression("aai")
+            } returns """{"url" : "http://xxx.com"}""".asJsonType()
+
+            every {
+                svc.bluePrintContext()
+            } returns bluePrintRuntimeService.bluePrintContext()
+            return executionServiceInput
+        }
 }
 
 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
-    override suspend fun init(selector: String) {
+
+    override suspend fun init(selector: Any) {
+    }
+
+    override suspend fun uploadBlueprint(uploadBpInput: RemoteScriptUploadBlueprintInput): RemoteScriptUploadBlueprintOutput {
+        val uploadBpOutput = mockk<RemoteScriptUploadBlueprintOutput>()
+        every { uploadBpOutput.payload } returns "[]".asJsonPrimitive()
+        every { uploadBpOutput.status } returns StatusType.SUCCESS
+        every { uploadBpOutput.requestId } returns "123456-1000"
+        every { uploadBpOutput.subRequestId } returns "1234"
+        return uploadBpOutput
     }
 
     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
@@ -71,7 +242,8 @@ class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
         assertNotNull(prepareEnvInput.packages, "failed to get packages")
 
         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
-        every { remoteScriptExecutionOutput.response } returns "prepared successfully"
+        every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
+        every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
         return remoteScriptExecutionOutput
     }
@@ -80,12 +252,12 @@ class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
 
         val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
-        every { remoteScriptExecutionOutput.response } returns "processed successfully"
+        every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
+        every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
         every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
         return remoteScriptExecutionOutput
     }
 
     override suspend fun close() {
-
     }
-}
\ No newline at end of file
+}