Remote Script Executor Component
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / ComponentRemoteScriptExecutorTest.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
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.services.execution
18
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
23 import io.mockk.every
24 import io.mockk.mockk
25 import kotlinx.coroutines.runBlocking
26 import org.junit.Test
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
45
46 class ComponentRemoteScriptExecutorTest {
47
48     @Test
49     fun testNodeComponentRemoteScriptExecutorType() {
50         val nodeType = BluePrintTypes.nodeTypeComponentRemoteScriptExecutor()
51         assertNotNull(nodeType, "failed to generate nodeType Component Remote Script Executor")
52     }
53
54     @Test
55     fun testNodeTemplateComponentRemoteScriptExecutor() {
56
57         val nodeTemplate = BluePrintTypes.nodeTemplateComponentRemoteScriptExecutor(
58             "remote-sample",
59             "This is sample node template"
60         ) {
61             definedOperation(" Sample Operation") {
62                 implementation(180, "SELF")
63                 inputs {
64                     selector("remote-script-executor")
65                     blueprintName("sample")
66                     blueprintVersion("1.0.0")
67                     blueprintAction("sample-action")
68                     timeout(120)
69                     requestData("""{"key" :"value"}""")
70                 }
71                 outputs {
72                     status("success")
73                 }
74             }
75         }
76         assertNotNull(nodeTemplate, "failed to generate nodeTemplate Component Remote Script Executor")
77     }
78
79     @Test
80     fun testComponentRemoteScriptExecutor() {
81         runBlocking {
82             /** Mock blueprint context */
83             val blueprintContext = mockk<BluePrintContext>()
84             every { blueprintContext.rootPath } returns normalizedPathName("target")
85             every {
86                 blueprintContext.nodeTemplateOperationImplementation(
87                     "remote-execute", "ComponentRemoteScriptExecutor", "process"
88                 )
89             } returns Implementation()
90
91             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
92             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
93
94             val mockExecutionServiceInput = mockExecutionServiceInput(bluePrintRuntime)
95
96             val mockStreamingRemoteExecutionService = mockk<StreamingRemoteExecutionService<
97                 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
98                 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>>()
99
100             coEvery {
101                 mockStreamingRemoteExecutionService.sendNonInteractive(
102                     any(),
103                     any(),
104                     any(),
105                     any()
106                 )
107             } returns mockExecutionServiceOutput(mockExecutionServiceInput)
108
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")
114         }
115     }
116
117     private fun mockExecutionServiceInput(bluePrintRuntime: DefaultBluePrintRuntimeService): ExecutionServiceInput {
118
119         val mapper = ObjectMapper()
120         val requestNode = mapper.createObjectNode()
121         requestNode.put("ip-address", "0.0.0.0")
122         requestNode.put("type", "grpc")
123
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()
129
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
136
137         val stepInputData = StepData().apply {
138             name = "remote-execute"
139             properties = operationInputs
140         }
141         val executionServiceInput = ExecutionServiceInput().apply {
142             commonHeader = CommonHeader().apply {
143                 requestId = "1234"
144                 subRequestId = "1234-123"
145                 originatorId = "test-client"
146             }
147             actionIdentifiers = ActionIdentifiers().apply {
148                 blueprintName = "sample-blueprint"
149                 blueprintVersion = "1.0.0"
150                 actionName = "remote-activate"
151                 mode = ACTION_MODE_SYNC
152             }
153             payload = """{}""".jsonAsJsonType() as ObjectNode
154         }
155         executionServiceInput.stepData = stepInputData
156
157         every {
158             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
159                 "remote-execute", "ComponentRemoteScriptExecutor", "process"
160             )
161         } returns operationInputs
162
163         /** Mock Set Attributes */
164         every {
165             bluePrintRuntime.setNodeTemplateAttributeValue(
166                 "remote-execute", any(), any()
167             )
168         } returns Unit
169
170         val operationOutputs = hashMapOf<String, JsonNode>()
171         every {
172             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
173                 "remote-execute", "ComponentRemoteScriptExecutor", "process"
174             )
175         } returns operationOutputs
176
177         return executionServiceInput
178     }
179
180     private fun mockExecutionServiceOutput(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
181         val actionName = executionServiceInput.actionIdentifiers.actionName
182         val responsePayload = """
183             {
184             "$actionName-response" :{
185             "key" : "value"
186             }
187             }
188         """.trimIndent()
189         return createExecutionServiceOutputProto(
190             executionServiceInput.commonHeader.toProto(),
191             executionServiceInput.actionIdentifiers.toProto(),
192             createStatus(BluePrintConstants.STATUS_SUCCESS, 200),
193             responsePayload
194         )
195     }
196 }