2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import io.mockk.coEvery
25 import kotlinx.coroutines.runBlocking
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createExecutionServiceOutputProto
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createStatus
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.toProto
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
36 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
37 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
39 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
40 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
42 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
44 import kotlin.test.assertNotNull
46 class ComponentRemoteScriptExecutorTest {
49 fun testNodeComponentRemoteScriptExecutorType() {
50 val nodeType = BluePrintTypes.nodeTypeComponentRemoteScriptExecutor()
51 assertNotNull(nodeType, "failed to generate nodeType Component Remote Script Executor")
55 fun testNodeTemplateComponentRemoteScriptExecutor() {
57 val nodeTemplate = BluePrintTypes.nodeTemplateComponentRemoteScriptExecutor(
59 "This is sample node template"
61 definedOperation(" Sample Operation") {
62 implementation(180, "SELF")
64 selector("remote-script-executor")
65 blueprintName("sample")
66 blueprintVersion("1.0.0")
67 blueprintAction("sample-action")
69 requestData("""{"key" :"value"}""")
76 assertNotNull(nodeTemplate, "failed to generate nodeTemplate Component Remote Script Executor")
80 fun testComponentRemoteScriptExecutor() {
82 /** Mock blueprint context */
83 val blueprintContext = mockk<BluePrintContext>()
84 every { blueprintContext.rootPath } returns normalizedPathName("target")
86 blueprintContext.nodeTemplateOperationImplementation(
87 "remote-execute", "ComponentRemoteScriptExecutor", "process"
89 } returns Implementation()
91 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
92 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
94 val mockExecutionServiceInput = mockExecutionServiceInput(bluePrintRuntime)
96 val mockStreamingRemoteExecutionService = mockk<StreamingRemoteExecutionService<
97 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
98 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>>()
101 mockStreamingRemoteExecutionService.sendNonInteractive(
107 } returns mockExecutionServiceOutput(mockExecutionServiceInput)
109 val componentRemoteScriptExecutor = ComponentRemoteScriptExecutor(mockStreamingRemoteExecutionService)
110 componentRemoteScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
111 componentRemoteScriptExecutor.implementation = Implementation()
112 val componentRemoteScriptExecutorOutput = componentRemoteScriptExecutor.applyNB(mockExecutionServiceInput)
113 assertNotNull(componentRemoteScriptExecutorOutput, "failed to get executor output")
117 private fun mockExecutionServiceInput(bluePrintRuntime: DefaultBluePrintRuntimeService): ExecutionServiceInput {
119 val mapper = ObjectMapper()
120 val requestNode = mapper.createObjectNode()
121 requestNode.put("ip-address", "0.0.0.0")
122 requestNode.put("type", "grpc")
124 val operationInputs = hashMapOf<String, JsonNode>()
125 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "remote-execute".asJsonPrimitive()
126 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
127 "ComponentRemoteScriptExecutor".asJsonPrimitive()
128 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "process".asJsonPrimitive()
130 operationInputs[ComponentRemoteScriptExecutor.INPUT_SELECTOR] = "remote-script-executor".asJsonPrimitive()
131 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_NAME] = "sample-blueprint".asJsonPrimitive()
132 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_VERSION] = "1.0.0".asJsonPrimitive()
133 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_ACTION] = "remote-activate".asJsonPrimitive()
134 operationInputs[ComponentRemoteScriptExecutor.INPUT_TIMEOUT] = 120.asJsonPrimitive()
135 operationInputs[ComponentRemoteScriptExecutor.INPUT_REQUEST_DATA] = requestNode
137 val stepInputData = StepData().apply {
138 name = "remote-execute"
139 properties = operationInputs
141 val executionServiceInput = ExecutionServiceInput().apply {
142 commonHeader = CommonHeader().apply {
144 subRequestId = "1234-123"
145 originatorId = "test-client"
147 actionIdentifiers = ActionIdentifiers().apply {
148 blueprintName = "sample-blueprint"
149 blueprintVersion = "1.0.0"
150 actionName = "remote-activate"
151 mode = ACTION_MODE_SYNC
153 payload = """{}""".jsonAsJsonType() as ObjectNode
155 executionServiceInput.stepData = stepInputData
158 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
159 "remote-execute", "ComponentRemoteScriptExecutor", "process"
161 } returns operationInputs
163 /** Mock Set Attributes */
165 bluePrintRuntime.setNodeTemplateAttributeValue(
166 "remote-execute", any(), any()
170 val operationOutputs = hashMapOf<String, JsonNode>()
172 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
173 "remote-execute", "ComponentRemoteScriptExecutor", "process"
175 } returns operationOutputs
177 return executionServiceInput
180 private fun mockExecutionServiceOutput(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
181 val actionName = executionServiceInput.actionIdentifiers.actionName
182 val responsePayload = """
184 "$actionName-response" :{
189 return createExecutionServiceOutputProto(
190 executionServiceInput.commonHeader.toProto(),
191 executionServiceInput.actionIdentifiers.toProto(),
192 createStatus(BluePrintConstants.STATUS_SUCCESS, 200),