Merge "Description: Create RESTCONF based vLB CBA Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
20
21 import io.mockk.every
22 import io.mockk.mockk
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Assert
25 import org.junit.Before
26 import org.junit.Test
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
53
54 /**
55  * ResourceResolutionServiceTest
56  *
57  * @author Brinda Santh DATE : 8/15/2018
58  */
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 {
70
71     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
72
73     @Autowired
74     lateinit var resourceResolutionService: ResourceResolutionService
75
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
81
82     @Before
83     fun setup() {
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
89     }
90
91     @Test
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")
97     }
98
99     @Test
100     @Throws(Exception::class)
101     fun testResolveResource() {
102         runBlocking {
103
104             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
105
106             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
107                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
108
109             val executionServiceInput =
110                     JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
111                             ExecutionServiceInput::class.java)!!
112
113
114             val resourceAssignmentRuntimeService =
115                     ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
116                             "testResolveResource")
117
118
119             // Prepare Inputs
120             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
121                     executionServiceInput.payload,
122                     "resource-assignment")
123
124             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
125                     "resource-assignment",
126                     "baseconfig",
127                     props)
128
129         }
130     }
131
132     @Test
133     @Throws(Exception::class)
134     fun testResolveResources() {
135         runBlocking {
136             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
137
138             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
139                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
140
141             val executionServiceInput =
142                     JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
143                             ExecutionServiceInput::class.java)!!
144
145             val artefactNames = listOf("baseconfig", "another")
146
147             // Prepare Inputs
148             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
149                     executionServiceInput.payload,
150                     "resource-assignment")
151
152             resourceResolutionService.resolveResources(bluePrintRuntimeService,
153                     "resource-assignment",
154                     artefactNames,
155                     props)
156         }
157
158     }
159
160     @Test
161     @Throws(Exception::class)
162     fun testResolveResourcesWithMappingAndTemplate() {
163         runBlocking {
164             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
165
166             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
167                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
168
169             val executionServiceInput =
170                     JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
171                             ExecutionServiceInput::class.java)!!
172
173             val resourceAssignmentRuntimeService =
174                     ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
175                             "testResolveResourcesWithMappingAndTemplate")
176
177             val artifactPrefix = "another"
178
179             // Prepare Inputs
180             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
181                     executionServiceInput.payload,
182                     "resource-assignment")
183
184             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
185                     "resource-assignment",
186                     artifactPrefix,
187                     props)
188         }
189     }
190
191
192     @Test
193     fun testResolveResourcesWithResourceIdAndResourceType() {
194
195         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
196
197         runBlocking {
198             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
199
200             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
201                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
202
203             val executionServiceInput =
204                     JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
205                             ExecutionServiceInput::class.java)!!
206
207             val resourceAssignmentRuntimeService =
208                     ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
209                             "testResolveResourcesWithMappingAndTemplate")
210
211             val artifactPrefix = "another"
212
213             // Prepare Inputs
214             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
215                     executionServiceInput.payload,
216                     "resource-assignment")
217
218             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
219                     "resource-assignment",
220                     artifactPrefix,
221                     props)
222         }
223     }
224
225     @Test
226     fun testResourceResolutionForDefinition() {
227         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
228             resourceDefinition(name = "port-speed", description = "Port Speed") {
229                 property(type = "string", required = true)
230                 sources {
231                     sourceCapability(id = "sdno", description = "SDNO Source") {
232                         definedProperties {
233                             type(BluePrintConstants.SCRIPT_KOTLIN)
234                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
235                             keyDependencies(arrayListOf("device-id"))
236                         }
237                     }
238                     sourceDb(id = "sdnc", description = "SDNC Controller") {
239                         definedProperties {
240                             endpointSelector("processor-db")
241                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
242                             inputKeyMapping {
243                                 map("device_id", "\$device-id")
244                             }
245                             keyDependencies(arrayListOf("device-id"))
246                         }
247                     }
248                 }
249             }
250             resourceDefinition(name = "device-id", description = "Device Id") {
251                 property(type = "string", required = true) {
252                     sources {
253                         sourceInput(id = "input", description = "Dependency Source") {}
254                     }
255                 }
256             }
257         }
258         runBlocking {
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
265
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()
270
271             val sources = arrayListOf<String>("sdno", "sdnc")
272
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")
277         }
278     }
279 }