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.BluePrintConstants
33 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
34 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
35 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
41 import org.slf4j.LoggerFactory
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
44 import org.springframework.context.ApplicationContext
45 import org.springframework.context.annotation.ComponentScan
46 import org.springframework.test.context.ContextConfiguration
47 import org.springframework.test.context.TestPropertySource
48 import org.springframework.test.context.junit4.SpringRunner
49 import kotlin.test.assertEquals
50 import kotlin.test.assertNotNull
51 import kotlin.test.assertTrue
54 * ResourceResolutionServiceTest
56 * @author Brinda Santh DATE : 8/15/2018
58 @RunWith(SpringRunner::class)
59 @ContextConfiguration(
60 classes = [TestDatabaseConfiguration::class]
62 @TestPropertySource(locations = ["classpath:application-test.properties"])
63 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
64 @EnableAutoConfiguration
65 class ResourceResolutionServiceTest {
67 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
70 lateinit var resourceResolutionService: ResourceResolutionService
72 private val props = hashMapOf<String, Any>()
73 private val resolutionKey = "resolutionKey"
74 private val resourceId = "1"
75 private val resourceType = "ServiceInstance"
76 private val occurrence = 0
80 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
81 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
82 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
83 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
84 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
85 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = false
89 fun testRegisteredSource() {
90 val sources = resourceResolutionService.registeredResourceSources()
91 assertNotNull(sources, "failed to get registered sources")
95 "source-input", "source-default", "source-db",
96 "source-rest", "source-capability"
99 "failed to get registered sources : $sources"
104 @Throws(Exception::class)
105 fun testResolveResource() {
108 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
110 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
112 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
115 val executionServiceInput =
116 JacksonUtils.readValueFromClassPathFile(
117 "payload/requests/sample-resourceresolution-request.json",
118 ExecutionServiceInput::class.java
121 val resourceAssignmentRuntimeService =
122 ResourceAssignmentUtils.transformToRARuntimeService(
123 bluePrintRuntimeService,
124 "testResolveResource"
128 PayloadUtils.prepareInputsFromWorkflowPayload(
129 bluePrintRuntimeService,
130 executionServiceInput.payload,
131 "resource-assignment"
134 resourceResolutionService.resolveResources(
135 resourceAssignmentRuntimeService,
136 "resource-assignment",
140 }.let { (templateMap, assignmentMap) ->
141 assertEquals("This is Sample Velocity Template", templateMap)
143 val expectedAssignmentMap = hashMapOf(
144 "service-instance-id" to "siid_1234",
145 "vnf-id" to "vnf_1234",
146 "vnf_name" to "temp_vnf"
148 assertEquals(expectedAssignmentMap, assignmentMap)
153 @Throws(Exception::class)
154 fun testResolveResources() {
155 val artifactNames = listOf("baseconfig", "another")
157 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
159 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
161 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
164 val executionServiceInput =
165 JacksonUtils.readValueFromClassPathFile(
166 "payload/requests/sample-resourceresolution-request.json",
167 ExecutionServiceInput::class.java
171 PayloadUtils.prepareInputsFromWorkflowPayload(
172 bluePrintRuntimeService,
173 executionServiceInput.payload,
174 "resource-assignment"
177 resourceResolutionService.resolveResources(
178 bluePrintRuntimeService,
179 "resource-assignment",
184 assertEquals(artifactNames.toSet(), it.templateMap.keys)
185 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
187 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
188 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
193 @Throws(Exception::class)
194 fun testResolveResourcesWithMappingAndTemplate() {
196 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
198 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
200 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
203 val executionServiceInput =
204 JacksonUtils.readValueFromClassPathFile(
205 "payload/requests/sample-resourceresolution-request.json",
206 ExecutionServiceInput::class.java
209 val resourceAssignmentRuntimeService =
210 ResourceAssignmentUtils.transformToRARuntimeService(
211 bluePrintRuntimeService,
212 "testResolveResourcesWithMappingAndTemplate"
215 val artifactPrefix = "another"
218 PayloadUtils.prepareInputsFromWorkflowPayload(
219 bluePrintRuntimeService,
220 executionServiceInput.payload,
221 "resource-assignment"
225 resourceResolutionService.resolveResources(
226 resourceAssignmentRuntimeService,
227 "resource-assignment",
231 "Couldn't Resolve Resources for artifact $artifactPrefix"
237 @Throws(Exception::class)
238 fun testResolveResourcesResolutionSummary() {
240 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
241 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
243 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
245 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
248 val executionServiceInput =
249 JacksonUtils.readValueFromClassPathFile(
250 "payload/requests/sample-resourceresolution-request.json",
251 ExecutionServiceInput::class.java
254 val resourceAssignmentRuntimeService =
255 ResourceAssignmentUtils.transformToRARuntimeService(
256 bluePrintRuntimeService,
257 "testResolveResourcesWithMappingAndTemplate"
260 val artifactPrefix = "notemplate"
263 PayloadUtils.prepareInputsFromWorkflowPayload(
264 bluePrintRuntimeService,
265 executionServiceInput.payload,
266 "resource-assignment"
269 resourceResolutionService.resolveResources(
270 resourceAssignmentRuntimeService,
271 "resource-assignment",
276 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
277 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
278 assertEquals(list.size, 3)
283 @Throws(Exception::class)
284 fun testResolveResourcesWithoutTemplate() {
285 val artifactPrefix = "notemplate"
287 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
289 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
291 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
294 val executionServiceInput =
295 JacksonUtils.readValueFromClassPathFile(
296 "payload/requests/sample-resourceresolution-request.json",
297 ExecutionServiceInput::class.java
300 val resourceAssignmentRuntimeService =
301 ResourceAssignmentUtils.transformToRARuntimeService(
302 bluePrintRuntimeService,
303 "testResolveResourcesWithMappingAndTemplate"
307 PayloadUtils.prepareInputsFromWorkflowPayload(
308 bluePrintRuntimeService,
309 executionServiceInput.payload,
310 "resource-assignment"
313 resourceResolutionService.resolveResources(
314 resourceAssignmentRuntimeService,
315 "resource-assignment",
323 "service-instance-id" : "siid_1234",
324 "vnf-id" : "vnf_1234",
325 "vnf_name" : "temp_vnf"
330 assertEquals("siid_1234", it.second["service-instance-id"].asText())
335 fun testResolveResourcesWithResourceIdAndResourceType() {
336 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
338 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
340 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
342 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
345 val executionServiceInput =
346 JacksonUtils.readValueFromClassPathFile(
347 "payload/requests/sample-resourceresolution-request.json",
348 ExecutionServiceInput::class.java
351 val resourceAssignmentRuntimeService =
352 ResourceAssignmentUtils.transformToRARuntimeService(
353 bluePrintRuntimeService,
354 "testResolveResourcesWithMappingAndTemplate"
357 val artifactPrefix = "another"
360 PayloadUtils.prepareInputsFromWorkflowPayload(
361 bluePrintRuntimeService,
362 executionServiceInput.payload,
363 "resource-assignment"
367 resourceResolutionService.resolveResources(
368 resourceAssignmentRuntimeService,
369 "resource-assignment",
373 "Couldn't Resolve Resources for artifact $artifactPrefix"
379 fun testResourceResolutionForDefinition() {
380 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
381 resourceDefinition(name = "port-speed", description = "Port Speed") {
382 property(type = "string", required = true)
384 sourceCapability(id = "sdno", description = "SDNO Source") {
386 type(BluePrintConstants.SCRIPT_KOTLIN)
387 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
388 keyDependencies(arrayListOf("device-id"))
391 sourceDb(id = "sdnc", description = "SDNC Controller") {
393 endpointSelector("processor-db")
394 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
396 map("device_id", "\$device-id")
398 keyDependencies(arrayListOf("device-id"))
403 resourceDefinition(name = "device-id", description = "Device Id") {
404 property(type = "string", required = true) {
406 sourceInput(id = "input", description = "Dependency Source") {}
412 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
413 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
414 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
415 every { raRuntimeService.setBluePrintError(any()) } returns Unit
416 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
417 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
419 val applicationContext = mockk<ApplicationContext>()
420 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
421 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
422 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
424 val sources = arrayListOf<String>("sdno", "sdnc")
426 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
427 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
429 resourceDefinitions, "port-speed", sources
431 assertNotNull(resolvedResources, "failed to resolve the resources")