Fix: Run both sonar and clm scans in parallel
[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 testResolveResourceWithTransform() {
159         runBlocking {
160
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             val resourceAssignmentRuntimeService =
175                 ResourceAssignmentUtils.transformToRARuntimeService(
176                     bluePrintRuntimeService,
177                     "testResolveResourceWithTransform"
178                 )
179
180             // Prepare Inputs
181             PayloadUtils.prepareInputsFromWorkflowPayload(
182                 bluePrintRuntimeService,
183                 executionServiceInput.payload,
184                 "resource-assignment"
185             )
186
187             resourceResolutionService.resolveResources(
188                 resourceAssignmentRuntimeService,
189                 "resource-assignment",
190                 "transform",
191                 props
192             )
193         }.let { (templateMap, assignmentList) ->
194             assertEquals("This is Sample Velocity Template", templateMap)
195
196             val expectedAssignmentList = mutableListOf(
197                 "service-instance-id" to "siid_1234",
198                 "vnf-id" to "vnf_1234",
199                 "vnf_name" to "temp_vnf",
200                 "private_net_id" to "temp_vnf_private2"
201             )
202             assertEquals(expectedAssignmentList.size, assignmentList.size)
203
204             val areEqual = expectedAssignmentList.zip(assignmentList).all { (it1, it2) ->
205                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
206             }
207             assertEquals(true, areEqual)
208         }
209     }
210
211     /**
212      * Always perform new resolution even if resolution exists in the database.
213      */
214     @Test
215     @Throws(Exception::class)
216     fun testResolveResourceAlwaysPerformNewResolution() {
217         runBlocking {
218             // Occurrence <= 0 indicates to perform new resolution even if resolution exists in the database.
219             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = -1
220             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
221
222             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
223                 "1234",
224                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
225             )
226
227             // Request#1
228             val executionServiceInput =
229                 JacksonUtils.readValueFromClassPathFile(
230                     "payload/requests/sample-resourceresolution-request.json",
231                     ExecutionServiceInput::class.java
232                 )!!
233
234             val resourceAssignmentRuntimeService =
235                 ResourceAssignmentUtils.transformToRARuntimeService(
236                     bluePrintRuntimeService,
237                     "testResolveResource"
238                 )
239
240             // Prepare inputs from Request#1
241             PayloadUtils.prepareInputsFromWorkflowPayload(
242                 bluePrintRuntimeService,
243                 executionServiceInput.payload,
244                 "resource-assignment"
245             )
246
247             // Resolve resources as per Request#1
248             resourceResolutionService.resolveResources(
249                 resourceAssignmentRuntimeService,
250                 "resource-assignment",
251                 "baseconfig",
252                 props
253             )
254
255             // Request#2
256             val executionServiceInput2 =
257                 JacksonUtils.readValueFromClassPathFile(
258                     "payload/requests/sample-resourceresolution-request2.json",
259                     ExecutionServiceInput::class.java
260                 )!!
261
262             // Prepare inputs from Request#2
263             PayloadUtils.prepareInputsFromWorkflowPayload(
264                 bluePrintRuntimeService,
265                 executionServiceInput2.payload,
266                 "resource-assignment"
267             )
268
269             // Resolve resources as per Request#2
270             resourceResolutionService.resolveResources(
271                 resourceAssignmentRuntimeService,
272                 "resource-assignment",
273                 "baseconfig",
274                 props
275             )
276         }.let { (templateMap, assignmentList) ->
277             assertEquals("This is Sample Velocity Template", templateMap)
278
279             val assignmentListForRequest1 = mutableListOf(
280                 "service-instance-id" to "siid_1234",
281                 "vnf-id" to "vnf_1234",
282                 "vnf_name" to "temp_vnf"
283             )
284             val assignmentListForRequest2 = mutableListOf(
285                 "service-instance-id" to "siid_new_resolution",
286                 "vnf-id" to "vnf_new_resolution",
287                 "vnf_name" to "temp_vnf_new_resolution"
288             )
289             assertEquals(assignmentListForRequest1.size, assignmentList.size)
290             assertEquals(assignmentListForRequest2.size, assignmentList.size)
291
292             // AlwaysPerformNewResolution use case - resolution request #2 should returns the resolution as per
293             // assignmentListForRequest2 since new resolution is performed.
294             var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
295                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
296             }
297             assertEquals(false, areEqual)
298
299             areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
300                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
301             }
302             assertEquals(true, areEqual)
303         }
304     }
305
306     /**
307      * Always perform new resolution even if resolution exists in the database.
308      */
309     @Test
310     @Throws(Exception::class)
311     fun testResolveResourcesForMaxOccurrence() {
312         runBlocking {
313             // Occurrence <= 0 indicates to perform new resolution even if resolution exists in the database.
314             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = -1
315             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
316
317             // Run time for Request#1
318             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
319                 "1234",
320                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
321             )
322
323             // Request#1
324             val executionServiceInput =
325                 JacksonUtils.readValueFromClassPathFile(
326                     "payload/requests/sample-resourceresolution-request.json",
327                     ExecutionServiceInput::class.java
328                 )!!
329
330             // Prepare inputs from Request#1
331             PayloadUtils.prepareInputsFromWorkflowPayload(
332                 bluePrintRuntimeService,
333                 executionServiceInput.payload,
334                 "resource-assignment"
335             )
336
337             val resourceAssignmentRuntimeService =
338                 ResourceAssignmentUtils.transformToRARuntimeService(
339                     bluePrintRuntimeService,
340                     "testResolveResource"
341                 )
342
343             // Resolve resources as per Request#1
344             resourceResolutionService.resolveResources(
345                 resourceAssignmentRuntimeService,
346                 "resource-assignment",
347                 "maxoccurrence",
348                 props
349             )
350
351             // Run time for Request#2
352             val bluePrintRuntimeService2 = BluePrintMetadataUtils.getBluePrintRuntime(
353                 "1234",
354                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
355             )
356
357             // Request#2
358             val executionServiceInput2 =
359                 JacksonUtils.readValueFromClassPathFile(
360                     "payload/requests/sample-resourceresolution-request2.json",
361                     ExecutionServiceInput::class.java
362                 )!!
363
364             val resourceAssignmentRuntimeService2 =
365                 ResourceAssignmentUtils.transformToRARuntimeService(
366                     bluePrintRuntimeService2,
367                     "testResolveResource"
368                 )
369
370             // Prepare inputs from Request#2
371             PayloadUtils.prepareInputsFromWorkflowPayload(
372                 bluePrintRuntimeService2,
373                 executionServiceInput2.payload,
374                 "resource-assignment"
375             )
376
377             // Resolve resources as per Request#2
378             resourceResolutionService.resolveResources(
379                 resourceAssignmentRuntimeService2,
380                 "resource-assignment",
381                 "maxoccurrence",
382                 props
383             )
384         }.let { (template, assignmentList) ->
385             assertEquals("This is maxoccurrence Velocity Template", template)
386
387             val assignmentListForRequest1 = mutableListOf(
388                 "firmware-version" to "firmware-version-0",
389                 "ip-address" to "192.0.0.1"
390             )
391             val assignmentListForRequest2 = mutableListOf(
392                 "firmware-version" to "firmware-version-1",
393                 "ip-address" to "192.0.0.1"
394             )
395             assertEquals(assignmentListForRequest1.size, assignmentList.size)
396             assertEquals(assignmentListForRequest2.size, assignmentList.size)
397
398             // firmware-version has max-occurrence = 0 means perform new resolution all the time.
399             // ip-address has max-occurrence = 1 so its resolution should only be done once.
400             //
401             // AlwaysPerformNewResolution + max-occurrence feature use case - resolution request #2 should returns
402             // the resolution as per assignmentListForRequest2 since new resolution is only performed for
403             // firmware-version and not for ip-address.
404             var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
405                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
406             }
407             assertEquals(false, areEqual)
408
409             areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
410                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
411             }
412             assertEquals(true, areEqual)
413         }
414     }
415
416     /**
417      * Don't perform new resolution in case resolution already exists in the database.
418      */
419     @Test
420     @Throws(Exception::class)
421     fun testResolveResourceNoNewResolution() {
422         runBlocking {
423             // Occurrence > 0 indicates to not perform new resolution if resolution exists in the database.
424             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = 1
425             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
426
427             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
428                 "1234",
429                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
430             )
431
432             // Request#1
433             val executionServiceInput =
434                 JacksonUtils.readValueFromClassPathFile(
435                     "payload/requests/sample-resourceresolution-request.json",
436                     ExecutionServiceInput::class.java
437                 )!!
438
439             val resourceAssignmentRuntimeService =
440                 ResourceAssignmentUtils.transformToRARuntimeService(
441                     bluePrintRuntimeService,
442                     "testResolveResource"
443                 )
444
445             // Prepare inputs from Request#1
446             PayloadUtils.prepareInputsFromWorkflowPayload(
447                 bluePrintRuntimeService,
448                 executionServiceInput.payload,
449                 "resource-assignment"
450             )
451             // Resolve resources as per Request#1
452             resourceResolutionService.resolveResources(
453                 resourceAssignmentRuntimeService,
454                 "resource-assignment",
455                 "baseconfig",
456                 props
457             )
458
459             // Request#2
460             val executionServiceInput2 =
461                 JacksonUtils.readValueFromClassPathFile(
462                     "payload/requests/sample-resourceresolution-request2.json",
463                     ExecutionServiceInput::class.java
464                 )!!
465
466             // Prepare inputs from Request#2
467             PayloadUtils.prepareInputsFromWorkflowPayload(
468                 bluePrintRuntimeService,
469                 executionServiceInput2.payload,
470                 "resource-assignment"
471             )
472
473             // Resolve resources as per Request#2
474             resourceResolutionService.resolveResources(
475                 resourceAssignmentRuntimeService,
476                 "resource-assignment",
477                 "baseconfig",
478                 props
479             )
480         }.let { (templateMap, assignmentList) ->
481             assertEquals("This is Sample Velocity Template", templateMap)
482
483             val assignmentListForRequest1 = mutableListOf(
484                 "service-instance-id" to "siid_1234",
485                 "vnf-id" to "vnf_1234",
486                 "vnf_name" to "temp_vnf"
487             )
488             val assignmentListForRequest2 = mutableListOf(
489                 "service-instance-id" to "siid_new_resolution",
490                 "vnf-id" to "vnf_new_resolution",
491                 "vnf_name" to "temp_vnf_new_resolution"
492             )
493             assertEquals(assignmentListForRequest1.size, assignmentList.size)
494             assertEquals(assignmentListForRequest2.size, assignmentList.size)
495
496             //  NoNewResolution use case - resolution for request #2 returns the same resolution  as
497             //  assignmentListForRequest1 since no new resolution is/was actually performed.
498             var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
499                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
500             }
501             assertEquals(true, areEqual)
502
503             areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
504                 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
505             }
506             assertEquals(false, areEqual)
507         }
508     }
509
510     @Test
511     @Throws(Exception::class)
512     fun testResolveResources() {
513         val artifactNames = listOf("baseconfig", "another")
514         runBlocking {
515             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
516
517             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
518                 "1234",
519                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
520             )
521
522             val executionServiceInput =
523                 JacksonUtils.readValueFromClassPathFile(
524                     "payload/requests/sample-resourceresolution-request.json",
525                     ExecutionServiceInput::class.java
526                 )!!
527
528             // Prepare Inputs
529             PayloadUtils.prepareInputsFromWorkflowPayload(
530                 bluePrintRuntimeService,
531                 executionServiceInput.payload,
532                 "resource-assignment"
533             )
534
535             resourceResolutionService.resolveResources(
536                 bluePrintRuntimeService,
537                 "resource-assignment",
538                 artifactNames,
539                 props,
540                 "mockStep"
541             )
542         }.let {
543             assertEquals(artifactNames.toSet(), it.templateMap.keys)
544             assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
545
546             assertEquals("This is Sample Velocity Template", it.templateMap["another"])
547             assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
548         }
549     }
550
551     @Test
552     @Throws(Exception::class)
553     fun testResolveResourcesWithMappingAndTemplate() {
554         runBlocking {
555             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
556
557             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
558                 "1234",
559                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
560             )
561
562             val executionServiceInput =
563                 JacksonUtils.readValueFromClassPathFile(
564                     "payload/requests/sample-resourceresolution-request.json",
565                     ExecutionServiceInput::class.java
566                 )!!
567
568             val resourceAssignmentRuntimeService =
569                 ResourceAssignmentUtils.transformToRARuntimeService(
570                     bluePrintRuntimeService,
571                     "testResolveResourcesWithMappingAndTemplate"
572                 )
573
574             val artifactPrefix = "another"
575
576             // Prepare Inputs
577             PayloadUtils.prepareInputsFromWorkflowPayload(
578                 bluePrintRuntimeService,
579                 executionServiceInput.payload,
580                 "resource-assignment"
581             )
582
583             assertNotNull(
584                 resourceResolutionService.resolveResources(
585                     resourceAssignmentRuntimeService,
586                     "resource-assignment",
587                     artifactPrefix,
588                     props
589                 ),
590                 "Couldn't Resolve Resources for artifact $artifactPrefix"
591             )
592         }
593     }
594
595     @Test
596     @Throws(Exception::class)
597     fun testResolveResourcesResolutionSummary() {
598         runBlocking {
599             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
600             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
601
602             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
603                 "1234",
604                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
605             )
606
607             val executionServiceInput =
608                 JacksonUtils.readValueFromClassPathFile(
609                     "payload/requests/sample-resourceresolution-request.json",
610                     ExecutionServiceInput::class.java
611                 )!!
612
613             val resourceAssignmentRuntimeService =
614                 ResourceAssignmentUtils.transformToRARuntimeService(
615                     bluePrintRuntimeService,
616                     "testResolveResourcesWithMappingAndTemplate"
617                 )
618
619             val artifactPrefix = "notemplate"
620
621             // Prepare Inputs
622             PayloadUtils.prepareInputsFromWorkflowPayload(
623                 bluePrintRuntimeService,
624                 executionServiceInput.payload,
625                 "resource-assignment"
626             )
627
628             resourceResolutionService.resolveResources(
629                 resourceAssignmentRuntimeService,
630                 "resource-assignment",
631                 artifactPrefix,
632                 props
633             )
634         }.let {
635             val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
636             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
637             assertEquals(list.size, 3)
638         }
639     }
640
641     @Test
642     @Throws(Exception::class)
643     fun testResolveResourcesWithoutTemplate() {
644         val artifactPrefix = "notemplate"
645         runBlocking {
646             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
647
648             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
649                 "1234",
650                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
651             )
652
653             val executionServiceInput =
654                 JacksonUtils.readValueFromClassPathFile(
655                     "payload/requests/sample-resourceresolution-request.json",
656                     ExecutionServiceInput::class.java
657                 )!!
658
659             val resourceAssignmentRuntimeService =
660                 ResourceAssignmentUtils.transformToRARuntimeService(
661                     bluePrintRuntimeService,
662                     "testResolveResourcesWithMappingAndTemplate"
663                 )
664
665             // Prepare Inputs
666             PayloadUtils.prepareInputsFromWorkflowPayload(
667                 bluePrintRuntimeService,
668                 executionServiceInput.payload,
669                 "resource-assignment"
670             )
671
672             resourceResolutionService.resolveResources(
673                 resourceAssignmentRuntimeService,
674                 "resource-assignment",
675                 artifactPrefix,
676                 props
677             )
678         }.let {
679             assertEquals(
680                 """
681                 {
682                   "service-instance-id" : "siid_1234",
683                   "vnf-id" : "vnf_1234",
684                   "vnf_name" : "temp_vnf"
685                 }
686                 """.trimIndent(),
687                 it.first
688             )
689             val areEqual = it.second.first().name == "service-instance-id" &&
690                 "siid_1234" == it.second.first().property?.value?.asText() ?: null
691             assertEquals(true, areEqual)
692         }
693     }
694
695     @Test
696     fun testResolveResourcesWithResourceIdAndResourceType() {
697         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
698         runBlocking {
699             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
700
701             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
702                 "1234",
703                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
704             )
705
706             val executionServiceInput =
707                 JacksonUtils.readValueFromClassPathFile(
708                     "payload/requests/sample-resourceresolution-request.json",
709                     ExecutionServiceInput::class.java
710                 )!!
711
712             val resourceAssignmentRuntimeService =
713                 ResourceAssignmentUtils.transformToRARuntimeService(
714                     bluePrintRuntimeService,
715                     "testResolveResourcesWithMappingAndTemplate"
716                 )
717
718             val artifactPrefix = "another"
719
720             // Prepare Inputs
721             PayloadUtils.prepareInputsFromWorkflowPayload(
722                 bluePrintRuntimeService,
723                 executionServiceInput.payload,
724                 "resource-assignment"
725             )
726
727             assertNotNull(
728                 resourceResolutionService.resolveResources(
729                     resourceAssignmentRuntimeService,
730                     "resource-assignment",
731                     artifactPrefix,
732                     props
733                 ),
734                 "Couldn't Resolve Resources for artifact $artifactPrefix"
735             )
736         }
737     }
738
739     @Test
740     fun testResourceResolutionForDefinition() {
741         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
742             resourceDefinition(name = "port-speed", description = "Port Speed") {
743                 property(type = "string", required = true)
744                 sources {
745                     sourceCapability(id = "sdno", description = "SDNO Source") {
746                         definedProperties {
747                             type(BluePrintConstants.SCRIPT_KOTLIN)
748                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
749                             keyDependencies(arrayListOf("device-id"))
750                         }
751                     }
752                     sourceDb(id = "sdnc", description = "SDNC Controller") {
753                         definedProperties {
754                             endpointSelector("processor-db")
755                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
756                             inputKeyMapping {
757                                 map("device_id", "\$device-id")
758                             }
759                             keyDependencies(arrayListOf("device-id"))
760                         }
761                     }
762                 }
763             }
764             resourceDefinition(name = "device-id", description = "Device Id") {
765                 property(type = "string", required = true) {
766                     sources {
767                         sourceInput(id = "input", description = "Dependency Source") {}
768                     }
769                 }
770             }
771         }
772         runBlocking {
773             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
774             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
775             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
776             every { raRuntimeService.setBluePrintError(any()) } returns Unit
777             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
778             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
779
780             val applicationContext = mockk<ApplicationContext>()
781             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
782             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
783             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
784
785             val sources = arrayListOf<String>("sdno", "sdnc")
786
787             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
788             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
789                 raRuntimeService,
790                 resourceDefinitions, "port-speed", sources
791             )
792             assertNotNull(resolvedResources, "failed to resolve the resources")
793         }
794     }
795 }