Metadata for name, version, tags and type
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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.TestConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintRuntimeUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31 import kotlin.test.assertEquals
32 import kotlin.test.assertNotNull
33
34 /**
35  *
36  *
37  * @author Brinda Santh
38  */
39 class BluePrintRuntimeServiceTest {
40
41     private val log = LoggerFactory.getLogger(this::class.toString())
42
43     @Test
44     fun `test Resolve NodeTemplate Properties`() {
45         log.info("************************ testResolveNodeTemplateProperties **********************")
46
47         val bluePrintRuntimeService = getBluePrintRuntimeService()
48
49         val inputDataPath = "src/test/resources/data/default-context.json"
50
51         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
52         bluePrintRuntimeService.assignInputs(inputNode)
53
54         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
55             .resolveNodeTemplateProperties("activate-process")
56
57         assertNotNull(propContext, "Failed to populate interface property values")
58     }
59
60     @Test
61     fun `test resolve NodeTemplate Capability Properties`() {
62         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
63         val bluePrintRuntimeService = getBluePrintRuntimeService()
64
65         val executionContext = bluePrintRuntimeService.getExecutionContext()
66
67         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
68             bluePrintRuntimeService.bluePrintContext(),
69             "data/default-context.json", executionContext
70         )
71
72         val assignmentParams = "{\n" +
73             "            \"ipAddress\": \"127.0.0.1\",\n" +
74             "            \"hostName\": \"vnf-host\"\n" +
75             "          }"
76
77         bluePrintRuntimeService.setNodeTemplateAttributeValue(
78             "resource-assignment", "assignment-params",
79             JacksonUtils.jsonNode(assignmentParams)
80         )
81
82         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(
83             "sample-netconf-device",
84             "netconf"
85         )
86         assertNotNull(capProperties, "Failed to populate capability property values")
87         assertEquals(
88             capProperties["target-ip-address"],
89             "127.0.0.1".asJsonPrimitive(),
90             "Failed to populate parameter target-ip-address"
91         )
92         assertEquals(
93             capProperties["port-number"],
94             JacksonUtils.jsonNodeFromObject(830),
95             "Failed to populate parameter port-number"
96         )
97     }
98
99     @Test
100     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
101         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
102
103         val bluePrintRuntimeService = getBluePrintRuntimeService()
104
105         val executionContext = bluePrintRuntimeService.getExecutionContext()
106
107         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
108             bluePrintRuntimeService.bluePrintContext(),
109             "data/default-context.json", executionContext
110         )
111
112         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
113             .resolveNodeTemplateInterfaceOperationInputs(
114                 "resource-assignment",
115                 "ResourceResolutionComponent", "process"
116             )
117
118         assertNotNull(inContext, "Failed to populate interface input property values")
119         assertEquals(
120             inContext["action-name"],
121             JacksonUtils.jsonNodeFromObject("sample-action"),
122             "Failed to populate parameter action-name"
123         )
124         assertEquals(
125             inContext["request-id"],
126             JacksonUtils.jsonNodeFromObject("12345"),
127             "Failed to populate parameter action-name"
128         )
129     }
130
131     @Test
132     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
133         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
134
135         val bluePrintRuntimeService = getBluePrintRuntimeService()
136
137         bluePrintRuntimeService.setNodeTemplateAttributeValue(
138             "resource-assignment",
139             "assignment-params",
140             NullNode.getInstance()
141         )
142
143         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs(
144             "resource-assignment",
145             "ResourceResolutionComponent", "process"
146         )
147
148         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
149             "resource-assignment",
150             "ResourceResolutionComponent", "process", "status"
151         )
152         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
153
154         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
155             "resource-assignment",
156             "ResourceResolutionComponent", "process", "resource-assignment-params"
157         )
158         assertEquals(
159             NullNode.getInstance(),
160             outputParams,
161             "Failed to get operation property resource-assignment-params"
162         )
163     }
164
165     @Test
166     fun `test NodeTemplate Context Property`() {
167         log.info("************************ testNodeTemplateContextProperty **********************")
168         val bluePrintRuntimeService = getBluePrintRuntimeService()
169
170         bluePrintRuntimeService.setNodeTemplateAttributeValue(
171             "resource-assignment-ra-component", "context1",
172             JacksonUtils.jsonNodeFromObject("context1-value")
173         )
174         bluePrintRuntimeService.setNodeTemplateAttributeValue(
175             "resource-assignment-ra-component", "context2",
176             JacksonUtils.jsonNodeFromObject("context2-value")
177         )
178
179         val keys = listOf("context1", "context2")
180
181         val jsonValueNode =
182             bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
183         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
184         log.info("JSON Prepared Value Context {}", jsonValueNode)
185     }
186
187     @Test
188     fun `test Resolve DSL Properties`() {
189         log.info("************************ resolveDSLExpression **********************")
190
191         val bluePrintRuntimeService = getBluePrintRuntimeService()
192
193         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
194
195         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
196         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
197     }
198
199     @Test
200     fun `test Resolve Workflow Outputs`() {
201         log.info("************************ resolvePropertyAssignments **********************")
202         val bluePrintRuntimeService = getBluePrintRuntimeService()
203
204         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
205
206         bluePrintRuntimeService.setNodeTemplateAttributeValue(
207             "resource-assignment", "assignment-params",
208             JacksonUtils.jsonNode(assignmentParams)
209         )
210
211         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
212         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
213     }
214
215     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
216         val blueprintBasePath = normalizedPathName(TestConstants.PATH_TEST_BLUEPRINTS_BASECONFIG)
217         val blueprintRuntime = BluePrintMetadataUtils.bluePrintRuntime("1234", blueprintBasePath)
218         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
219         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
220
221         assertEquals(
222             "1234".asJsonPrimitive(),
223             checkProcessId, "Failed to get process id after runtime creation"
224         )
225         assertEquals(
226             blueprintBasePath.asJsonPrimitive(),
227             checkBasePath, "Failed to get base path after runtime creation"
228         )
229
230         return blueprintRuntime
231     }
232 }