Enabling Code Formatter
[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             coEvery {
90                 componentFunctionScriptingService.cleanupInstance(any(), any())
91             } returns mockk()
92
93             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
94             every { raRuntimeService.bluePrintContext() } returns mockk()
95             every { raRuntimeService.getInputValue("fixed_ipv4_Address_01") } returns NullNode.getInstance()
96             every { raRuntimeService.getInputValue("fixed_ipv4_Address_02") } returns NullNode.getInstance()
97             every { raRuntimeService.getInputValue("fixed_ipv4_Address_03") } returns NullNode.getInstance()
98
99             every { raRuntimeService.getResolutionStore("CloudRegionId") } returns "cloud-123".asJsonPrimitive()
100             every { raRuntimeService.getResolutionStore("IpServiceName") } returns "MobilityPlan".asJsonPrimitive()
101
102             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
103             every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
104
105             val capabilityResourceResolutionProcessor =
106                 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
107             capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
108
109             capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions()
110             // log.info("ResourceAssignments Definitions : ${ capabilityResourceResolutionProcessor.resourceDictionaries.asJsonString(true)} ")
111             val resourceAssignments = resourceAssignments()
112             val resourceAssignmentList = resourceAssignments.values.toMutableList()
113             // log.info("ResourceAssignments Assignments : ${resourceAssignmentList.asJsonString(true)} ")
114             capabilityResourceResolutionProcessor.resourceAssignments = resourceAssignmentList
115
116             var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_01"]!!)
117             assertTrue(status, "failed to execute capability source")
118             assertEquals(
119                 "10.10.10.11".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_01"]!!.property!!.value,
120                 "assigned value miss match"
121             )
122
123             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_02"]!!)
124             assertTrue(status, "failed to execute capability source")
125             assertEquals(
126                 "10.10.10.12".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_02"]!!.property!!.value,
127                 "assigned value miss match"
128             )
129
130             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_03"]!!)
131             assertTrue(status, "failed to execute capability source")
132             assertEquals(
133                 "10.10.10.13".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_03"]!!.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 ipAddress = list.filter { it.name == "fixed_ipv4_Address_01" }
148
149             assertEquals(list.size, 5)
150             assertNotNull(ipAddress[0].keyIdentifiers)
151             assertNotNull(ipAddress[0].requestPayload)
152         }
153     }
154
155     /** Test dictionaries */
156
157     /** Test dictionaries */
158     private fun resourceDefinitions(): MutableMap<String, ResourceDefinition> {
159         return BluePrintTypes.resourceDefinitions {
160             resourceDefinition("CloudRegionId", "Cloud Region Id Resource Definition") {
161                 tags("CloudRegionId")
162                 updatedBy("saurav.paira@att.com")
163                 property("string", true)
164                 sources {
165                     sourceInput("input", "") {}
166                 }
167             }
168             resourceDefinition("IpServiceName", "Ip Service Name Resource Definition") {
169                 tags("IpServiceName")
170                 updatedBy("saurav.paira@att.com")
171                 property("string", true)
172                 sources {
173                     sourceInput("input", "") {}
174                 }
175             }
176             resourceDefinition("fixed_ipv4_Address_01", "fixed_ipv4_Address_01 Resource Definition") {
177                 tags("fixed_ipv4_Address_01")
178                 updatedBy("saurav.paira@att.com")
179                 property("string", true)
180                 sources {
181                     sourceCapability("ipassign-ms", "") {
182                         definedProperties {
183                             type("internal")
184                             scriptClassReference(IpAssignResolutionCapability::class)
185                             keyDependencies(
186                                 arrayListOf(
187                                     "CloudRegionId",
188                                     "IpServiceName"
189                                 )
190                             )
191                         }
192                     }
193                 }
194             }
195             resourceDefinition("fixed_ipv4_Address_02", "fixed_ipv4_Address_02 Resource Definition") {
196                 tags("fixed_ipv4_Address_01")
197                 updatedBy("saurav.paira@att.com")
198                 property("string", true)
199                 sources {
200                     sourceCapability("ipassign-ms", "") {
201                         definedProperties {
202                             type("internal")
203                             scriptClassReference(IpAssignResolutionCapability::class)
204                             keyDependencies(
205                                 arrayListOf(
206                                     "CloudRegionId",
207                                     "IpServiceName"
208                                 )
209                             )
210                         }
211                     }
212                 }
213             }
214             resourceDefinition("fixed_ipv4_Address_03", "fixed_ipv4_Address_03 Resource Definition") {
215                 tags("fixed_ipv4_Address_03")
216                 updatedBy("saurav.paira@att.com")
217                 property("string", true)
218                 sources {
219                     sourceCapability("ipassign-ms", "") {
220                         definedProperties {
221                             type("internal")
222                             scriptClassReference(IpAssignResolutionCapability::class)
223                             keyDependencies(
224                                 arrayListOf(
225                                     "CloudRegionId",
226                                     "IpServiceName"
227                                 )
228                             )
229                         }
230                     }
231                 }
232             }
233         }
234     }
235
236     private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
237         return BluePrintTypes.resourceAssignments {
238             resourceAssignment(
239                 name = "CloudRegionId", dictionaryName = "CloudRegionId",
240                 dictionarySource = "input"
241             ) {
242                 property("string", true, "")
243                 dependencies(arrayListOf())
244             }
245             resourceAssignment(
246                 name = "IpServiceName", dictionaryName = "IpServiceName",
247                 dictionarySource = "input"
248             ) {
249                 property("string", true, "")
250                 dependencies(arrayListOf())
251             }
252             resourceAssignment(
253                 name = "fixed_ipv4_Address_01", dictionaryName = "fixed_ipv4_Address_01",
254                 dictionarySource = "ipassign-ms"
255             ) {
256                 property("string", true, "")
257                 dependencies(
258                     arrayListOf(
259                         "CloudRegionId",
260                         "IpServiceName"
261                     )
262                 )
263             }
264             resourceAssignment(
265                 name = "fixed_ipv4_Address_02", dictionaryName = "fixed_ipv4_Address_02",
266                 dictionarySource = "ipassign-ms"
267             ) {
268                 property("string", true, "")
269                 dependencies(
270                     arrayListOf(
271                         "fixed_ipv4_Address_01"
272                     )
273                 )
274             }
275             resourceAssignment(
276                 name = "fixed_ipv4_Address_03", dictionaryName = "fixed_ipv4_Address_03",
277                 dictionarySource = "ipassign-ms"
278             ) {
279                 property("string", true, "")
280                 dependencies(
281                     arrayListOf(
282                         "fixed_ipv4_Address_02"
283                     )
284                 )
285             }
286         }
287     }
288 }