Formatting Code base with ktlint
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintRuntimeServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018-2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintRuntimeUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.slf4j.LoggerFactory
30 import kotlin.test.assertEquals
31 import kotlin.test.assertNotNull
32
33 /**
34  *
35  *
36  * @author Brinda Santh
37  */
38 class BluePrintRuntimeServiceTest {
39
40     private val log = LoggerFactory.getLogger(this::class.toString())
41
42     @Test
43     fun `test Resolve NodeTemplate Properties`() {
44         log.info("************************ testResolveNodeTemplateProperties **********************")
45
46         val bluePrintRuntimeService = getBluePrintRuntimeService()
47
48         val inputDataPath = "src/test/resources/data/default-context.json"
49
50         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
51         bluePrintRuntimeService.assignInputs(inputNode)
52
53         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
54             .resolveNodeTemplateProperties("activate-process")
55
56         assertNotNull(propContext, "Failed to populate interface property values")
57     }
58
59     @Test
60     fun `test resolve NodeTemplate Capability Properties`() {
61         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
62         val bluePrintRuntimeService = getBluePrintRuntimeService()
63
64         val executionContext = bluePrintRuntimeService.getExecutionContext()
65
66         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
67             bluePrintRuntimeService.bluePrintContext(),
68             "data/default-context.json", executionContext
69         )
70
71         val assignmentParams = "{\n" +
72                 "            \"ipAddress\": \"127.0.0.1\",\n" +
73                 "            \"hostName\": \"vnf-host\"\n" +
74                 "          }"
75
76         bluePrintRuntimeService.setNodeTemplateAttributeValue(
77             "resource-assignment", "assignment-params",
78             JacksonUtils.jsonNode(assignmentParams)
79         )
80
81         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(
82             "sample-netconf-device",
83             "netconf"
84         )
85         assertNotNull(capProperties, "Failed to populate capability property values")
86         assertEquals(capProperties["target-ip-address"], "127.0.0.1".asJsonPrimitive(), "Failed to populate parameter target-ip-address")
87         assertEquals(capProperties["port-number"], JacksonUtils.jsonNodeFromObject(830), "Failed to populate parameter port-number")
88     }
89
90     @Test
91     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
92         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
93
94         val bluePrintRuntimeService = getBluePrintRuntimeService()
95
96         val executionContext = bluePrintRuntimeService.getExecutionContext()
97
98         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
99             bluePrintRuntimeService.bluePrintContext(),
100             "data/default-context.json", executionContext
101         )
102
103         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
104             .resolveNodeTemplateInterfaceOperationInputs(
105                 "resource-assignment",
106                 "ResourceResolutionComponent", "process"
107             )
108
109         assertNotNull(inContext, "Failed to populate interface input property values")
110         assertEquals(inContext["action-name"], JacksonUtils.jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name")
111         assertEquals(inContext["request-id"], JacksonUtils.jsonNodeFromObject("12345"), "Failed to populate parameter action-name")
112     }
113
114     @Test
115     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
116         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
117
118         val bluePrintRuntimeService = getBluePrintRuntimeService()
119
120         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params", NullNode.getInstance())
121
122         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs(
123             "resource-assignment",
124             "ResourceResolutionComponent", "process"
125         )
126
127         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
128             "resource-assignment",
129             "ResourceResolutionComponent", "process", "status"
130         )
131         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
132
133         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
134             "resource-assignment",
135             "ResourceResolutionComponent", "process", "resource-assignment-params"
136         )
137         assertEquals(NullNode.getInstance(), outputParams, "Failed to get operation property resource-assignment-params")
138     }
139
140     @Test
141     fun `test NodeTemplate Context Property`() {
142         log.info("************************ testNodeTemplateContextProperty **********************")
143         val bluePrintRuntimeService = getBluePrintRuntimeService()
144
145         bluePrintRuntimeService.setNodeTemplateAttributeValue(
146             "resource-assignment-ra-component", "context1",
147             JacksonUtils.jsonNodeFromObject("context1-value")
148         )
149         bluePrintRuntimeService.setNodeTemplateAttributeValue(
150             "resource-assignment-ra-component", "context2",
151             JacksonUtils.jsonNodeFromObject("context2-value")
152         )
153
154         val keys = listOf("context1", "context2")
155
156         val jsonValueNode = bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
157         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
158         log.info("JSON Prepared Value Context {}", jsonValueNode)
159     }
160
161     @Test
162     fun `test Resolve DSL Properties`() {
163         log.info("************************ resolveDSLExpression **********************")
164
165         val bluePrintRuntimeService = getBluePrintRuntimeService()
166
167         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
168
169         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
170         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
171     }
172
173     @Test
174     fun `test Resolve Workflow Outputs`() {
175         log.info("************************ resolvePropertyAssignments **********************")
176         val bluePrintRuntimeService = getBluePrintRuntimeService()
177
178         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
179
180         bluePrintRuntimeService.setNodeTemplateAttributeValue(
181             "resource-assignment", "assignment-params",
182             JacksonUtils.jsonNode(assignmentParams)
183         )
184
185         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
186         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
187     }
188
189     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
190         val blueprintBasePath = normalizedPathName("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
191         val blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
192         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
193         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
194
195         assertEquals(
196             "1234".asJsonPrimitive(),
197             checkProcessId, "Failed to get process id after runtime creation"
198         )
199         assertEquals(
200             blueprintBasePath.asJsonPrimitive(),
201             checkBasePath, "Failed to get base path after runtime creation"
202         )
203
204         return blueprintRuntime
205     }
206 }