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"
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, assignmentMap) ->
140 assertEquals("This is Sample Velocity Template", templateMap)
142 val expectedAssignmentMap = hashMapOf(
143 "service-instance-id" to "siid_1234",
144 "vnf-id" to "vnf_1234",
145 "vnf_name" to "temp_vnf"
147 assertEquals(expectedAssignmentMap, assignmentMap)
152 @Throws(Exception::class)
153 fun testResolveResources() {
154 val artifactNames = listOf("baseconfig", "another")
156 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
158 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
160 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
163 val executionServiceInput =
164 JacksonUtils.readValueFromClassPathFile(
165 "payload/requests/sample-resourceresolution-request.json",
166 ExecutionServiceInput::class.java
170 PayloadUtils.prepareInputsFromWorkflowPayload(
171 bluePrintRuntimeService,
172 executionServiceInput.payload,
173 "resource-assignment"
176 resourceResolutionService.resolveResources(
177 bluePrintRuntimeService,
178 "resource-assignment",
183 assertEquals(artifactNames.toSet(), it.templateMap.keys)
184 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
186 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
187 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
192 @Throws(Exception::class)
193 fun testResolveResourcesWithMappingAndTemplate() {
195 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
197 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
199 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
202 val executionServiceInput =
203 JacksonUtils.readValueFromClassPathFile(
204 "payload/requests/sample-resourceresolution-request.json",
205 ExecutionServiceInput::class.java
208 val resourceAssignmentRuntimeService =
209 ResourceAssignmentUtils.transformToRARuntimeService(
210 bluePrintRuntimeService,
211 "testResolveResourcesWithMappingAndTemplate"
214 val artifactPrefix = "another"
217 PayloadUtils.prepareInputsFromWorkflowPayload(
218 bluePrintRuntimeService,
219 executionServiceInput.payload,
220 "resource-assignment"
224 resourceResolutionService.resolveResources(
225 resourceAssignmentRuntimeService,
226 "resource-assignment",
229 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
235 @Throws(Exception::class)
236 fun testResolveResourcesResolutionSummary() {
238 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
239 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
241 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
243 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
246 val executionServiceInput =
247 JacksonUtils.readValueFromClassPathFile(
248 "payload/requests/sample-resourceresolution-request.json",
249 ExecutionServiceInput::class.java
252 val resourceAssignmentRuntimeService =
253 ResourceAssignmentUtils.transformToRARuntimeService(
254 bluePrintRuntimeService,
255 "testResolveResourcesWithMappingAndTemplate"
258 val artifactPrefix = "notemplate"
261 PayloadUtils.prepareInputsFromWorkflowPayload(
262 bluePrintRuntimeService,
263 executionServiceInput.payload,
264 "resource-assignment"
267 resourceResolutionService.resolveResources(
268 resourceAssignmentRuntimeService,
269 "resource-assignment",
274 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
275 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
276 assertEquals(list.size, 3)
281 @Throws(Exception::class)
282 fun testResolveResourcesWithoutTemplate() {
283 val artifactPrefix = "notemplate"
285 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
287 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
289 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
292 val executionServiceInput =
293 JacksonUtils.readValueFromClassPathFile(
294 "payload/requests/sample-resourceresolution-request.json",
295 ExecutionServiceInput::class.java
298 val resourceAssignmentRuntimeService =
299 ResourceAssignmentUtils.transformToRARuntimeService(
300 bluePrintRuntimeService,
301 "testResolveResourcesWithMappingAndTemplate"
305 PayloadUtils.prepareInputsFromWorkflowPayload(
306 bluePrintRuntimeService,
307 executionServiceInput.payload,
308 "resource-assignment"
311 resourceResolutionService.resolveResources(
312 resourceAssignmentRuntimeService,
313 "resource-assignment",
320 "service-instance-id" : "siid_1234",
321 "vnf-id" : "vnf_1234",
322 "vnf_name" : "temp_vnf"
324 """.trimIndent(), it.first)
325 assertEquals("siid_1234", it.second["service-instance-id"].asText())
330 fun testResolveResourcesWithResourceIdAndResourceType() {
331 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
333 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
335 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
337 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
340 val executionServiceInput =
341 JacksonUtils.readValueFromClassPathFile(
342 "payload/requests/sample-resourceresolution-request.json",
343 ExecutionServiceInput::class.java
346 val resourceAssignmentRuntimeService =
347 ResourceAssignmentUtils.transformToRARuntimeService(
348 bluePrintRuntimeService,
349 "testResolveResourcesWithMappingAndTemplate"
352 val artifactPrefix = "another"
355 PayloadUtils.prepareInputsFromWorkflowPayload(
356 bluePrintRuntimeService,
357 executionServiceInput.payload,
358 "resource-assignment"
362 resourceResolutionService.resolveResources(
363 resourceAssignmentRuntimeService,
364 "resource-assignment",
367 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
373 fun testResourceResolutionForDefinition() {
374 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
375 resourceDefinition(name = "port-speed", description = "Port Speed") {
376 property(type = "string", required = true)
378 sourceCapability(id = "sdno", description = "SDNO Source") {
380 type(BluePrintConstants.SCRIPT_KOTLIN)
381 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
382 keyDependencies(arrayListOf("device-id"))
385 sourceDb(id = "sdnc", description = "SDNC Controller") {
387 endpointSelector("processor-db")
388 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
390 map("device_id", "\$device-id")
392 keyDependencies(arrayListOf("device-id"))
397 resourceDefinition(name = "device-id", description = "Device Id") {
398 property(type = "string", required = true) {
400 sourceInput(id = "input", description = "Dependency Source") {}
406 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
407 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
408 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
409 every { raRuntimeService.setBluePrintError(any()) } returns Unit
410 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
411 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
413 val applicationContext = mockk<ApplicationContext>()
414 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
415 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
416 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
418 val sources = arrayListOf<String>("sdno", "sdnc")
420 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
421 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
423 resourceDefinitions, "port-speed", sources
425 assertNotNull(resolvedResources, "failed to resolve the resources")