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 summaries = JacksonUtils.jsonNode(it)["resolution-summary"]
260 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
261 assertEquals(list.size, 3)
266 @Throws(Exception::class)
267 fun testResolveResourcesWithoutTemplate() {
269 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
271 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
273 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
276 val executionServiceInput =
277 JacksonUtils.readValueFromClassPathFile(
278 "payload/requests/sample-resourceresolution-request.json",
279 ExecutionServiceInput::class.java
282 val resourceAssignmentRuntimeService =
283 ResourceAssignmentUtils.transformToRARuntimeService(
284 bluePrintRuntimeService,
285 "testResolveResourcesWithMappingAndTemplate"
288 val artifactPrefix = "notemplate"
291 PayloadUtils.prepareInputsFromWorkflowPayload(
292 bluePrintRuntimeService,
293 executionServiceInput.payload,
294 "resource-assignment"
297 resourceResolutionService.resolveResources(
298 resourceAssignmentRuntimeService,
299 "resource-assignment",
306 "service-instance-id" : "siid_1234",
307 "vnf-id" : "vnf_1234",
308 "vnf_name" : "temp_vnf"
310 """.trimIndent(), it)
315 fun testResolveResourcesWithResourceIdAndResourceType() {
316 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
318 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
320 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
322 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
325 val executionServiceInput =
326 JacksonUtils.readValueFromClassPathFile(
327 "payload/requests/sample-resourceresolution-request.json",
328 ExecutionServiceInput::class.java
331 val resourceAssignmentRuntimeService =
332 ResourceAssignmentUtils.transformToRARuntimeService(
333 bluePrintRuntimeService,
334 "testResolveResourcesWithMappingAndTemplate"
337 val artifactPrefix = "another"
340 PayloadUtils.prepareInputsFromWorkflowPayload(
341 bluePrintRuntimeService,
342 executionServiceInput.payload,
343 "resource-assignment"
347 resourceResolutionService.resolveResources(
348 resourceAssignmentRuntimeService,
349 "resource-assignment",
352 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
358 fun testResourceResolutionForDefinition() {
359 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
360 resourceDefinition(name = "port-speed", description = "Port Speed") {
361 property(type = "string", required = true)
363 sourceCapability(id = "sdno", description = "SDNO Source") {
365 type(BluePrintConstants.SCRIPT_KOTLIN)
366 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
367 keyDependencies(arrayListOf("device-id"))
370 sourceDb(id = "sdnc", description = "SDNC Controller") {
372 endpointSelector("processor-db")
373 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
375 map("device_id", "\$device-id")
377 keyDependencies(arrayListOf("device-id"))
382 resourceDefinition(name = "device-id", description = "Device Id") {
383 property(type = "string", required = true) {
385 sourceInput(id = "input", description = "Dependency Source") {}
391 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
392 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
393 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
394 every { raRuntimeService.setBluePrintError(any()) } returns Unit
395 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
396 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
398 val applicationContext = mockk<ApplicationContext>()
399 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
400 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
401 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
403 val sources = arrayListOf<String>("sdno", "sdnc")
405 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
406 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
408 resourceDefinitions, "port-speed", sources
410 assertNotNull(resolvedResources, "failed to resolve the resources")