Enabling Code Formatter
[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.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.asJsonType
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
41 import org.slf4j.LoggerFactory
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
44 import org.springframework.context.ApplicationContext
45 import org.springframework.context.annotation.ComponentScan
46 import org.springframework.test.context.ContextConfiguration
47 import org.springframework.test.context.TestPropertySource
48 import org.springframework.test.context.junit4.SpringRunner
49 import kotlin.test.assertEquals
50 import kotlin.test.assertNotNull
51 import kotlin.test.assertTrue
52
53 /**
54  * ResourceResolutionServiceTest
55  *
56  * @author Brinda Santh DATE : 8/15/2018
57  */
58 @RunWith(SpringRunner::class)
59 @ContextConfiguration(
60     classes = [TestDatabaseConfiguration::class]
61 )
62 @TestPropertySource(locations = ["classpath:application-test.properties"])
63 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
64 @EnableAutoConfiguration
65 class ResourceResolutionServiceTest {
66
67     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
68
69     @Autowired
70     lateinit var resourceResolutionService: ResourceResolutionService
71
72     private val props = hashMapOf<String, Any>()
73     private val resolutionKey = "resolutionKey"
74     private val resourceId = "1"
75     private val resourceType = "ServiceInstance"
76     private val occurrence = 0
77
78     @Before
79     fun setup() {
80         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
81         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
82         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
83         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
84         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
85         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = false
86     }
87
88     @Test
89     fun testRegisteredSource() {
90         val sources = resourceResolutionService.registeredResourceSources()
91         assertNotNull(sources, "failed to get registered sources")
92         assertTrue(
93             sources.containsAll(
94                 arrayListOf(
95                     "source-input", "source-default", "source-db",
96                     "source-rest", "source-capability"
97                 )
98             ),
99             "failed to get registered sources : $sources"
100         )
101     }
102
103     @Test
104     @Throws(Exception::class)
105     fun testResolveResource() {
106         runBlocking {
107
108             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
109
110             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
111                 "1234",
112                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
113             )
114
115             val executionServiceInput =
116                 JacksonUtils.readValueFromClassPathFile(
117                     "payload/requests/sample-resourceresolution-request.json",
118                     ExecutionServiceInput::class.java
119                 )!!
120
121             val resourceAssignmentRuntimeService =
122                 ResourceAssignmentUtils.transformToRARuntimeService(
123                     bluePrintRuntimeService,
124                     "testResolveResource"
125                 )
126
127             // Prepare Inputs
128             PayloadUtils.prepareInputsFromWorkflowPayload(
129                 bluePrintRuntimeService,
130                 executionServiceInput.payload,
131                 "resource-assignment"
132             )
133
134             resourceResolutionService.resolveResources(
135                 resourceAssignmentRuntimeService,
136                 "resource-assignment",
137                 "baseconfig",
138                 props
139             )
140         }.let { (templateMap, assignmentMap) ->
141             assertEquals("This is Sample Velocity Template", templateMap)
142
143             val expectedAssignmentMap = hashMapOf(
144                 "service-instance-id" to "siid_1234",
145                 "vnf-id" to "vnf_1234",
146                 "vnf_name" to "temp_vnf"
147             ).asJsonType()
148             assertEquals(expectedAssignmentMap, assignmentMap)
149         }
150     }
151
152     @Test
153     @Throws(Exception::class)
154     fun testResolveResources() {
155         val artifactNames = listOf("baseconfig", "another")
156         runBlocking {
157             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
158
159             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
160                 "1234",
161                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
162             )
163
164             val executionServiceInput =
165                 JacksonUtils.readValueFromClassPathFile(
166                     "payload/requests/sample-resourceresolution-request.json",
167                     ExecutionServiceInput::class.java
168                 )!!
169
170             // Prepare Inputs
171             PayloadUtils.prepareInputsFromWorkflowPayload(
172                 bluePrintRuntimeService,
173                 executionServiceInput.payload,
174                 "resource-assignment"
175             )
176
177             resourceResolutionService.resolveResources(
178                 bluePrintRuntimeService,
179                 "resource-assignment",
180                 artifactNames,
181                 props
182             )
183         }.let {
184             assertEquals(artifactNames.toSet(), it.templateMap.keys)
185             assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
186
187             assertEquals("This is Sample Velocity Template", it.templateMap["another"])
188             assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
189         }
190     }
191
192     @Test
193     @Throws(Exception::class)
194     fun testResolveResourcesWithMappingAndTemplate() {
195         runBlocking {
196             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
197
198             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
199                 "1234",
200                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
201             )
202
203             val executionServiceInput =
204                 JacksonUtils.readValueFromClassPathFile(
205                     "payload/requests/sample-resourceresolution-request.json",
206                     ExecutionServiceInput::class.java
207                 )!!
208
209             val resourceAssignmentRuntimeService =
210                 ResourceAssignmentUtils.transformToRARuntimeService(
211                     bluePrintRuntimeService,
212                     "testResolveResourcesWithMappingAndTemplate"
213                 )
214
215             val artifactPrefix = "another"
216
217             // Prepare Inputs
218             PayloadUtils.prepareInputsFromWorkflowPayload(
219                 bluePrintRuntimeService,
220                 executionServiceInput.payload,
221                 "resource-assignment"
222             )
223
224             assertNotNull(
225                 resourceResolutionService.resolveResources(
226                     resourceAssignmentRuntimeService,
227                     "resource-assignment",
228                     artifactPrefix,
229                     props
230                 ),
231                 "Couldn't Resolve Resources for artifact $artifactPrefix"
232             )
233         }
234     }
235
236     @Test
237     @Throws(Exception::class)
238     fun testResolveResourcesResolutionSummary() {
239         runBlocking {
240             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
241             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
242
243             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
244                 "1234",
245                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
246             )
247
248             val executionServiceInput =
249                 JacksonUtils.readValueFromClassPathFile(
250                     "payload/requests/sample-resourceresolution-request.json",
251                     ExecutionServiceInput::class.java
252                 )!!
253
254             val resourceAssignmentRuntimeService =
255                 ResourceAssignmentUtils.transformToRARuntimeService(
256                     bluePrintRuntimeService,
257                     "testResolveResourcesWithMappingAndTemplate"
258                 )
259
260             val artifactPrefix = "notemplate"
261
262             // Prepare Inputs
263             PayloadUtils.prepareInputsFromWorkflowPayload(
264                 bluePrintRuntimeService,
265                 executionServiceInput.payload,
266                 "resource-assignment"
267             )
268
269             resourceResolutionService.resolveResources(
270                 resourceAssignmentRuntimeService,
271                 "resource-assignment",
272                 artifactPrefix,
273                 props
274             )
275         }.let {
276             val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
277             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
278             assertEquals(list.size, 3)
279         }
280     }
281
282     @Test
283     @Throws(Exception::class)
284     fun testResolveResourcesWithoutTemplate() {
285         val artifactPrefix = "notemplate"
286         runBlocking {
287             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
288
289             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
290                 "1234",
291                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
292             )
293
294             val executionServiceInput =
295                 JacksonUtils.readValueFromClassPathFile(
296                     "payload/requests/sample-resourceresolution-request.json",
297                     ExecutionServiceInput::class.java
298                 )!!
299
300             val resourceAssignmentRuntimeService =
301                 ResourceAssignmentUtils.transformToRARuntimeService(
302                     bluePrintRuntimeService,
303                     "testResolveResourcesWithMappingAndTemplate"
304                 )
305
306             // Prepare Inputs
307             PayloadUtils.prepareInputsFromWorkflowPayload(
308                 bluePrintRuntimeService,
309                 executionServiceInput.payload,
310                 "resource-assignment"
311             )
312
313             resourceResolutionService.resolveResources(
314                 resourceAssignmentRuntimeService,
315                 "resource-assignment",
316                 artifactPrefix,
317                 props
318             )
319         }.let {
320             assertEquals(
321                 """
322                 {
323                   "service-instance-id" : "siid_1234",
324                   "vnf-id" : "vnf_1234",
325                   "vnf_name" : "temp_vnf"
326                 }
327                 """.trimIndent(),
328                 it.first
329             )
330             assertEquals("siid_1234", it.second["service-instance-id"].asText())
331         }
332     }
333
334     @Test
335     fun testResolveResourcesWithResourceIdAndResourceType() {
336         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
337         runBlocking {
338             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
339
340             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
341                 "1234",
342                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
343             )
344
345             val executionServiceInput =
346                 JacksonUtils.readValueFromClassPathFile(
347                     "payload/requests/sample-resourceresolution-request.json",
348                     ExecutionServiceInput::class.java
349                 )!!
350
351             val resourceAssignmentRuntimeService =
352                 ResourceAssignmentUtils.transformToRARuntimeService(
353                     bluePrintRuntimeService,
354                     "testResolveResourcesWithMappingAndTemplate"
355                 )
356
357             val artifactPrefix = "another"
358
359             // Prepare Inputs
360             PayloadUtils.prepareInputsFromWorkflowPayload(
361                 bluePrintRuntimeService,
362                 executionServiceInput.payload,
363                 "resource-assignment"
364             )
365
366             assertNotNull(
367                 resourceResolutionService.resolveResources(
368                     resourceAssignmentRuntimeService,
369                     "resource-assignment",
370                     artifactPrefix,
371                     props
372                 ),
373                 "Couldn't Resolve Resources for artifact $artifactPrefix"
374             )
375         }
376     }
377
378     @Test
379     fun testResourceResolutionForDefinition() {
380         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
381             resourceDefinition(name = "port-speed", description = "Port Speed") {
382                 property(type = "string", required = true)
383                 sources {
384                     sourceCapability(id = "sdno", description = "SDNO Source") {
385                         definedProperties {
386                             type(BluePrintConstants.SCRIPT_KOTLIN)
387                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
388                             keyDependencies(arrayListOf("device-id"))
389                         }
390                     }
391                     sourceDb(id = "sdnc", description = "SDNC Controller") {
392                         definedProperties {
393                             endpointSelector("processor-db")
394                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
395                             inputKeyMapping {
396                                 map("device_id", "\$device-id")
397                             }
398                             keyDependencies(arrayListOf("device-id"))
399                         }
400                     }
401                 }
402             }
403             resourceDefinition(name = "device-id", description = "Device Id") {
404                 property(type = "string", required = true) {
405                     sources {
406                         sourceInput(id = "input", description = "Dependency Source") {}
407                     }
408                 }
409             }
410         }
411         runBlocking {
412             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
413             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
414             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
415             every { raRuntimeService.setBluePrintError(any()) } returns Unit
416             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
417             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
418
419             val applicationContext = mockk<ApplicationContext>()
420             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
421             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
422             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
423
424             val sources = arrayListOf<String>("sdno", "sdnc")
425
426             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
427             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
428                 raRuntimeService,
429                 resourceDefinitions, "port-speed", sources
430             )
431             assertNotNull(resolvedResources, "failed to resolve the resources")
432         }
433     }
434 }