Add resource assignment DSL.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceDefinitionDSLTest.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.blueprintsprocessor.functions.resource.resolution
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
20 import kotlin.test.Test
21 import kotlin.test.assertEquals
22 import kotlin.test.assertNotNull
23
24 class ResourceDefinitionDSLTest {
25
26     @Test
27     fun testResourceDefinitionDSL() {
28         val testResourceDefinition = BluePrintTypes.resourceDefinition("service-instance-id",
29                 "VFW Service Instance Name") {
30             tags("service-instance-name, vfw, resources")
31             updatedBy("brindasanth@onap.com")
32             property("string", true)
33             sources {
34                 sourceInput("input", "") {}
35                 sourceDefault("default", "") {}
36                 sourceDb("sdnctl", "") {
37                     definedProperties {
38                         type("SQL")
39                         query("SELECT name FROM SERVICE_INSTANCE WHERE id = \$id")
40                         endpointSelector("db-source-endpoint")
41                         inputKeyMapping {
42                             map("id", "\$service-instance-id")
43                         }
44                         outputKeyMapping {
45                             map("service-instance-name", "\$name")
46                         }
47                         keyDependencies(arrayListOf("service-instance-id"))
48                     }
49                 }
50                 sourceRest("odl-mdsal", "") {
51                     definedProperties {
52                         type("JSON")
53                         endpointSelector("rest-source-endpoint")
54                         expressionType("JSON_PATH")
55                         urlPath("/service-instance/\$id")
56                         path(".\$name")
57                         verb("GET")
58                         payload("sample payload")
59                         inputKeyMapping {
60                             map("id", "\$service-instance-id")
61                         }
62                         outputKeyMapping {
63                             map("service-instance-name", "\$name")
64                         }
65                         keyDependencies(arrayListOf("service-instance-id"))
66                     }
67                 }
68                 sourceCapability("custom-component", "") {
69                     definedProperties {
70                         type("kotlin")
71                         scriptClassReference("Scripts/ServiceInstance.kt")
72                         keyDependencies(arrayListOf("service-instance-id"))
73                     }
74                 }
75             }
76         }
77         //println(resourceDefinition.asJsonString(true))
78         assertNotNull(testResourceDefinition, "failed to generate testResourceDefinition")
79
80         val testResourceDefinitions = BluePrintTypes.resourceDefinitions {
81             resourceDefinition(testResourceDefinition)
82         }
83         assertNotNull(testResourceDefinitions, "failed to generate testResourceDefinitions")
84         assertEquals(1, testResourceDefinitions.size, "testResourceDefinitions size doesn't match")
85     }
86
87     @Test
88     fun testResourceAssignment() {
89         val testResourceAssignment = BluePrintTypes.resourceAssignment("instance-name",
90                 "service-instance-name", "odl-mdsal") {
91             inputParameter(true)
92             property("string", true)
93             dependencies(arrayListOf("service-instance-id"))
94         }
95         //println(resourceAssignment.asJsonString(true))
96         assertNotNull(testResourceAssignment, "failed to generate resourceAssignment")
97
98         val testResourceAssignments = BluePrintTypes.resourceAssignments {
99             resourceAssignment(testResourceAssignment)
100             resourceAssignment("instance-name1",
101                     "service-instance-name", "odl-mdsal") {
102                 inputParameter(true)
103                 property("string", true)
104                 dependencies(arrayListOf("service-instance-id"))
105             }
106         }
107         //println(testResourceAssignments.asJsonString(true))
108         assertNotNull(testResourceAssignments, "failed to generate testResourceAssignments")
109         assertEquals(2, testResourceAssignments.size, "testResourceAssignments size doesn't match")
110     }
111 }