2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Assert
25 import org.junit.Before
27 import org.junit.runner.RunWith
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.MockCapabilityScriptRA
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
33 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
34 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintError
35 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BlueprintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
40 import org.slf4j.LoggerFactory
41 import org.springframework.beans.factory.annotation.Autowired
42 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
43 import org.springframework.context.ApplicationContext
44 import org.springframework.context.annotation.ComponentScan
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.TestPropertySource
47 import org.springframework.test.context.junit4.SpringRunner
48 import kotlin.test.assertEquals
49 import kotlin.test.assertNotNull
50 import kotlin.test.assertTrue
53 * ResourceResolutionServiceTest
55 * @author Brinda Santh DATE : 8/15/2018
57 @RunWith(SpringRunner::class)
58 @ContextConfiguration(
59 classes = [TestDatabaseConfiguration::class]
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
63 @EnableAutoConfiguration
64 class ResourceResolutionServiceTest {
66 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
69 lateinit var resourceResolutionService: ResourceResolutionService
71 private val props = hashMapOf<String, Any>()
72 private val resolutionKey = "resolutionKey"
73 private val resourceId = "1"
74 private val resourceType = "ServiceInstance"
75 private val occurrence = 0
79 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
80 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
81 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
82 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
83 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
84 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = false
88 fun testRegisteredSource() {
89 val sources = resourceResolutionService.registeredResourceSources()
90 assertNotNull(sources, "failed to get registered sources")
94 "source-input", "source-default", "source-db",
95 "source-rest", "source-capability"
98 "failed to get registered sources : $sources"
103 @Throws(Exception::class)
104 fun testResolveResource() {
107 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
109 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
111 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
114 val executionServiceInput =
115 JacksonUtils.readValueFromClassPathFile(
116 "payload/requests/sample-resourceresolution-request.json",
117 ExecutionServiceInput::class.java
120 val resourceAssignmentRuntimeService =
121 ResourceAssignmentUtils.transformToRARuntimeService(
122 bluePrintRuntimeService,
123 "testResolveResource"
127 PayloadUtils.prepareInputsFromWorkflowPayload(
128 bluePrintRuntimeService,
129 executionServiceInput.payload,
130 "resource-assignment"
133 resourceResolutionService.resolveResources(
134 resourceAssignmentRuntimeService,
135 "resource-assignment",
139 }.let { (templateMap, assignmentList) ->
140 assertEquals("This is Sample Velocity Template", templateMap)
142 val expectedAssignmentList = mutableListOf(
143 "service-instance-id" to "siid_1234",
144 "vnf-id" to "vnf_1234",
145 "vnf_name" to "temp_vnf"
147 assertEquals(expectedAssignmentList.size, assignmentList.size)
149 val areEqual = expectedAssignmentList.zip(assignmentList).all { (it1, it2) ->
150 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
152 assertEquals(true, areEqual)
157 @Throws(Exception::class)
158 fun testResolveResources() {
159 val artifactNames = listOf("baseconfig", "another")
161 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
163 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
165 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
168 val executionServiceInput =
169 JacksonUtils.readValueFromClassPathFile(
170 "payload/requests/sample-resourceresolution-request.json",
171 ExecutionServiceInput::class.java
175 PayloadUtils.prepareInputsFromWorkflowPayload(
176 bluePrintRuntimeService,
177 executionServiceInput.payload,
178 "resource-assignment"
181 resourceResolutionService.resolveResources(
182 bluePrintRuntimeService,
183 "resource-assignment",
188 assertEquals(artifactNames.toSet(), it.templateMap.keys)
189 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
191 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
192 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
197 @Throws(Exception::class)
198 fun testResolveResourcesWithMappingAndTemplate() {
200 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
202 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
204 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
207 val executionServiceInput =
208 JacksonUtils.readValueFromClassPathFile(
209 "payload/requests/sample-resourceresolution-request.json",
210 ExecutionServiceInput::class.java
213 val resourceAssignmentRuntimeService =
214 ResourceAssignmentUtils.transformToRARuntimeService(
215 bluePrintRuntimeService,
216 "testResolveResourcesWithMappingAndTemplate"
219 val artifactPrefix = "another"
222 PayloadUtils.prepareInputsFromWorkflowPayload(
223 bluePrintRuntimeService,
224 executionServiceInput.payload,
225 "resource-assignment"
229 resourceResolutionService.resolveResources(
230 resourceAssignmentRuntimeService,
231 "resource-assignment",
235 "Couldn't Resolve Resources for artifact $artifactPrefix"
241 @Throws(Exception::class)
242 fun testResolveResourcesResolutionSummary() {
244 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
245 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
247 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
249 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
252 val executionServiceInput =
253 JacksonUtils.readValueFromClassPathFile(
254 "payload/requests/sample-resourceresolution-request.json",
255 ExecutionServiceInput::class.java
258 val resourceAssignmentRuntimeService =
259 ResourceAssignmentUtils.transformToRARuntimeService(
260 bluePrintRuntimeService,
261 "testResolveResourcesWithMappingAndTemplate"
264 val artifactPrefix = "notemplate"
267 PayloadUtils.prepareInputsFromWorkflowPayload(
268 bluePrintRuntimeService,
269 executionServiceInput.payload,
270 "resource-assignment"
273 resourceResolutionService.resolveResources(
274 resourceAssignmentRuntimeService,
275 "resource-assignment",
280 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
281 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
282 assertEquals(list.size, 3)
287 @Throws(Exception::class)
288 fun testResolveResourcesWithoutTemplate() {
289 val artifactPrefix = "notemplate"
291 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
293 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
295 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
298 val executionServiceInput =
299 JacksonUtils.readValueFromClassPathFile(
300 "payload/requests/sample-resourceresolution-request.json",
301 ExecutionServiceInput::class.java
304 val resourceAssignmentRuntimeService =
305 ResourceAssignmentUtils.transformToRARuntimeService(
306 bluePrintRuntimeService,
307 "testResolveResourcesWithMappingAndTemplate"
311 PayloadUtils.prepareInputsFromWorkflowPayload(
312 bluePrintRuntimeService,
313 executionServiceInput.payload,
314 "resource-assignment"
317 resourceResolutionService.resolveResources(
318 resourceAssignmentRuntimeService,
319 "resource-assignment",
327 "service-instance-id" : "siid_1234",
328 "vnf-id" : "vnf_1234",
329 "vnf_name" : "temp_vnf"
334 val areEqual = it.second.first().name == "service-instance-id" &&
335 "siid_1234" == it.second.first().property?.value?.asText() ?: null
336 assertEquals(true, areEqual)
341 fun testResolveResourcesWithResourceIdAndResourceType() {
342 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
344 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
346 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
348 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
351 val executionServiceInput =
352 JacksonUtils.readValueFromClassPathFile(
353 "payload/requests/sample-resourceresolution-request.json",
354 ExecutionServiceInput::class.java
357 val resourceAssignmentRuntimeService =
358 ResourceAssignmentUtils.transformToRARuntimeService(
359 bluePrintRuntimeService,
360 "testResolveResourcesWithMappingAndTemplate"
363 val artifactPrefix = "another"
366 PayloadUtils.prepareInputsFromWorkflowPayload(
367 bluePrintRuntimeService,
368 executionServiceInput.payload,
369 "resource-assignment"
373 resourceResolutionService.resolveResources(
374 resourceAssignmentRuntimeService,
375 "resource-assignment",
379 "Couldn't Resolve Resources for artifact $artifactPrefix"
385 fun testResourceResolutionForDefinition() {
386 val resourceDefinitions = BlueprintTypes.resourceDefinitions {
387 resourceDefinition(name = "port-speed", description = "Port Speed") {
388 property(type = "string", required = true)
390 sourceCapability(id = "sdno", description = "SDNO Source") {
392 type(BlueprintConstants.SCRIPT_KOTLIN)
393 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
394 keyDependencies(arrayListOf("device-id"))
397 sourceDb(id = "sdnc", description = "SDNC Controller") {
399 endpointSelector("processor-db")
400 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
402 map("device_id", "\$device-id")
404 keyDependencies(arrayListOf("device-id"))
409 resourceDefinition(name = "device-id", description = "Device Id") {
410 property(type = "string", required = true) {
412 sourceInput(id = "input", description = "Dependency Source") {}
418 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
419 every { raRuntimeService.bluePrintContext() } returns mockk<BlueprintContext>()
420 every { raRuntimeService.getBlueprintError() } returns BlueprintError()
421 every { raRuntimeService.setBlueprintError(any()) } returns Unit
422 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
423 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
425 val applicationContext = mockk<ApplicationContext>()
426 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
427 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
428 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
430 val sources = arrayListOf<String>("sdno", "sdnc")
432 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
433 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
435 resourceDefinitions, "port-speed", sources
437 assertNotNull(resolvedResources, "failed to resolve the resources")