e46d457cd8893ae31d13e00e2cb776ebe34d7d43
[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.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
58
59 /**
60  * ResourceResolutionServiceTest
61  *
62  * @author Brinda Santh DATE : 8/15/2018
63  */
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]
72 )
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 {
77
78     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
79
80     @Autowired
81     lateinit var resourceResolutionService: ResourceResolutionService
82
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
88
89     @Before
90     fun setup() {
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
96     }
97
98     @Test
99     fun testRegisteredSource() {
100         val sources = resourceResolutionService.registeredResourceSources()
101         assertNotNull(sources, "failed to get registered sources")
102         assertTrue(
103             sources.containsAll(
104                 arrayListOf(
105                     "source-input", "source-default", "source-db",
106                     "source-rest", "source-capability"
107                 )
108             ), "failed to get registered sources : $sources"
109         )
110     }
111
112     @Test
113     @Throws(Exception::class)
114     fun testResolveResource() {
115         runBlocking {
116
117             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
118
119             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
120                 "1234",
121                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
122             )
123
124             val executionServiceInput =
125                 JacksonUtils.readValueFromClassPathFile(
126                     "payload/requests/sample-resourceresolution-request.json",
127                     ExecutionServiceInput::class.java
128                 )!!
129
130             val resourceAssignmentRuntimeService =
131                 ResourceAssignmentUtils.transformToRARuntimeService(
132                     bluePrintRuntimeService,
133                     "testResolveResource"
134                 )
135
136             // Prepare Inputs
137             PayloadUtils.prepareInputsFromWorkflowPayload(
138                 bluePrintRuntimeService,
139                 executionServiceInput.payload,
140                 "resource-assignment"
141             )
142
143             resourceResolutionService.resolveResources(
144                 resourceAssignmentRuntimeService,
145                 "resource-assignment",
146                 "baseconfig",
147                 props
148             )
149         }
150     }
151
152     @Test
153     @Throws(Exception::class)
154     fun testResolveResources() {
155         runBlocking {
156             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
157
158             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
159                 "1234",
160                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
161             )
162
163             val executionServiceInput =
164                 JacksonUtils.readValueFromClassPathFile(
165                     "payload/requests/sample-resourceresolution-request.json",
166                     ExecutionServiceInput::class.java
167                 )!!
168
169             val artefactNames = listOf("baseconfig", "another")
170
171             // Prepare Inputs
172             PayloadUtils.prepareInputsFromWorkflowPayload(
173                 bluePrintRuntimeService,
174                 executionServiceInput.payload,
175                 "resource-assignment"
176             )
177
178             resourceResolutionService.resolveResources(
179                 bluePrintRuntimeService,
180                 "resource-assignment",
181                 artefactNames,
182                 props
183             )
184         }
185     }
186
187     @Test
188     @Throws(Exception::class)
189     fun testResolveResourcesWithMappingAndTemplate() {
190         runBlocking {
191             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
192
193             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
194                 "1234",
195                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
196             )
197
198             val executionServiceInput =
199                 JacksonUtils.readValueFromClassPathFile(
200                     "payload/requests/sample-resourceresolution-request.json",
201                     ExecutionServiceInput::class.java
202                 )!!
203
204             val resourceAssignmentRuntimeService =
205                 ResourceAssignmentUtils.transformToRARuntimeService(
206                     bluePrintRuntimeService,
207                     "testResolveResourcesWithMappingAndTemplate"
208                 )
209
210             val artifactPrefix = "another"
211
212             // Prepare Inputs
213             PayloadUtils.prepareInputsFromWorkflowPayload(
214                 bluePrintRuntimeService,
215                 executionServiceInput.payload,
216                 "resource-assignment"
217             )
218
219             resourceResolutionService.resolveResources(
220                 resourceAssignmentRuntimeService,
221                 "resource-assignment",
222                 artifactPrefix,
223                 props
224             )
225         }
226     }
227
228     @Test
229     fun testResolveResourcesWithResourceIdAndResourceType() {
230
231         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
232
233         runBlocking {
234             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
235
236             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
237                 "1234",
238                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
239             )
240
241             val executionServiceInput =
242                 JacksonUtils.readValueFromClassPathFile(
243                     "payload/requests/sample-resourceresolution-request.json",
244                     ExecutionServiceInput::class.java
245                 )!!
246
247             val resourceAssignmentRuntimeService =
248                 ResourceAssignmentUtils.transformToRARuntimeService(
249                     bluePrintRuntimeService,
250                     "testResolveResourcesWithMappingAndTemplate"
251                 )
252
253             val artifactPrefix = "another"
254
255             // Prepare Inputs
256             PayloadUtils.prepareInputsFromWorkflowPayload(
257                 bluePrintRuntimeService,
258                 executionServiceInput.payload,
259                 "resource-assignment"
260             )
261
262             resourceResolutionService.resolveResources(
263                 resourceAssignmentRuntimeService,
264                 "resource-assignment",
265                 artifactPrefix,
266                 props
267             )
268         }
269     }
270
271     @Test
272     fun testResourceResolutionForDefinition() {
273         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
274             resourceDefinition(name = "port-speed", description = "Port Speed") {
275                 property(type = "string", required = true)
276                 sources {
277                     sourceCapability(id = "sdno", description = "SDNO Source") {
278                         definedProperties {
279                             type(BluePrintConstants.SCRIPT_KOTLIN)
280                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
281                             keyDependencies(arrayListOf("device-id"))
282                         }
283                     }
284                     sourceDb(id = "sdnc", description = "SDNC Controller") {
285                         definedProperties {
286                             endpointSelector("processor-db")
287                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
288                             inputKeyMapping {
289                                 map("device_id", "\$device-id")
290                             }
291                             keyDependencies(arrayListOf("device-id"))
292                         }
293                     }
294                 }
295             }
296             resourceDefinition(name = "device-id", description = "Device Id") {
297                 property(type = "string", required = true) {
298                     sources {
299                         sourceInput(id = "input", description = "Dependency Source") {}
300                     }
301                 }
302             }
303         }
304         runBlocking {
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
311
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()
316
317             val sources = arrayListOf<String>("sdno", "sdnc")
318
319             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
320             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
321                 raRuntimeService,
322                 resourceDefinitions, "port-speed", sources
323             )
324             assertNotNull(resolvedResources, "failed to resolve the resources")
325         }
326     }
327 }