2 * Copyright © 2019 AT&T.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.capabilities
19 import com.fasterxml.jackson.databind.node.NullNode
20 import io.mockk.coEvery
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
50 * @author saurav.paira
53 class IpAssignResolutionCapabilityTest {
55 val log = logger(IpAssignResolutionCapabilityTest::class)
60 mockkObject(BluePrintDependencyService)
62 val blueprintWebClientService = mockk<BlueprintWebClientService>()
63 // Create mock Response
64 val mockResponse = BlueprintWebClientService.WebClientResponse(
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"
73 every { blueprintWebClientService.exchangeResource(any(), any(), any()) } returns mockResponse
75 val restLibPropertyService = mockk<BluePrintRestLibPropertyService>()
76 every { restLibPropertyService.blueprintWebClientService("ipassign-ms") } returns blueprintWebClientService
77 every { BluePrintDependencyService.applicationContext.getBean(any()) } returns restLibPropertyService
81 fun testIpAssignResolutionCapability() {
83 val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
85 componentFunctionScriptingService
86 .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
87 } returns IpAssignResolutionCapability()
90 componentFunctionScriptingService.cleanupInstance(any(), any())
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()
99 every { raRuntimeService.getResolutionStore("CloudRegionId") } returns "cloud-123".asJsonPrimitive()
100 every { raRuntimeService.getResolutionStore("IpServiceName") } returns "MobilityPlan".asJsonPrimitive()
102 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
103 every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
105 val capabilityResourceResolutionProcessor =
106 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
107 capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
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
116 var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_01"]!!)
117 assertTrue(status, "failed to execute capability source")
119 "10.10.10.11".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_01"]!!.property!!.value,
120 "assigned value miss match"
123 status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_02"]!!)
124 assertTrue(status, "failed to execute capability source")
126 "10.10.10.12".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_02"]!!.property!!.value,
127 "assigned value miss match"
130 status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_03"]!!)
131 assertTrue(status, "failed to execute capability source")
133 "10.10.10.13".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_03"]!!.property!!.value,
134 "assigned value miss match"
137 val resoulutionSummary =
138 ResourceAssignmentUtils.generateResolutionSummaryData(
139 resourceAssignments.values.toList(),
140 capabilityResourceResolutionProcessor.resourceDictionaries
142 log.info(resoulutionSummary.asJsonType().toPrettyString())
143 assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))
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" }
149 assertEquals(list.size, 5)
150 assertNotNull(ipAddress[0].keyIdentifiers)
151 assertNotNull(ipAddress[0].requestPayload)
155 /** Test dictionaries */
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)
165 sourceInput("input", "") {}
168 resourceDefinition("IpServiceName", "Ip Service Name Resource Definition") {
169 tags("IpServiceName")
170 updatedBy("saurav.paira@att.com")
171 property("string", true)
173 sourceInput("input", "") {}
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)
181 sourceCapability("ipassign-ms", "") {
184 scriptClassReference(IpAssignResolutionCapability::class)
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)
200 sourceCapability("ipassign-ms", "") {
203 scriptClassReference(IpAssignResolutionCapability::class)
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)
219 sourceCapability("ipassign-ms", "") {
222 scriptClassReference(IpAssignResolutionCapability::class)
236 private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
237 return BluePrintTypes.resourceAssignments {
239 name = "CloudRegionId", dictionaryName = "CloudRegionId",
240 dictionarySource = "input"
242 property("string", true, "")
243 dependencies(arrayListOf())
246 name = "IpServiceName", dictionaryName = "IpServiceName",
247 dictionarySource = "input"
249 property("string", true, "")
250 dependencies(arrayListOf())
253 name = "fixed_ipv4_Address_01", dictionaryName = "fixed_ipv4_Address_01",
254 dictionarySource = "ipassign-ms"
256 property("string", true, "")
265 name = "fixed_ipv4_Address_02", dictionaryName = "fixed_ipv4_Address_02",
266 dictionarySource = "ipassign-ms"
268 property("string", true, "")
271 "fixed_ipv4_Address_01"
276 name = "fixed_ipv4_Address_03", dictionaryName = "fixed_ipv4_Address_03",
277 dictionarySource = "ipassign-ms"
279 property("string", true, "")
282 "fixed_ipv4_Address_02"