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.BluePrintPropertiesService
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
32 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.CapabilityResourceResolutionProcessor
34 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.DatabaseResourceAssignmentProcessor
35 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.DefaultResourceResolutionProcessor
36 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.InputResourceResolutionProcessor
37 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.MockCapabilityScriptRA
38 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.RestResourceResolutionProcessor
39 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
40 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
41 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
42 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
43 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
44 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
45 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
46 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
47 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
48 import org.slf4j.LoggerFactory
49 import org.springframework.beans.factory.annotation.Autowired
50 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
51 import org.springframework.context.ApplicationContext
52 import org.springframework.context.annotation.ComponentScan
53 import org.springframework.test.context.ContextConfiguration
54 import org.springframework.test.context.TestPropertySource
55 import org.springframework.test.context.junit4.SpringRunner
56 import kotlin.test.assertNotNull
57 import kotlin.test.assertTrue
60 * ResourceResolutionServiceTest
62 * @author Brinda Santh DATE : 8/15/2018
64 @RunWith(SpringRunner::class)
65 @ContextConfiguration(
66 classes = [ResourceResolutionServiceImpl::class,
67 InputResourceResolutionProcessor::class, DefaultResourceResolutionProcessor::class,
68 DatabaseResourceAssignmentProcessor::class, RestResourceResolutionProcessor::class,
69 CapabilityResourceResolutionProcessor::class,
70 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class,
71 BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class]
73 @TestPropertySource(locations = ["classpath:application-test.properties"])
74 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
75 @EnableAutoConfiguration
76 class ResourceResolutionServiceTest {
78 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
81 lateinit var resourceResolutionService: ResourceResolutionService
83 private val props = hashMapOf<String, Any>()
84 private val resolutionKey = "resolutionKey"
85 private val resourceId = "1"
86 private val resourceType = "ServiceInstance"
87 private val occurrence = 0
91 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
92 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
93 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
94 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
95 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
99 fun testRegisteredSource() {
100 val sources = resourceResolutionService.registeredResourceSources()
101 assertNotNull(sources, "failed to get registered sources")
105 "source-input", "source-default", "source-db",
106 "source-rest", "source-capability"
108 ), "failed to get registered sources : $sources"
113 @Throws(Exception::class)
114 fun testResolveResource() {
117 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
119 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
121 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
124 val executionServiceInput =
125 JacksonUtils.readValueFromClassPathFile(
126 "payload/requests/sample-resourceresolution-request.json",
127 ExecutionServiceInput::class.java
130 val resourceAssignmentRuntimeService =
131 ResourceAssignmentUtils.transformToRARuntimeService(
132 bluePrintRuntimeService,
133 "testResolveResource"
137 PayloadUtils.prepareInputsFromWorkflowPayload(
138 bluePrintRuntimeService,
139 executionServiceInput.payload,
140 "resource-assignment"
143 resourceResolutionService.resolveResources(
144 resourceAssignmentRuntimeService,
145 "resource-assignment",
153 @Throws(Exception::class)
154 fun testResolveResources() {
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
169 val artefactNames = listOf("baseconfig", "another")
172 PayloadUtils.prepareInputsFromWorkflowPayload(
173 bluePrintRuntimeService,
174 executionServiceInput.payload,
175 "resource-assignment"
178 resourceResolutionService.resolveResources(
179 bluePrintRuntimeService,
180 "resource-assignment",
188 @Throws(Exception::class)
189 fun testResolveResourcesWithMappingAndTemplate() {
191 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
193 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
195 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
198 val executionServiceInput =
199 JacksonUtils.readValueFromClassPathFile(
200 "payload/requests/sample-resourceresolution-request.json",
201 ExecutionServiceInput::class.java
204 val resourceAssignmentRuntimeService =
205 ResourceAssignmentUtils.transformToRARuntimeService(
206 bluePrintRuntimeService,
207 "testResolveResourcesWithMappingAndTemplate"
210 val artifactPrefix = "another"
213 PayloadUtils.prepareInputsFromWorkflowPayload(
214 bluePrintRuntimeService,
215 executionServiceInput.payload,
216 "resource-assignment"
219 resourceResolutionService.resolveResources(
220 resourceAssignmentRuntimeService,
221 "resource-assignment",
229 fun testResolveResourcesWithResourceIdAndResourceType() {
231 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
234 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
236 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
238 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
241 val executionServiceInput =
242 JacksonUtils.readValueFromClassPathFile(
243 "payload/requests/sample-resourceresolution-request.json",
244 ExecutionServiceInput::class.java
247 val resourceAssignmentRuntimeService =
248 ResourceAssignmentUtils.transformToRARuntimeService(
249 bluePrintRuntimeService,
250 "testResolveResourcesWithMappingAndTemplate"
253 val artifactPrefix = "another"
256 PayloadUtils.prepareInputsFromWorkflowPayload(
257 bluePrintRuntimeService,
258 executionServiceInput.payload,
259 "resource-assignment"
262 resourceResolutionService.resolveResources(
263 resourceAssignmentRuntimeService,
264 "resource-assignment",
272 fun testResourceResolutionForDefinition() {
273 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
274 resourceDefinition(name = "port-speed", description = "Port Speed") {
275 property(type = "string", required = true)
277 sourceCapability(id = "sdno", description = "SDNO Source") {
279 type(BluePrintConstants.SCRIPT_KOTLIN)
280 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
281 keyDependencies(arrayListOf("device-id"))
284 sourceDb(id = "sdnc", description = "SDNC Controller") {
286 endpointSelector("processor-db")
287 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
289 map("device_id", "\$device-id")
291 keyDependencies(arrayListOf("device-id"))
296 resourceDefinition(name = "device-id", description = "Device Id") {
297 property(type = "string", required = true) {
299 sourceInput(id = "input", description = "Dependency Source") {}
305 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
306 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
307 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
308 every { raRuntimeService.setBluePrintError(any()) } returns Unit
309 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
310 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
312 val applicationContext = mockk<ApplicationContext>()
313 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
314 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
315 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
317 val sources = arrayListOf<String>("sdno", "sdnc")
319 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
320 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
322 resourceDefinitions, "port-speed", sources
324 assertNotNull(resolvedResources, "failed to resolve the resources")