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()
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()
95 every { raRuntimeService.getResolutionStore("CloudRegionId") } returns "cloud-123".asJsonPrimitive()
96 every { raRuntimeService.getResolutionStore("IpServiceName") } returns "MobilityPlan".asJsonPrimitive()
98 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
99 every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
101 val capabilityResourceResolutionProcessor =
102 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
103 capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
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
112 var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_01"]!!)
113 assertTrue(status, "failed to execute capability source")
115 "10.10.10.11".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_01"]!!.property!!.value,
116 "assigned value miss match"
119 status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_02"]!!)
120 assertTrue(status, "failed to execute capability source")
122 "10.10.10.12".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_02"]!!.property!!.value,
123 "assigned value miss match"
126 status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_03"]!!)
127 assertTrue(status, "failed to execute capability source")
129 "10.10.10.13".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_03"]!!.property!!.value,
130 "assigned value miss match"
133 val resoulutionSummary =
134 ResourceAssignmentUtils.generateResolutionSummaryData(
135 resourceAssignments.values.toList(),
136 capabilityResourceResolutionProcessor.resourceDictionaries
138 log.info(resoulutionSummary.asJsonType().toPrettyString())
139 assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))
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" }
145 assertEquals(list.size, 5)
146 assertNotNull(ipAddress[0].keyIdentifiers)
147 assertNotNull(ipAddress[0].requestPayload)
151 /** Test dictionaries */
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)
161 sourceInput("input", "") {}
164 resourceDefinition("IpServiceName", "Ip Service Name Resource Definition") {
165 tags("IpServiceName")
166 updatedBy("saurav.paira@att.com")
167 property("string", true)
169 sourceInput("input", "") {}
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)
177 sourceCapability("ipassign-ms", "") {
180 scriptClassReference(IpAssignResolutionCapability::class)
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)
196 sourceCapability("ipassign-ms", "") {
199 scriptClassReference(IpAssignResolutionCapability::class)
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)
215 sourceCapability("ipassign-ms", "") {
218 scriptClassReference(IpAssignResolutionCapability::class)
232 private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
233 return BluePrintTypes.resourceAssignments {
235 name = "CloudRegionId", dictionaryName = "CloudRegionId",
236 dictionarySource = "input"
238 property("string", true, "")
239 dependencies(arrayListOf())
242 name = "IpServiceName", dictionaryName = "IpServiceName",
243 dictionarySource = "input"
245 property("string", true, "")
246 dependencies(arrayListOf())
249 name = "fixed_ipv4_Address_01", dictionaryName = "fixed_ipv4_Address_01",
250 dictionarySource = "ipassign-ms"
252 property("string", true, "")
261 name = "fixed_ipv4_Address_02", dictionaryName = "fixed_ipv4_Address_02",
262 dictionarySource = "ipassign-ms"
264 property("string", true, "")
267 "fixed_ipv4_Address_01"
272 name = "fixed_ipv4_Address_03", dictionaryName = "fixed_ipv4_Address_03",
273 dictionarySource = "ipassign-ms"
275 property("string", true, "")
278 "fixed_ipv4_Address_02"