Convert component functions IT to UT.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / netconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / netconf / executor / ComponentNetconfExecutorTest.kt
index b84c2d1..be3ee46 100644 (file)
@@ -2,6 +2,7 @@
  * Copyright © 2017-2018 AT&T Intellectual Property.
  *
  * Modifications Copyright © 2019 IBM, Bell Canada.
+ * Modifications Copyright © 2019 IBM.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor
 
 import com.fasterxml.jackson.databind.JsonNode
+import io.mockk.coEvery
+import io.mockk.every
+import io.mockk.mockk
+import io.mockk.spyk
+import kotlinx.coroutines.runBlocking
 import org.junit.Test
-import org.junit.runner.RunWith
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
+import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
-import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration
-import org.springframework.context.annotation.ComponentScan
-import org.springframework.test.annotation.DirtiesContext
-import org.springframework.test.context.TestPropertySource
-import org.springframework.test.context.junit4.SpringRunner
-
-@RunWith(SpringRunner::class)
-@EnableAutoConfiguration
-@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
-@DirtiesContext
-@TestPropertySource(properties =
-["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_netconf,./../../../../components/scripts/python/ccsdk_blueprints",
-    "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_netconf"],
-    locations = ["classpath:application-test.properties"])
-class ComponentNetconfExecutorTest {
-
-    @Autowired
-    lateinit var componentNetconfExecutor: ComponentNetconfExecutor
+import org.springframework.context.ApplicationContext
 
+class ComponentNetconfExecutorTest {
 
     @Test
     fun testComponentNetconfExecutor() {
 
-        val executionServiceInput = JacksonUtils.readValueFromClassPathFile("requests/sample-activate-request.json",
-            ExecutionServiceInput::class.java)!!
+        runBlocking {
+
+            val applicationContext = mockk<ApplicationContext>()
+            every { applicationContext.getBean(any()) } returns mockk()
+
+            val blueprintJythonService = mockk<BlueprintJythonService>()
+            val mockAbstractScriptComponentFunction = spyk<AbstractScriptComponentFunction>()
+            coEvery { mockAbstractScriptComponentFunction.executeScript(any()) } returns mockk()
+
+            coEvery { blueprintJythonService.jythonComponentInstance(any(), any()) } returns mockAbstractScriptComponentFunction
+
+            val componentFunctionScriptingService = ComponentFunctionScriptingService(applicationContext,
+                    blueprintJythonService)
+
+            val componentNetconfExecutor = ComponentNetconfExecutor(componentFunctionScriptingService)
 
-        val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
-            "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
+            val executionServiceInput = JacksonUtils.readValueFromClassPathFile("requests/sample-activate-request.json",
+                    ExecutionServiceInput::class.java)!!
 
-        val executionContext = bluePrintRuntimeService.getExecutionContext()
+            val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
+                    "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
 
+            val assignmentParams = """{
+                "ipAddress" : "127.0.0.1",
+                "hostName" : "vnf-host"
+                }                
+            """.trimIndent()
 
-        componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService
+            val json = """{
+                "hostname" : "127.0.0.1"
+                }                
+            """.trimIndent()
 
-        //TODO("Set Attribute properties")
-        val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
-        stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "activate-netconf")
-        stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentNetconfExecutor")
-        stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
-        // Set Step Inputs in Blueprint Runtime Service
-        bluePrintRuntimeService.put("activate-netconf-step-inputs", stepMetaData.asJsonNode())
+            bluePrintRuntimeService.assignInputs(json.asJsonType())
+            bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
+                    JacksonUtils.jsonNode(assignmentParams))
 
-        componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService
-        componentNetconfExecutor.stepName = "activate-netconf"
-        componentNetconfExecutor.apply(executionServiceInput)
+            componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService
 
+            //TODO("Set Attribute properties")
+            val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
+            stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "activate-netconf")
+            stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentNetconfExecutor")
+            stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
+            // Set Step Inputs in Blueprint Runtime Service
+            componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService
+            val stepInputData = StepData().apply {
+                name = "activate-netconf"
+                properties = stepMetaData
+            }
+            executionServiceInput.stepData = stepInputData
+            componentNetconfExecutor.applyNB(executionServiceInput)
+        }
     }
 }