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.slf4j.LoggerFactory
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
42 import org.springframework.context.ApplicationContext
43 import org.springframework.context.annotation.ComponentScan
44 import org.springframework.test.context.ContextConfiguration
45 import org.springframework.test.context.TestPropertySource
46 import org.springframework.test.context.junit4.SpringRunner
47 import kotlin.test.assertNotNull
48 import kotlin.test.assertTrue
51 * ResourceResolutionServiceTest
53 * @author Brinda Santh DATE : 8/15/2018
55 @RunWith(SpringRunner::class)
56 @ContextConfiguration(
57 classes = [TestDatabaseConfiguration::class]
59 @TestPropertySource(locations = ["classpath:application-test.properties"])
60 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
61 @EnableAutoConfiguration
62 class ResourceResolutionServiceTest {
64 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
67 lateinit var resourceResolutionService: ResourceResolutionService
69 private val props = hashMapOf<String, Any>()
70 private val resolutionKey = "resolutionKey"
71 private val resourceId = "1"
72 private val resourceType = "ServiceInstance"
73 private val occurrence = 0
77 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
78 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
79 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
80 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
81 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
85 fun testRegisteredSource() {
86 val sources = resourceResolutionService.registeredResourceSources()
87 assertNotNull(sources, "failed to get registered sources")
91 "source-input", "source-default", "source-db",
92 "source-rest", "source-capability"
94 ), "failed to get registered sources : $sources"
99 @Throws(Exception::class)
100 fun testResolveResource() {
103 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
105 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
107 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
110 val executionServiceInput =
111 JacksonUtils.readValueFromClassPathFile(
112 "payload/requests/sample-resourceresolution-request.json",
113 ExecutionServiceInput::class.java
116 val resourceAssignmentRuntimeService =
117 ResourceAssignmentUtils.transformToRARuntimeService(
118 bluePrintRuntimeService,
119 "testResolveResource"
123 PayloadUtils.prepareInputsFromWorkflowPayload(
124 bluePrintRuntimeService,
125 executionServiceInput.payload,
126 "resource-assignment"
129 resourceResolutionService.resolveResources(
130 resourceAssignmentRuntimeService,
131 "resource-assignment",
139 @Throws(Exception::class)
140 fun testResolveResources() {
142 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
144 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
146 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
149 val executionServiceInput =
150 JacksonUtils.readValueFromClassPathFile(
151 "payload/requests/sample-resourceresolution-request.json",
152 ExecutionServiceInput::class.java
155 val artefactNames = listOf("baseconfig", "another")
158 PayloadUtils.prepareInputsFromWorkflowPayload(
159 bluePrintRuntimeService,
160 executionServiceInput.payload,
161 "resource-assignment"
164 resourceResolutionService.resolveResources(
165 bluePrintRuntimeService,
166 "resource-assignment",
174 @Throws(Exception::class)
175 fun testResolveResourcesWithMappingAndTemplate() {
177 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
179 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
181 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
184 val executionServiceInput =
185 JacksonUtils.readValueFromClassPathFile(
186 "payload/requests/sample-resourceresolution-request.json",
187 ExecutionServiceInput::class.java
190 val resourceAssignmentRuntimeService =
191 ResourceAssignmentUtils.transformToRARuntimeService(
192 bluePrintRuntimeService,
193 "testResolveResourcesWithMappingAndTemplate"
196 val artifactPrefix = "another"
199 PayloadUtils.prepareInputsFromWorkflowPayload(
200 bluePrintRuntimeService,
201 executionServiceInput.payload,
202 "resource-assignment"
205 resourceResolutionService.resolveResources(
206 resourceAssignmentRuntimeService,
207 "resource-assignment",
215 fun testResolveResourcesWithResourceIdAndResourceType() {
217 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
220 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
222 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
224 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
227 val executionServiceInput =
228 JacksonUtils.readValueFromClassPathFile(
229 "payload/requests/sample-resourceresolution-request.json",
230 ExecutionServiceInput::class.java
233 val resourceAssignmentRuntimeService =
234 ResourceAssignmentUtils.transformToRARuntimeService(
235 bluePrintRuntimeService,
236 "testResolveResourcesWithMappingAndTemplate"
239 val artifactPrefix = "another"
242 PayloadUtils.prepareInputsFromWorkflowPayload(
243 bluePrintRuntimeService,
244 executionServiceInput.payload,
245 "resource-assignment"
248 resourceResolutionService.resolveResources(
249 resourceAssignmentRuntimeService,
250 "resource-assignment",
258 fun testResourceResolutionForDefinition() {
259 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
260 resourceDefinition(name = "port-speed", description = "Port Speed") {
261 property(type = "string", required = true)
263 sourceCapability(id = "sdno", description = "SDNO Source") {
265 type(BluePrintConstants.SCRIPT_KOTLIN)
266 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
267 keyDependencies(arrayListOf("device-id"))
270 sourceDb(id = "sdnc", description = "SDNC Controller") {
272 endpointSelector("processor-db")
273 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
275 map("device_id", "\$device-id")
277 keyDependencies(arrayListOf("device-id"))
282 resourceDefinition(name = "device-id", description = "Device Id") {
283 property(type = "string", required = true) {
285 sourceInput(id = "input", description = "Dependency Source") {}
291 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
292 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
293 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
294 every { raRuntimeService.setBluePrintError(any()) } returns Unit
295 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
296 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
298 val applicationContext = mockk<ApplicationContext>()
299 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
300 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
301 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
303 val sources = arrayListOf<String>("sdno", "sdnc")
305 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
306 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
308 resourceDefinitions, "port-speed", sources
310 assertNotNull(resolvedResources, "failed to resolve the resources")