bef1c457081e82bbe79d8e593b038587eeb941aa
[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.data.PropertyDefinition
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     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(bluePrintRuntimeService.bluePrintContext(),
67                 "data/default-context.json", executionContext)
68
69         val assignmentParams = "{\n" +
70                 "            \"ipAddress\": \"127.0.0.1\",\n" +
71                 "            \"hostName\": \"vnf-host\"\n" +
72                 "          }"
73
74         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
75                 JacksonUtils.jsonNode(assignmentParams))
76
77         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties("sample-netconf-device",
78                 "netconf")
79         assertNotNull(capProperties, "Failed to populate capability property values")
80         assertEquals(capProperties["target-ip-address"], "127.0.0.1".asJsonPrimitive(), "Failed to populate parameter target-ip-address")
81         assertEquals(capProperties["port-number"], JacksonUtils.jsonNodeFromObject(830), "Failed to populate parameter port-number")
82     }
83
84     @Test
85     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
86         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
87
88         val bluePrintRuntimeService = getBluePrintRuntimeService()
89
90         val executionContext = bluePrintRuntimeService.getExecutionContext()
91
92         BluePrintRuntimeUtils.assignInputsFromClassPathFile(bluePrintRuntimeService.bluePrintContext(),
93                 "data/default-context.json", executionContext)
94
95         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
96                 .resolveNodeTemplateInterfaceOperationInputs("resource-assignment",
97                         "ResourceResolutionComponent", "process")
98
99         assertNotNull(inContext, "Failed to populate interface input property values")
100         assertEquals(inContext["action-name"], JacksonUtils.jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name")
101         assertEquals(inContext["request-id"], JacksonUtils.jsonNodeFromObject("12345"), "Failed to populate parameter action-name")
102     }
103
104     @Test
105     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
106         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
107
108         val bluePrintRuntimeService = getBluePrintRuntimeService()
109
110         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params", NullNode.getInstance())
111
112         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs("resource-assignment",
113                 "ResourceResolutionComponent", "process")
114
115         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue("resource-assignment",
116                 "ResourceResolutionComponent", "process", "status")
117         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
118
119         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue("resource-assignment",
120                 "ResourceResolutionComponent", "process", "resource-assignment-params")
121         assertEquals(NullNode.getInstance(), outputParams, "Failed to get operation property resource-assignment-params")
122
123     }
124
125     @Test
126     fun `test NodeTemplate Context Property`() {
127         log.info("************************ testNodeTemplateContextProperty **********************")
128         val bluePrintRuntimeService = getBluePrintRuntimeService()
129
130         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context1",
131                 JacksonUtils.jsonNodeFromObject("context1-value"))
132         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context2",
133                 JacksonUtils.jsonNodeFromObject("context2-value"))
134
135         val keys = listOf("context1", "context2")
136
137         val jsonValueNode = bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
138         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
139         log.info("JSON Prepared Value Context {}", jsonValueNode)
140
141     }
142
143     @Test
144     fun `test Resolve DSL Properties`() {
145         log.info("************************ resolveDSLExpression **********************")
146
147         val bluePrintRuntimeService = getBluePrintRuntimeService()
148
149         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
150
151         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
152         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
153     }
154
155     @Test
156     fun `test Resolve Workflow Outputs`() {
157         log.info("************************ resolvePropertyAssignments **********************")
158         val bluePrintRuntimeService = getBluePrintRuntimeService()
159
160         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
161
162         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
163                 JacksonUtils.jsonNode(assignmentParams))
164
165         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
166         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
167     }
168
169     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
170         val blueprintBasePath = normalizedPathName("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
171         val blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
172         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
173         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
174
175         assertEquals("1234".asJsonPrimitive(),
176                 checkProcessId, "Failed to get process id after runtime creation")
177         assertEquals(blueprintBasePath.asJsonPrimitive(),
178                 checkBasePath, "Failed to get base path after runtime creation")
179
180         return blueprintRuntime
181     }
182
183 }