Add declarative acceptance tests
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintDSLTest.kt
1 /*
2  *  Copyright © 2019 IBM.
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.controllerblueprints.core.dsl
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.junit.Test
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
24 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
25 import kotlin.test.assertNotNull
26
27 class BluePrintDSLTest {
28
29     @Test
30     fun testOperationDSLWorkflow() {
31
32         val blueprint = blueprint("sample-bp", "1.0.0",
33                 "brindasanth@onap.com", "sample, blueprints") {
34
35             artifactType(BluePrintTypes.artifactTypeTemplateVelocity())
36
37             // For New Component Definition
38             component("resource-resolution", "component-script-executor", "1.0.0",
39                     "Resource Resolution component.") {
40                 implementation(180)
41                 // Attributes ( Properties which will be set during execution)
42                 attribute("template1-data", "string", true, "")
43
44                 // Properties
45                 property("string-value1", "string", true, "sample")
46                 property("string-value2", "string", true, getInput("key-1"))
47                 // Inputs
48                 input("json-content", "json", true, """{ "name" : "cds"}""")
49                 input("template-content", "string", true, getArtifact("template1"))
50                 // Outputs
51                 output("self-attribute-expression", "json", true, getAttribute("template1-data"))
52                 // Artifacts
53                 artifact("template1", "artifact-template-velocity", "Templates/template1.vtl")
54             }
55
56             // Already definitions Registered Components
57             registryComponent("activate-restconf", "component-resource-resolution", "1.0.0",
58                     "RestconfExecutor", "Resource Resolution component.") {
59                 implementation(180)
60                 // Properties
61                 property("string-value1", "data")
62                 // Inputs
63                 input("json-content", """{ "name" : "cds"}""")
64                 // Outputs
65                 output("self-attribute-expression", getAttribute("template1-data"))
66                 // Artifacts
67                 artifact("template2", "artifact-template-velocity", "Templates/template1.vtl")
68
69             }
70
71             workflow("resource-resolution-process", "Resource Resolution wf") {
72                 input("json-content", "json", true, "")
73                 input("key-1", "string", true, "")
74                 output("status", "string", true, "success")
75                 step("resource-resolution-call", "resource-resolution", "Resource Resolution component invoke")
76             }
77         }
78         assertNotNull(blueprint.components, "failed to get components")
79         assertNotNull(blueprint.workflows, "failed to get workflows")
80         //println(blueprint.asJsonString(true))
81
82         val serviceTemplateGenerator = BluePrintServiceTemplateGenerator(blueprint)
83         val serviceTemplate = serviceTemplateGenerator.serviceTemplate()
84         assertNotNull(serviceTemplate.topologyTemplate, "failed to get service topology template")
85         //println(serviceTemplate.asJsonString(true))
86     }
87
88     @Test
89     fun testServiceTemplate() {
90         val serviceTemplate = serviceTemplate("sample-bp", "1.0.0",
91                 "brindasanth@onap.com", "sample, blueprints") {
92             metadata("release", "1806")
93             import("Definition/data_types.json")
94             dsl("rest-endpoint", """{ "selector" : "odl-selector"}""")
95             dsl("db-endpoint", """{ "selector" : "db-selector"}""")
96             topologyTemplate {
97                 nodeTemplateOperation(nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor",
98                         description = "sample activation") {
99                     implementation(360, "SELF") {
100                         primary("Scripts/sample.py")
101                         dependencies("one", "two")
102                     }
103                     inputs {
104                         property("json-content", """{ "name" : "cds"}""")
105                         property("array-content", """["controller", "blueprints"]""")
106                         property("int-value", 234)
107                         property("boolean-value", true)
108                         property("string-value", "sample")
109                         property("input-expression", getInput("key-1"))
110                         property("self-property-expression", getProperty("key-1"))
111                         property("self-artifact-expression", getArtifact("key-1"))
112                         property("other-artifact-expression", getNodeTemplateArtifact("node-1", "key-1"))
113                     }
114                     outputs {
115                         property("self-attribute-expression", getAttribute("key-1"))
116                     }
117                 }
118                 // Other way of defining Node Template with artifacts, implementation
119                 nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") {
120                     operation("ResourceResolutionExecutor", "") {
121                         implementation(180)
122                         inputs {
123                             property("boolean-value", true)
124                             property("string-value", "sample")
125                         }
126                         outputs {
127                             property("resolve-expression", getAttribute("key-1"))
128                         }
129                     }
130                     artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl")
131                 }
132
133                 workflow("resource-resolution", "to resolve resources") {
134                     step("resource-resolution-call", "resolve", "Resource Resolution component invoke")
135                 }
136                 // Alternate way to define workflow
137                 workflow("activate", "to resolve resources") {
138                     // Alternate step definition
139                     step("netconf-activate-call", "activate", "call activation component") {
140                         success("END")
141                         failure("END")
142                     }
143                     inputs {
144                         property("request-content", "json", true)
145                     }
146                     outputs {
147                         property("response-content", "json", true) {
148                             value(getAttribute("key-1"))
149                             defaultValue("""{ "status" : "success"}""".jsonAsJsonType())
150                         }
151                     }
152                 }
153             }
154         }
155
156         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
157         assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes")
158         assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"], "failed to get nodeTypes(activate)")
159         //println(serviceTemplate.asJsonString(true))
160     }
161
162     @Test
163     fun testNodeTypePropertyConstrains() {
164         val nodeType = nodeType("data-node", "1.0.0", "tosca.Nodes.root", "") {
165             property("ip-address", "string", true, "") {
166                 defaultValue("127.0.0.1")
167                 constrain {
168                     validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive()))
169                     length(10)
170                     maxLength(20)
171                     minLength(10)
172                 }
173
174             }
175             property("disk-space", "string", true, "") {
176                 defaultValue(10)
177                 constrain {
178                     validValues("""["200KB", "400KB"]""")
179                     equal("200KB")
180                     inRange("""["100KB", "500KB" ]""")
181                     maxLength("10MB")
182                     minLength("10KB")
183                 }
184                 constrain {
185                     validValues("""[ 200, 400]""")
186                     greaterOrEqual("10KB")
187                     greaterThan("20KB")
188                     lessOrEqual("200KB")
189                     lessThan("190KB")
190                 }
191             }
192         }
193         assertNotNull(nodeType, "failed to get nodeType")
194         // println(nodeType.asJsonString(true))
195     }
196
197     @Test
198     fun testServiceTemplateWorkflow() {
199         val serviceTemplate = serviceTemplate("sample-bp", "1.0.0",
200                 "brindasanth@onap.com", "sample, blueprints") {
201             topologyTemplate {
202                 workflowNodeTemplate("activate", "component-resource-resolution", "") {
203                     operation("ResourceResolutionExecutor", "") {
204                         inputs {
205                             property("string-value", "sample")
206                         }
207                     }
208                 }
209             }
210         }
211         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
212         assertNotNull(serviceTemplate.topologyTemplate?.workflows?.get("activate"), "failed to get workflow(activate)")
213         //println(serviceTemplate.asJsonString(true))
214     }
215
216     @Test
217     fun testNodeTemplateOperationTypes() {
218
219         val testNodeTemplateInstance = BluePrintTypes.nodeTemplateComponentTestExecutor(id = "test-node-template",
220                 description = "") {
221             operation("") {
222                 implementation(360)
223                 inputs {
224                     request("i am request")
225                 }
226                 outputs {
227                     response(getAttribute("attribute1"))
228                 }
229             }
230         }
231         assertNotNull(testNodeTemplateInstance, "failed to get test node template")
232         //println(testNodeTemplateInstance.asJsonString(true))
233     }
234 }
235
236 fun BluePrintTypes.nodeTemplateComponentTestExecutor(id: String,
237                                                      description: String,
238                                                      block: TestNodeTemplateImplBuilder.() -> Unit)
239         : NodeTemplate {
240     return TestNodeTemplateImplBuilder(id, description).apply(block).build()
241 }
242
243 class TestNodeTemplateImplBuilder(id: String, description: String) :
244         AbstractNodeTemplateImplBuilder<TestInput, TestOutput>(id, "component-test-executor",
245                 "ComponentTestExecutor",
246                 description)
247
248 class TestInput : PropertiesAssignmentBuilder() {
249     fun request(request: String) {
250         property("request", request.asJsonPrimitive())
251     }
252 }
253
254 class TestOutput : PropertiesAssignmentBuilder() {
255     fun response(response: String) {
256         response(response.asJsonPrimitive())
257     }
258
259     fun response(response: JsonNode) {
260         property("response", response)
261     }
262 }