Revert "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 / IpAssignResolutionCapabilityTest.kt
1 /*
2  * Copyright © 2019 AT&T.
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.BluePrintTypes
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
37 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
38 import org.onap.ccsdk.cds.controllerblueprints.core.logger
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
43 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
44 import kotlin.test.Test
45 import kotlin.test.assertEquals
46 import kotlin.test.assertNotNull
47 import kotlin.test.assertTrue
48
49 /**
50  * @author saurav.paira
51  */
52
53 class IpAssignResolutionCapabilityTest {
54
55     val log = logger(IpAssignResolutionCapabilityTest::class)
56
57     @Before
58     fun setup() {
59
60         mockkObject(BluePrintDependencyService)
61
62         val blueprintWebClientService = mockk<BlueprintWebClientService>()
63         // Create mock Response
64         val mockResponse = BlueprintWebClientService.WebClientResponse(
65             200,
66             """{
67             "fixed_ipv4_Address_01" : "10.10.10.11",
68             "fixed_ipv4_Address_02" : "10.10.10.12",
69             "fixed_ipv4_Address_03" : "10.10.10.13"
70             }
71         """.trimMargin()
72         )
73         every { blueprintWebClientService.exchangeResource(any(), any(), any()) } returns mockResponse
74
75         val restLibPropertyService = mockk<BluePrintRestLibPropertyService>()
76         every { restLibPropertyService.blueprintWebClientService("ipassign-ms") } returns blueprintWebClientService
77         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns restLibPropertyService
78     }
79
80     @Test
81     fun testIpAssignResolutionCapability() {
82         runBlocking {
83             val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
84             coEvery {
85                 componentFunctionScriptingService
86                     .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
87             } returns IpAssignResolutionCapability()
88
89             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
90             every { raRuntimeService.bluePrintContext() } returns mockk()
91             every { raRuntimeService.getInputValue("fixed_ipv4_Address_01") } returns NullNode.getInstance()
92             every { raRuntimeService.getInputValue("fixed_ipv4_Address_02") } returns NullNode.getInstance()
93             every { raRuntimeService.getInputValue("fixed_ipv4_Address_03") } returns NullNode.getInstance()
94
95             every { raRuntimeService.getResolutionStore("CloudRegionId") } returns "cloud-123".asJsonPrimitive()
96             every { raRuntimeService.getResolutionStore("IpServiceName") } returns "MobilityPlan".asJsonPrimitive()
97
98             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
99             every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
100
101             val capabilityResourceResolutionProcessor =
102                 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
103             capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
104
105             capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions()
106             // log.info("ResourceAssignments Definitions : ${ capabilityResourceResolutionProcessor.resourceDictionaries.asJsonString(true)} ")
107             val resourceAssignments = resourceAssignments()
108             val resourceAssignmentList = resourceAssignments.values.toMutableList()
109             // log.info("ResourceAssignments Assignments : ${resourceAssignmentList.asJsonString(true)} ")
110             capabilityResourceResolutionProcessor.resourceAssignments = resourceAssignmentList
111
112             var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_01"]!!)
113             assertTrue(status, "failed to execute capability source")
114             assertEquals(
115                 "10.10.10.11".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_01"]!!.property!!.value,
116                 "assigned value miss match"
117             )
118
119             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_02"]!!)
120             assertTrue(status, "failed to execute capability source")
121             assertEquals(
122                 "10.10.10.12".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_02"]!!.property!!.value,
123                 "assigned value miss match"
124             )
125
126             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_03"]!!)
127             assertTrue(status, "failed to execute capability source")
128             assertEquals(
129                 "10.10.10.13".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_03"]!!.property!!.value,
130                 "assigned value miss match"
131             )
132
133             val resoulutionSummary =
134                 ResourceAssignmentUtils.generateResolutionSummaryData(
135                     resourceAssignments.values.toList(),
136                     capabilityResourceResolutionProcessor.resourceDictionaries
137                 )
138             log.info(resoulutionSummary.asJsonType().toPrettyString())
139             assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))
140
141             val summaries = JacksonUtils.jsonNode(resoulutionSummary)["resolution-summary"]
142             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
143             val ipAddress = list.filter { it.name == "fixed_ipv4_Address_01" }
144
145             assertEquals(list.size, 5)
146             assertNotNull(ipAddress[0].keyIdentifiers)
147             assertNotNull(ipAddress[0].requestPayload)
148         }
149     }
150
151     /** Test dictionaries */
152
153     /** Test dictionaries */
154     private fun resourceDefinitions(): MutableMap<String, ResourceDefinition> {
155         return BluePrintTypes.resourceDefinitions {
156             resourceDefinition("CloudRegionId", "Cloud Region Id Resource Definition") {
157                 tags("CloudRegionId")
158                 updatedBy("saurav.paira@att.com")
159                 property("string", true)
160                 sources {
161                     sourceInput("input", "") {}
162                 }
163             }
164             resourceDefinition("IpServiceName", "Ip Service Name Resource Definition") {
165                 tags("IpServiceName")
166                 updatedBy("saurav.paira@att.com")
167                 property("string", true)
168                 sources {
169                     sourceInput("input", "") {}
170                 }
171             }
172             resourceDefinition("fixed_ipv4_Address_01", "fixed_ipv4_Address_01 Resource Definition") {
173                 tags("fixed_ipv4_Address_01")
174                 updatedBy("saurav.paira@att.com")
175                 property("string", true)
176                 sources {
177                     sourceCapability("ipassign-ms", "") {
178                         definedProperties {
179                             type("internal")
180                             scriptClassReference(IpAssignResolutionCapability::class)
181                             keyDependencies(
182                                 arrayListOf(
183                                     "CloudRegionId",
184                                     "IpServiceName"
185                                 )
186                             )
187                         }
188                     }
189                 }
190             }
191             resourceDefinition("fixed_ipv4_Address_02", "fixed_ipv4_Address_02 Resource Definition") {
192                 tags("fixed_ipv4_Address_01")
193                 updatedBy("saurav.paira@att.com")
194                 property("string", true)
195                 sources {
196                     sourceCapability("ipassign-ms", "") {
197                         definedProperties {
198                             type("internal")
199                             scriptClassReference(IpAssignResolutionCapability::class)
200                             keyDependencies(
201                                 arrayListOf(
202                                     "CloudRegionId",
203                                     "IpServiceName"
204                                 )
205                             )
206                         }
207                     }
208                 }
209             }
210             resourceDefinition("fixed_ipv4_Address_03", "fixed_ipv4_Address_03 Resource Definition") {
211                 tags("fixed_ipv4_Address_03")
212                 updatedBy("saurav.paira@att.com")
213                 property("string", true)
214                 sources {
215                     sourceCapability("ipassign-ms", "") {
216                         definedProperties {
217                             type("internal")
218                             scriptClassReference(IpAssignResolutionCapability::class)
219                             keyDependencies(
220                                 arrayListOf(
221                                     "CloudRegionId",
222                                     "IpServiceName"
223                                 )
224                             )
225                         }
226                     }
227                 }
228             }
229         }
230     }
231
232     private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
233         return BluePrintTypes.resourceAssignments {
234             resourceAssignment(
235                 name = "CloudRegionId", dictionaryName = "CloudRegionId",
236                 dictionarySource = "input"
237             ) {
238                 property("string", true, "")
239                 dependencies(arrayListOf())
240             }
241             resourceAssignment(
242                 name = "IpServiceName", dictionaryName = "IpServiceName",
243                 dictionarySource = "input"
244             ) {
245                 property("string", true, "")
246                 dependencies(arrayListOf())
247             }
248             resourceAssignment(
249                 name = "fixed_ipv4_Address_01", dictionaryName = "fixed_ipv4_Address_01",
250                 dictionarySource = "ipassign-ms"
251             ) {
252                 property("string", true, "")
253                 dependencies(
254                     arrayListOf(
255                         "CloudRegionId",
256                         "IpServiceName"
257                     )
258                 )
259             }
260             resourceAssignment(
261                 name = "fixed_ipv4_Address_02", dictionaryName = "fixed_ipv4_Address_02",
262                 dictionarySource = "ipassign-ms"
263             ) {
264                 property("string", true, "")
265                 dependencies(
266                     arrayListOf(
267                         "fixed_ipv4_Address_01"
268                     )
269                 )
270             }
271             resourceAssignment(
272                 name = "fixed_ipv4_Address_03", dictionaryName = "fixed_ipv4_Address_03",
273                 dictionarySource = "ipassign-ms"
274             ) {
275                 property("string", true, "")
276                 dependencies(
277                     arrayListOf(
278                         "fixed_ipv4_Address_02"
279                     )
280                 )
281             }
282         }
283     }
284 }