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",
189 assertEquals(artifactNames.toSet(), it.templateMap.keys)
190 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
192 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
193 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
198 @Throws(Exception::class)
199 fun testResolveResourcesWithMappingAndTemplate() {
201 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
203 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
205 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
208 val executionServiceInput =
209 JacksonUtils.readValueFromClassPathFile(
210 "payload/requests/sample-resourceresolution-request.json",
211 ExecutionServiceInput::class.java
214 val resourceAssignmentRuntimeService =
215 ResourceAssignmentUtils.transformToRARuntimeService(
216 bluePrintRuntimeService,
217 "testResolveResourcesWithMappingAndTemplate"
220 val artifactPrefix = "another"
223 PayloadUtils.prepareInputsFromWorkflowPayload(
224 bluePrintRuntimeService,
225 executionServiceInput.payload,
226 "resource-assignment"
230 resourceResolutionService.resolveResources(
231 resourceAssignmentRuntimeService,
232 "resource-assignment",
236 "Couldn't Resolve Resources for artifact $artifactPrefix"
242 @Throws(Exception::class)
243 fun testResolveResourcesResolutionSummary() {
245 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
246 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
248 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
250 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
253 val executionServiceInput =
254 JacksonUtils.readValueFromClassPathFile(
255 "payload/requests/sample-resourceresolution-request.json",
256 ExecutionServiceInput::class.java
259 val resourceAssignmentRuntimeService =
260 ResourceAssignmentUtils.transformToRARuntimeService(
261 bluePrintRuntimeService,
262 "testResolveResourcesWithMappingAndTemplate"
265 val artifactPrefix = "notemplate"
268 PayloadUtils.prepareInputsFromWorkflowPayload(
269 bluePrintRuntimeService,
270 executionServiceInput.payload,
271 "resource-assignment"
274 resourceResolutionService.resolveResources(
275 resourceAssignmentRuntimeService,
276 "resource-assignment",
281 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
282 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
283 assertEquals(list.size, 3)
288 @Throws(Exception::class)
289 fun testResolveResourcesWithoutTemplate() {
290 val artifactPrefix = "notemplate"
292 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
294 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
296 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
299 val executionServiceInput =
300 JacksonUtils.readValueFromClassPathFile(
301 "payload/requests/sample-resourceresolution-request.json",
302 ExecutionServiceInput::class.java
305 val resourceAssignmentRuntimeService =
306 ResourceAssignmentUtils.transformToRARuntimeService(
307 bluePrintRuntimeService,
308 "testResolveResourcesWithMappingAndTemplate"
312 PayloadUtils.prepareInputsFromWorkflowPayload(
313 bluePrintRuntimeService,
314 executionServiceInput.payload,
315 "resource-assignment"
318 resourceResolutionService.resolveResources(
319 resourceAssignmentRuntimeService,
320 "resource-assignment",
328 "service-instance-id" : "siid_1234",
329 "vnf-id" : "vnf_1234",
330 "vnf_name" : "temp_vnf"
335 val areEqual = it.second.first().name == "service-instance-id" &&
336 "siid_1234" == it.second.first().property?.value?.asText() ?: null
337 assertEquals(true, areEqual)
342 fun testResolveResourcesWithResourceIdAndResourceType() {
343 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
345 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
347 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
349 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
352 val executionServiceInput =
353 JacksonUtils.readValueFromClassPathFile(
354 "payload/requests/sample-resourceresolution-request.json",
355 ExecutionServiceInput::class.java
358 val resourceAssignmentRuntimeService =
359 ResourceAssignmentUtils.transformToRARuntimeService(
360 bluePrintRuntimeService,
361 "testResolveResourcesWithMappingAndTemplate"
364 val artifactPrefix = "another"
367 PayloadUtils.prepareInputsFromWorkflowPayload(
368 bluePrintRuntimeService,
369 executionServiceInput.payload,
370 "resource-assignment"
374 resourceResolutionService.resolveResources(
375 resourceAssignmentRuntimeService,
376 "resource-assignment",
380 "Couldn't Resolve Resources for artifact $artifactPrefix"
386 fun testResourceResolutionForDefinition() {
387 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
388 resourceDefinition(name = "port-speed", description = "Port Speed") {
389 property(type = "string", required = true)
391 sourceCapability(id = "sdno", description = "SDNO Source") {
393 type(BluePrintConstants.SCRIPT_KOTLIN)
394 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
395 keyDependencies(arrayListOf("device-id"))
398 sourceDb(id = "sdnc", description = "SDNC Controller") {
400 endpointSelector("processor-db")
401 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
403 map("device_id", "\$device-id")
405 keyDependencies(arrayListOf("device-id"))
410 resourceDefinition(name = "device-id", description = "Device Id") {
411 property(type = "string", required = true) {
413 sourceInput(id = "input", description = "Dependency Source") {}
419 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
420 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
421 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
422 every { raRuntimeService.setBluePrintError(any()) } returns Unit
423 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
424 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
426 val applicationContext = mockk<ApplicationContext>()
427 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
428 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
429 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
431 val sources = arrayListOf<String>("sdno", "sdnc")
433 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
434 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
436 resourceDefinitions, "port-speed", sources
438 assertNotNull(resolvedResources, "failed to resolve the resources")