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