Migrate ccsdk/apps to ccsdk/cds
[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.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import com.fasterxml.jackson.databind.JsonNode
23 import com.fasterxml.jackson.databind.node.NullNode
24 import org.junit.Test
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintRuntimeUtils
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
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: EELFLogger = EELFManager.getInstance().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", PropertyDefinition(), "sample-username"
150                 .asJsonPrimitive())
151
152         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
153         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
154     }
155
156     @Test
157     fun `test Resolve Workflow Outputs`() {
158         log.info("************************ resolvePropertyAssignments **********************")
159         val bluePrintRuntimeService = getBluePrintRuntimeService()
160
161         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
162
163         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
164                 JacksonUtils.jsonNode(assignmentParams))
165
166         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
167         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
168     }
169
170     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
171         val blueprintBasePath: String = ("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
172         val blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
173         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
174
175         assertEquals(blueprintBasePath.asJsonPrimitive(), checkBasePath, "Failed to get base path after runtime creation")
176
177         return blueprintRuntime
178     }
179
180 }