Revert "Renaming Files having BluePrint to have Blueprint"
[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.dsl.serviceTemplate
40 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
41 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
42 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
43 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
45 import kotlin.test.assertNotNull
46
47 class ComponentRemoteScriptExecutorTest {
48
49     @Test
50     fun testNodeComponentRemoteScriptExecutorType() {
51         val nodeType = BluePrintTypes.nodeTypeComponentRemoteScriptExecutor()
52         assertNotNull(nodeType, "failed to generate nodeType Component Remote Script Executor")
53     }
54
55     @Test
56     fun testNodeTemplateComponentRemoteScriptExecutor() {
57
58         val serviceTemplate = serviceTemplate("remote-script-dsl", "1.0.0", "xx@xx.com", "remote-script-ds") {
59             topologyTemplate {
60                 nodeTemplateComponentRemoteScriptExecutor(
61                     "remote-sample",
62                     "This is sample node template"
63                 ) {
64                     definedOperation(" Sample Operation") {
65                         implementation(180, "SELF")
66                         inputs {
67                             selector("remote-script-executor")
68                             blueprintName("sample")
69                             blueprintVersion("1.0.0")
70                             blueprintAction("sample-action")
71                             timeout(120)
72                             requestData("""{"key" :"value"}""")
73                         }
74                         outputs {
75                             status("success")
76                         }
77                     }
78                 }
79             }
80             nodeTypeComponentRemoteScriptExecutor()
81         }
82         // println(serviceTemplate.asJsonString(true))
83         assertNotNull(serviceTemplate, "failed to service template")
84         assertNotNull(serviceTemplate.nodeTypes, "failed to service template node Types")
85         assertNotNull(
86             serviceTemplate.nodeTypes!!["component-remote-script-executor"],
87             "failed to service template nodeType(component-remote-script-executor)"
88         )
89         assertNotNull(
90             serviceTemplate.topologyTemplate?.nodeTemplates?.get("remote-sample"),
91             "failed to nodeTemplate(remote-sample)"
92         )
93     }
94
95     @Test
96     fun testComponentRemoteScriptExecutor() {
97         runBlocking {
98             /** Mock blueprint context */
99             val blueprintContext = mockk<BluePrintContext>()
100             every { blueprintContext.rootPath } returns normalizedPathName("target")
101             every {
102                 blueprintContext.nodeTemplateOperationImplementation(
103                     "remote-execute", "ComponentRemoteScriptExecutor", "process"
104                 )
105             } returns Implementation()
106
107             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
108             every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
109
110             val mockExecutionServiceInput = mockExecutionServiceInput(bluePrintRuntime)
111
112             val mockStreamingRemoteExecutionService = mockk<StreamingRemoteExecutionService<
113                     org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
114                     ExecutionServiceOutput>>()
115
116             coEvery {
117                 mockStreamingRemoteExecutionService.sendNonInteractive(
118                     any(),
119                     any(),
120                     any(),
121                     any()
122                 )
123             } returns mockExecutionServiceOutput(mockExecutionServiceInput)
124
125             val componentRemoteScriptExecutor = ComponentRemoteScriptExecutor(mockStreamingRemoteExecutionService)
126             componentRemoteScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
127             componentRemoteScriptExecutor.implementation = Implementation()
128             val componentRemoteScriptExecutorOutput = componentRemoteScriptExecutor.applyNB(mockExecutionServiceInput)
129             assertNotNull(componentRemoteScriptExecutorOutput, "failed to get executor output")
130         }
131     }
132
133     private fun mockExecutionServiceInput(bluePrintRuntime: DefaultBluePrintRuntimeService): ExecutionServiceInput {
134
135         val mapper = ObjectMapper()
136         val requestNode = mapper.createObjectNode()
137         requestNode.put("ip-address", "0.0.0.0")
138         requestNode.put("type", "grpc")
139
140         val operationInputs = hashMapOf<String, JsonNode>()
141         operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "remote-execute".asJsonPrimitive()
142         operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
143             "ComponentRemoteScriptExecutor".asJsonPrimitive()
144         operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "process".asJsonPrimitive()
145
146         operationInputs[ComponentRemoteScriptExecutor.INPUT_SELECTOR] = "remote-script-executor".asJsonPrimitive()
147         operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_NAME] = "sample-blueprint".asJsonPrimitive()
148         operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_VERSION] = "1.0.0".asJsonPrimitive()
149         operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_ACTION] = "remote-activate".asJsonPrimitive()
150         operationInputs[ComponentRemoteScriptExecutor.INPUT_TIMEOUT] = 120.asJsonPrimitive()
151         operationInputs[ComponentRemoteScriptExecutor.INPUT_REQUEST_DATA] = requestNode
152
153         val stepInputData = StepData().apply {
154             name = "remote-execute"
155             properties = operationInputs
156         }
157         val executionServiceInput = ExecutionServiceInput().apply {
158             commonHeader = CommonHeader().apply {
159                 requestId = "1234"
160                 subRequestId = "1234-123"
161                 originatorId = "test-client"
162             }
163             actionIdentifiers = ActionIdentifiers().apply {
164                 blueprintName = "sample-blueprint"
165                 blueprintVersion = "1.0.0"
166                 actionName = "remote-activate"
167                 mode = ACTION_MODE_SYNC
168             }
169             payload = """{}""".jsonAsJsonType() as ObjectNode
170         }
171         executionServiceInput.stepData = stepInputData
172
173         every {
174             bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
175                 "remote-execute", "ComponentRemoteScriptExecutor", "process"
176             )
177         } returns operationInputs
178
179         /** Mock Set Attributes */
180         every {
181             bluePrintRuntime.setNodeTemplateAttributeValue(
182                 "remote-execute", any(), any()
183             )
184         } returns Unit
185
186         val operationOutputs = hashMapOf<String, JsonNode>()
187         every {
188             bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
189                 "remote-execute", "ComponentRemoteScriptExecutor", "process"
190             )
191         } returns operationOutputs
192
193         return executionServiceInput
194     }
195
196     private fun mockExecutionServiceOutput(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
197         val actionName = executionServiceInput.actionIdentifiers.actionName
198         val responsePayload = """
199             {
200             "$actionName-response" :{
201             "key" : "value"
202             }
203             }
204         """.trimIndent()
205         return createExecutionServiceOutputProto(
206             executionServiceInput.commonHeader.toProto(),
207             executionServiceInput.actionIdentifiers.toProto(),
208             createStatus(BluePrintConstants.STATUS_SUCCESS, 200),
209             responsePayload
210         )
211     }
212 }