Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / capabilities / NamingResolutionCapabilityTest.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.capabilities
18
19 import com.fasterxml.jackson.databind.node.NullNode
20 import io.mockk.coEvery
21 import io.mockk.every
22 import io.mockk.mockk
23 import io.mockk.mockkObject
24 import kotlinx.coroutines.runBlocking
25 import org.junit.Before
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.CapabilityResourceResolutionProcessor
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceAssignments
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceDefinitions
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
32 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintRestLibPropertyService
33 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
34 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
35 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintError
36 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
37 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
39 import org.onap.ccsdk.cds.controllerblueprints.core.logger
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintDependencyService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
44 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
45 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
46 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
47 import kotlin.test.Test
48 import kotlin.test.assertEquals
49 import kotlin.test.assertNotNull
50 import kotlin.test.assertTrue
51
52 /**
53  * @author brindasanth
54  */
55
56 class NamingResolutionCapabilityTest {
57
58     private val log = logger(NamingResolutionCapabilityTest::class)
59
60     @Before
61     fun setup() {
62
63         mockkObject(BlueprintDependencyService)
64
65         val blueprintWebClientService = mockk<BlueprintWebClientService>()
66         // Create mock Response
67         val mockResponse = BlueprintWebClientService.WebClientResponse<String>(
68             200,
69             """{
70             "vf-module-name" : "dlsst001dbcx-adsf-Base-01",
71             "vnfc-name" : "dlsst001dbcx"
72             }
73         """.trimMargin()
74         )
75         every { blueprintWebClientService.exchangeResource(any(), any(), any()) } returns mockResponse
76
77         val restLibPropertyService = mockk<BlueprintRestLibPropertyService>()
78         every { restLibPropertyService.blueprintWebClientService("naming-ms") } returns blueprintWebClientService
79         every { BlueprintDependencyService.applicationContext.getBean(any()) } returns restLibPropertyService
80     }
81
82     @Test
83     fun testNamingResolutionCapability() {
84         runBlocking {
85             val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
86             coEvery {
87                 componentFunctionScriptingService
88                     .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
89             } returns NamingResolutionCapability()
90
91             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
92             every { raRuntimeService.getBlueprintError() } returns BlueprintError()
93             every { raRuntimeService.bluePrintContext() } returns mockk<BlueprintContext>()
94             every { raRuntimeService.getInputValue("vf-module-name") } returns NullNode.getInstance()
95             every { raRuntimeService.getInputValue("vnfc-name") } returns NullNode.getInstance()
96
97             every { raRuntimeService.getResolutionStore("policy-instance-name") } returns "SDNC_Policy.Config_MS_1806SRIOV_VNATJson.4.xml".asJsonPrimitive()
98             every { raRuntimeService.getResolutionStore("naming-code") } returns "dbc".asJsonPrimitive()
99             every { raRuntimeService.getResolutionStore("vnf-name") } returns "vnf-123".asJsonPrimitive()
100             every { raRuntimeService.getResolutionStore("vf-module-label") } returns "adsf".asJsonPrimitive()
101             every { raRuntimeService.getResolutionStore("vf-module-type") } returns "base".asJsonPrimitive()
102             every { raRuntimeService.getResolutionStore("cloud-region-id") } returns "region-123".asJsonPrimitive()
103
104             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
105             every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
106
107             val capabilityResourceResolutionProcessor =
108                 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
109             capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
110
111             capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions()
112             // log.info("ResourceAssignments Definitions : ${ capabilityResourceResolutionProcessor.resourceDictionaries.asJsonString(true)} ")
113             val resourceAssignments = resourceAssignments()
114             val resourceAssignmentList = resourceAssignments.values.toMutableList()
115             // log.info("ResourceAssignments Assignments : ${resourceAssignmentList.asJsonString(true)} ")
116             capabilityResourceResolutionProcessor.resourceAssignments = resourceAssignmentList
117
118             val bulkSequenced =
119                 BulkResourceSequencingUtils.process(capabilityResourceResolutionProcessor.resourceAssignments)
120             // log.info("Bulk Sequenced : ${bulkSequenced} ")
121             val resourceAssignment1 = resourceAssignments["vf-module-name"]
122             var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment1!!)
123             assertTrue(status, "failed to execute capability source")
124             assertEquals(
125                 "dlsst001dbcx-adsf-Base-01".asJsonPrimitive(), resourceAssignment1.property!!.value,
126                 "assigned value miss match"
127             )
128
129             val resourceAssignment2 = resourceAssignments["vnfc-name"]
130             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment2!!)
131             assertTrue(status, "failed to execute capability source")
132             assertEquals(
133                 "dlsst001dbcx".asJsonPrimitive(), resourceAssignment2.property!!.value,
134                 "assigned value miss match"
135             )
136
137             val resoulutionSummary =
138                 ResourceAssignmentUtils.generateResolutionSummaryData(
139                     resourceAssignments.values.toList(),
140                     capabilityResourceResolutionProcessor.resourceDictionaries
141                 )
142             log.info(resoulutionSummary.asJsonType().toPrettyString())
143             assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))
144
145             val summaries = JacksonUtils.jsonNode(resoulutionSummary)["resolution-summary"]
146             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
147             val vnfModuleName = list.filter { it.name == "vf-module-name" }
148
149             assertEquals(list.size, 9)
150             assertNotNull(vnfModuleName[0].keyIdentifiers)
151             assertNotNull(vnfModuleName[0].requestPayload)
152         }
153     }
154
155     /** Test dictionaries */
156     private fun resourceDefinitions(): MutableMap<String, ResourceDefinition> {
157         return BlueprintTypes.resourceDefinitions {
158             resourceDefinition("naming-code", "naming-code Resource Definition") {
159                 tags("naming-code")
160                 updatedBy("brindasanth@onap.com")
161                 property("string", true)
162                 sources {
163                     sourceInput("input", "") {}
164                 }
165             }
166             resourceDefinition("naming-type", "naming-type Resource Definition") {
167                 tags("naming-type")
168                 updatedBy("brindasanth@onap.com")
169                 property("string", true)
170                 sources {
171                     sourceInput("input", "") {}
172                 }
173             }
174             resourceDefinition("cloud-region-id", "cloud-region-id Resource Definition") {
175                 tags("cloud-region-id")
176                 updatedBy("brindasanth@onap.com")
177                 property("string", true)
178                 sources {
179                     sourceInput("input", "") {}
180                 }
181             }
182             resourceDefinition("policy-instance-name", "policy-instance-name Resource Definition") {
183                 tags("policy-instance-name")
184                 updatedBy("sp694w@att.com")
185                 property("string", true)
186                 sources {
187                     sourceInput("input", "") {}
188                 }
189             }
190             resourceDefinition("vnf-name", "vnf-name Resource Definition") {
191                 tags("vnf-name")
192                 updatedBy("sp694w@att.com")
193                 property("string", true)
194                 sources {
195                     sourceInput("input", "") {}
196                 }
197             }
198             resourceDefinition("vf-module-label", "vf-module-label Resource Definition") {
199                 tags("vf-module-label")
200                 updatedBy("sp694w@att.com")
201                 property("string", true)
202                 sources {
203                     sourceInput("input", "") {}
204                 }
205             }
206             resourceDefinition("vf-module-type", "vf-module-type Resource Definition") {
207                 tags("vf-module-type")
208                 updatedBy("sp694w@att.com")
209                 property("string", true)
210                 sources {
211                     sourceInput("input", "") {}
212                 }
213             }
214             resourceDefinition("vf-module-name", "vf-module-name Resource Definition") {
215                 tags("vf-module-name")
216                 updatedBy("brindasanth@onap.com")
217                 property("string", true) {
218                     metadata("naming-type", "VF-MODULE")
219                 }
220                 sources {
221                     sourceCapability("naming-ms", "") {
222                         definedProperties {
223                             type("internal")
224                             scriptClassReference(NamingResolutionCapability::class)
225                             keyDependencies(
226                                 arrayListOf(
227                                     "policy-instance-name",
228                                     "naming-code",
229                                     "vnf-name",
230                                     "vf-module-label",
231                                     "vf-module-type"
232                                 )
233                             )
234                         }
235                     }
236                 }
237             }
238             resourceDefinition("vnfc-name", "vnfc-name Resource Definition") {
239                 tags("vnfc-name")
240                 updatedBy("brindasanth@onap.com")
241                 property("string", true) {
242                     metadata("naming-type", "VNFC")
243                 }
244
245                 sources {
246                     sourceCapability("naming-ms", "") {
247                         definedProperties {
248                             type("internal")
249                             scriptClassReference(NamingResolutionCapability::class)
250                             keyDependencies(
251                                 arrayListOf("vf-module-name")
252                             )
253                         }
254                     }
255                 }
256             }
257             resourceDefinition("ra-dict-name-3", "ra-dict-name-3 Resource Definition") {
258                 tags("ra-dict-name-3")
259                 updatedBy("brindasanth@onap.com")
260                 property("string", true)
261                 sources {
262                     sourceCapability("naming-ms", "") {
263                         definedProperties {
264                             type("internal")
265                             scriptClassReference(NamingResolutionCapability::class)
266                             keyDependencies(
267                                 arrayListOf("vf-module-name")
268                             )
269                         }
270                     }
271                 }
272             }
273         }
274     }
275
276     private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
277         return BlueprintTypes.resourceAssignments {
278             resourceAssignment(
279                 name = "naming-code", dictionaryName = "naming-code",
280                 dictionarySource = "input"
281             ) {
282                 property("string", true, "")
283                 dependencies(arrayListOf())
284             }
285             resourceAssignment(
286                 name = "naming-type", dictionaryName = "naming-type",
287                 dictionarySource = "input"
288             ) {
289                 property("string", true, "")
290                 dependencies(arrayListOf())
291             }
292             resourceAssignment(
293                 name = "cloud-region-id", dictionaryName = " cloud-region-id",
294                 dictionarySource = "input"
295             ) {
296                 property("string", true, "")
297                 dependencies(arrayListOf())
298             }
299             resourceAssignment(
300                 name = "policy-instance-name", dictionaryName = " policy-instance-name",
301                 dictionarySource = "input"
302             ) {
303                 property("string", true, "")
304                 dependencies(arrayListOf())
305             }
306             resourceAssignment(
307                 name = "vnf-name", dictionaryName = " vnf-name",
308                 dictionarySource = "input"
309             ) {
310                 property("string", true, "")
311                 dependencies(arrayListOf())
312             }
313             resourceAssignment(
314                 name = "vf-module-label", dictionaryName = " vf-module-label",
315                 dictionarySource = "input"
316             ) {
317                 property("string", true, "")
318                 dependencies(arrayListOf())
319             }
320             resourceAssignment(
321                 name = "vf-module-type", dictionaryName = " vf-module-type",
322                 dictionarySource = "input"
323             ) {
324                 property("string", true, "")
325                 dependencies(arrayListOf())
326             }
327             resourceAssignment(
328                 name = "vf-module-name", dictionaryName = "vf-module-name",
329                 dictionarySource = "naming-ms"
330             ) {
331                 property("string", true, "")
332                 dependencies(
333                     arrayListOf(
334                         "policy-instance-name",
335                         "naming-code",
336                         "vnf-name",
337                         "vf-module-label",
338                         "vf-module-type"
339                     )
340                 )
341             }
342             resourceAssignment(
343                 name = "vnfc-name", dictionaryName = "vnfc-name",
344                 dictionarySource = "naming-ms"
345             ) {
346                 property("string", true, "")
347                 dependencies(
348                     arrayListOf(
349                         "vf-module-name"
350                     )
351                 )
352             }
353         }
354     }
355 }