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.BluePrintProperties
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.*
34 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
36 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
37 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
39 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
43 import org.slf4j.LoggerFactory
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
46 import org.springframework.context.ApplicationContext
47 import org.springframework.context.annotation.ComponentScan
48 import org.springframework.test.context.ContextConfiguration
49 import org.springframework.test.context.TestPropertySource
50 import org.springframework.test.context.junit4.SpringRunner
51 import kotlin.test.assertNotNull
52 import kotlin.test.assertTrue
55 * ResourceResolutionServiceTest
57 * @author Brinda Santh DATE : 8/15/2018
59 @RunWith(SpringRunner::class)
60 @ContextConfiguration(classes = [ResourceResolutionServiceImpl::class,
61 InputResourceResolutionProcessor::class, DefaultResourceResolutionProcessor::class,
62 DatabaseResourceAssignmentProcessor::class, RestResourceResolutionProcessor::class,
63 CapabilityResourceResolutionProcessor::class,
64 BlueprintPropertyConfiguration::class, BluePrintProperties::class,
65 BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class])
66 @TestPropertySource(locations = ["classpath:application-test.properties"])
67 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
68 @EnableAutoConfiguration
69 class ResourceResolutionServiceTest {
71 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
74 lateinit var resourceResolutionService: ResourceResolutionService
76 private val props = hashMapOf<String, Any>()
77 private val resolutionKey = "resolutionKey"
78 private val resourceId = "1"
79 private val resourceType = "ServiceInstance"
80 private val occurrence = 0
84 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
85 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
86 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
87 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
88 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
92 fun testRegisteredSource() {
93 val sources = resourceResolutionService.registeredResourceSources()
94 assertNotNull(sources, "failed to get registered sources")
95 assertTrue(sources.containsAll(arrayListOf("source-input", "source-default", "source-db",
96 "source-rest", "source-capability")), "failed to get registered sources : $sources")
100 @Throws(Exception::class)
101 fun testResolveResource() {
104 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
106 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
107 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
109 val executionServiceInput =
110 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
111 ExecutionServiceInput::class.java)!!
114 val resourceAssignmentRuntimeService =
115 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
116 "testResolveResource")
120 PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
121 executionServiceInput.payload,
122 "resource-assignment")
124 resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
125 "resource-assignment",
133 @Throws(Exception::class)
134 fun testResolveResources() {
136 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
138 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
139 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
141 val executionServiceInput =
142 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
143 ExecutionServiceInput::class.java)!!
145 val artefactNames = listOf("baseconfig", "another")
148 PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
149 executionServiceInput.payload,
150 "resource-assignment")
152 resourceResolutionService.resolveResources(bluePrintRuntimeService,
153 "resource-assignment",
161 @Throws(Exception::class)
162 fun testResolveResourcesWithMappingAndTemplate() {
164 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
166 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
167 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
169 val executionServiceInput =
170 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
171 ExecutionServiceInput::class.java)!!
173 val resourceAssignmentRuntimeService =
174 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
175 "testResolveResourcesWithMappingAndTemplate")
177 val artifactPrefix = "another"
180 PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
181 executionServiceInput.payload,
182 "resource-assignment")
184 resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
185 "resource-assignment",
193 fun testResolveResourcesWithResourceIdAndResourceType() {
195 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
198 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
200 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
201 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
203 val executionServiceInput =
204 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
205 ExecutionServiceInput::class.java)!!
207 val resourceAssignmentRuntimeService =
208 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
209 "testResolveResourcesWithMappingAndTemplate")
211 val artifactPrefix = "another"
214 PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
215 executionServiceInput.payload,
216 "resource-assignment")
218 resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
219 "resource-assignment",
226 fun testResourceResolutionForDefinition() {
227 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
228 resourceDefinition(name = "port-speed", description = "Port Speed") {
229 property(type = "string", required = true)
231 sourceCapability(id = "sdno", description = "SDNO Source") {
233 type(BluePrintConstants.SCRIPT_KOTLIN)
234 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
235 keyDependencies(arrayListOf("device-id"))
238 sourceDb(id = "sdnc", description = "SDNC Controller") {
240 endpointSelector("processor-db")
241 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
243 map("device_id", "\$device-id")
245 keyDependencies(arrayListOf("device-id"))
250 resourceDefinition(name = "device-id", description = "Device Id") {
251 property(type = "string", required = true) {
253 sourceInput(id = "input", description = "Dependency Source") {}
259 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
260 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
261 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
262 every { raRuntimeService.setBluePrintError(any())} returns Unit
263 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
264 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
266 val applicationContext = mockk<ApplicationContext>()
267 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
268 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
269 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
271 val sources = arrayListOf<String>("sdno", "sdnc")
273 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
274 val resolvedResources = resourceResolutionService.resolveResourceDefinition(raRuntimeService,
275 resourceDefinitions, "port-speed", sources)
276 assertNotNull(resolvedResources, "failed to resolve the resources")