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.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"
97 ), "failed to get registered sources : $sources"
102 @Throws(Exception::class)
103 fun testResolveResource() {
106 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
108 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
110 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
113 val executionServiceInput =
114 JacksonUtils.readValueFromClassPathFile(
115 "payload/requests/sample-resourceresolution-request.json",
116 ExecutionServiceInput::class.java
119 val resourceAssignmentRuntimeService =
120 ResourceAssignmentUtils.transformToRARuntimeService(
121 bluePrintRuntimeService,
122 "testResolveResource"
126 PayloadUtils.prepareInputsFromWorkflowPayload(
127 bluePrintRuntimeService,
128 executionServiceInput.payload,
129 "resource-assignment"
132 resourceResolutionService.resolveResources(
133 resourceAssignmentRuntimeService,
134 "resource-assignment",
142 @Throws(Exception::class)
143 fun testResolveResources() {
145 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
147 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
149 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
152 val executionServiceInput =
153 JacksonUtils.readValueFromClassPathFile(
154 "payload/requests/sample-resourceresolution-request.json",
155 ExecutionServiceInput::class.java
158 val artefactNames = listOf("baseconfig", "another")
161 PayloadUtils.prepareInputsFromWorkflowPayload(
162 bluePrintRuntimeService,
163 executionServiceInput.payload,
164 "resource-assignment"
167 resourceResolutionService.resolveResources(
168 bluePrintRuntimeService,
169 "resource-assignment",
177 @Throws(Exception::class)
178 fun testResolveResourcesWithMappingAndTemplate() {
180 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
182 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
184 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
187 val executionServiceInput =
188 JacksonUtils.readValueFromClassPathFile(
189 "payload/requests/sample-resourceresolution-request.json",
190 ExecutionServiceInput::class.java
193 val resourceAssignmentRuntimeService =
194 ResourceAssignmentUtils.transformToRARuntimeService(
195 bluePrintRuntimeService,
196 "testResolveResourcesWithMappingAndTemplate"
199 val artifactPrefix = "another"
202 PayloadUtils.prepareInputsFromWorkflowPayload(
203 bluePrintRuntimeService,
204 executionServiceInput.payload,
205 "resource-assignment"
209 resourceResolutionService.resolveResources(
210 resourceAssignmentRuntimeService,
211 "resource-assignment",
214 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
220 @Throws(Exception::class)
221 fun testResolveResourcesResolutionSummary() {
223 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
224 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
226 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
228 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
231 val executionServiceInput =
232 JacksonUtils.readValueFromClassPathFile(
233 "payload/requests/sample-resourceresolution-request.json",
234 ExecutionServiceInput::class.java
237 val resourceAssignmentRuntimeService =
238 ResourceAssignmentUtils.transformToRARuntimeService(
239 bluePrintRuntimeService,
240 "testResolveResourcesWithMappingAndTemplate"
243 val artifactPrefix = "notemplate"
246 PayloadUtils.prepareInputsFromWorkflowPayload(
247 bluePrintRuntimeService,
248 executionServiceInput.payload,
249 "resource-assignment"
252 resourceResolutionService.resolveResources(
253 resourceAssignmentRuntimeService,
254 "resource-assignment",
259 val list = JacksonUtils.getListFromJson(it, ResolutionSummary::class.java)
260 assertEquals(list.size, 3)
265 @Throws(Exception::class)
266 fun testResolveResourcesWithoutTemplate() {
268 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
270 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
272 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
275 val executionServiceInput =
276 JacksonUtils.readValueFromClassPathFile(
277 "payload/requests/sample-resourceresolution-request.json",
278 ExecutionServiceInput::class.java
281 val resourceAssignmentRuntimeService =
282 ResourceAssignmentUtils.transformToRARuntimeService(
283 bluePrintRuntimeService,
284 "testResolveResourcesWithMappingAndTemplate"
287 val artifactPrefix = "notemplate"
290 PayloadUtils.prepareInputsFromWorkflowPayload(
291 bluePrintRuntimeService,
292 executionServiceInput.payload,
293 "resource-assignment"
296 resourceResolutionService.resolveResources(
297 resourceAssignmentRuntimeService,
298 "resource-assignment",
305 "service-instance-id" : "siid_1234",
306 "vnf-id" : "vnf_1234",
307 "vnf_name" : "temp_vnf"
309 """.trimIndent(), it)
314 fun testResolveResourcesWithResourceIdAndResourceType() {
315 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
317 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
319 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
321 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
324 val executionServiceInput =
325 JacksonUtils.readValueFromClassPathFile(
326 "payload/requests/sample-resourceresolution-request.json",
327 ExecutionServiceInput::class.java
330 val resourceAssignmentRuntimeService =
331 ResourceAssignmentUtils.transformToRARuntimeService(
332 bluePrintRuntimeService,
333 "testResolveResourcesWithMappingAndTemplate"
336 val artifactPrefix = "another"
339 PayloadUtils.prepareInputsFromWorkflowPayload(
340 bluePrintRuntimeService,
341 executionServiceInput.payload,
342 "resource-assignment"
346 resourceResolutionService.resolveResources(
347 resourceAssignmentRuntimeService,
348 "resource-assignment",
351 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
357 fun testResourceResolutionForDefinition() {
358 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
359 resourceDefinition(name = "port-speed", description = "Port Speed") {
360 property(type = "string", required = true)
362 sourceCapability(id = "sdno", description = "SDNO Source") {
364 type(BluePrintConstants.SCRIPT_KOTLIN)
365 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
366 keyDependencies(arrayListOf("device-id"))
369 sourceDb(id = "sdnc", description = "SDNC Controller") {
371 endpointSelector("processor-db")
372 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
374 map("device_id", "\$device-id")
376 keyDependencies(arrayListOf("device-id"))
381 resourceDefinition(name = "device-id", description = "Device Id") {
382 property(type = "string", required = true) {
384 sourceInput(id = "input", description = "Dependency Source") {}
390 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
391 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
392 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
393 every { raRuntimeService.setBluePrintError(any()) } returns Unit
394 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
395 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
397 val applicationContext = mockk<ApplicationContext>()
398 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
399 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
400 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
402 val sources = arrayListOf<String>("sdno", "sdnc")
404 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
405 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
407 resourceDefinitions, "port-speed", sources
409 assertNotNull(resolvedResources, "failed to resolve the resources")