Add ResourceResolutionResult to ResourceResolutionService
[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             ), "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, assignmentMap) ->
140             assertEquals("This is Sample Velocity Template", templateMap)
141
142             val expectedAssignmentMap = hashMapOf(
143                     "service-instance-id" to "siid_1234",
144                     "vnf-id" to "vnf_1234",
145                     "vnf_name" to "temp_vnf"
146             ).asJsonType()
147             assertEquals(expectedAssignmentMap, assignmentMap)
148         }
149     }
150
151     @Test
152     @Throws(Exception::class)
153     fun testResolveResources() {
154         val artifactNames = listOf("baseconfig", "another")
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             // Prepare Inputs
170             PayloadUtils.prepareInputsFromWorkflowPayload(
171                 bluePrintRuntimeService,
172                 executionServiceInput.payload,
173                 "resource-assignment"
174             )
175
176             resourceResolutionService.resolveResources(
177                 bluePrintRuntimeService,
178                 "resource-assignment",
179                 artifactNames,
180                 props
181             )
182         }.let {
183             assertEquals(artifactNames.toSet(), it.templateMap.keys)
184             assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
185
186             assertEquals("This is Sample Velocity Template", it.templateMap["another"])
187             assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
188         }
189     }
190
191     @Test
192     @Throws(Exception::class)
193     fun testResolveResourcesWithMappingAndTemplate() {
194         runBlocking {
195             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
196
197             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
198                 "1234",
199                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
200             )
201
202             val executionServiceInput =
203                 JacksonUtils.readValueFromClassPathFile(
204                     "payload/requests/sample-resourceresolution-request.json",
205                     ExecutionServiceInput::class.java
206                 )!!
207
208             val resourceAssignmentRuntimeService =
209                 ResourceAssignmentUtils.transformToRARuntimeService(
210                     bluePrintRuntimeService,
211                     "testResolveResourcesWithMappingAndTemplate"
212                 )
213
214             val artifactPrefix = "another"
215
216             // Prepare Inputs
217             PayloadUtils.prepareInputsFromWorkflowPayload(
218                 bluePrintRuntimeService,
219                 executionServiceInput.payload,
220                 "resource-assignment"
221             )
222
223             assertNotNull(
224                 resourceResolutionService.resolveResources(
225                     resourceAssignmentRuntimeService,
226                     "resource-assignment",
227                     artifactPrefix,
228                     props
229                 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
230             )
231         }
232     }
233
234     @Test
235     @Throws(Exception::class)
236     fun testResolveResourcesResolutionSummary() {
237         runBlocking {
238             props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
239             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
240
241             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
242                     "1234",
243                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
244             )
245
246             val executionServiceInput =
247                     JacksonUtils.readValueFromClassPathFile(
248                             "payload/requests/sample-resourceresolution-request.json",
249                             ExecutionServiceInput::class.java
250                     )!!
251
252             val resourceAssignmentRuntimeService =
253                     ResourceAssignmentUtils.transformToRARuntimeService(
254                             bluePrintRuntimeService,
255                             "testResolveResourcesWithMappingAndTemplate"
256                     )
257
258             val artifactPrefix = "notemplate"
259
260             // Prepare Inputs
261             PayloadUtils.prepareInputsFromWorkflowPayload(
262                     bluePrintRuntimeService,
263                     executionServiceInput.payload,
264                     "resource-assignment"
265             )
266
267             resourceResolutionService.resolveResources(
268                     resourceAssignmentRuntimeService,
269                     "resource-assignment",
270                     artifactPrefix,
271                     props
272             )
273         }.let {
274             val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
275             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
276             assertEquals(list.size, 3)
277         }
278     }
279
280     @Test
281     @Throws(Exception::class)
282     fun testResolveResourcesWithoutTemplate() {
283         val artifactPrefix = "notemplate"
284         runBlocking {
285             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
286
287             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
288                     "1234",
289                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
290             )
291
292             val executionServiceInput =
293                     JacksonUtils.readValueFromClassPathFile(
294                             "payload/requests/sample-resourceresolution-request.json",
295                             ExecutionServiceInput::class.java
296                     )!!
297
298             val resourceAssignmentRuntimeService =
299                     ResourceAssignmentUtils.transformToRARuntimeService(
300                             bluePrintRuntimeService,
301                             "testResolveResourcesWithMappingAndTemplate"
302                     )
303
304             // Prepare Inputs
305             PayloadUtils.prepareInputsFromWorkflowPayload(
306                     bluePrintRuntimeService,
307                     executionServiceInput.payload,
308                     "resource-assignment"
309             )
310
311             resourceResolutionService.resolveResources(
312                     resourceAssignmentRuntimeService,
313                     "resource-assignment",
314                     artifactPrefix,
315                     props
316             )
317         }.let {
318             assertEquals("""
319                 {
320                   "service-instance-id" : "siid_1234",
321                   "vnf-id" : "vnf_1234",
322                   "vnf_name" : "temp_vnf"
323                 }
324             """.trimIndent(), it.first)
325             assertEquals("siid_1234", it.second["service-instance-id"].asText())
326         }
327     }
328
329     @Test
330     fun testResolveResourcesWithResourceIdAndResourceType() {
331         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
332         runBlocking {
333             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
334
335             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
336                 "1234",
337                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
338             )
339
340             val executionServiceInput =
341                 JacksonUtils.readValueFromClassPathFile(
342                     "payload/requests/sample-resourceresolution-request.json",
343                     ExecutionServiceInput::class.java
344                 )!!
345
346             val resourceAssignmentRuntimeService =
347                 ResourceAssignmentUtils.transformToRARuntimeService(
348                     bluePrintRuntimeService,
349                     "testResolveResourcesWithMappingAndTemplate"
350                 )
351
352             val artifactPrefix = "another"
353
354             // Prepare Inputs
355             PayloadUtils.prepareInputsFromWorkflowPayload(
356                 bluePrintRuntimeService,
357                 executionServiceInput.payload,
358                 "resource-assignment"
359             )
360
361             assertNotNull(
362                 resourceResolutionService.resolveResources(
363                     resourceAssignmentRuntimeService,
364                     "resource-assignment",
365                     artifactPrefix,
366                     props
367                 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
368             )
369         }
370     }
371
372     @Test
373     fun testResourceResolutionForDefinition() {
374         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
375             resourceDefinition(name = "port-speed", description = "Port Speed") {
376                 property(type = "string", required = true)
377                 sources {
378                     sourceCapability(id = "sdno", description = "SDNO Source") {
379                         definedProperties {
380                             type(BluePrintConstants.SCRIPT_KOTLIN)
381                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
382                             keyDependencies(arrayListOf("device-id"))
383                         }
384                     }
385                     sourceDb(id = "sdnc", description = "SDNC Controller") {
386                         definedProperties {
387                             endpointSelector("processor-db")
388                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
389                             inputKeyMapping {
390                                 map("device_id", "\$device-id")
391                             }
392                             keyDependencies(arrayListOf("device-id"))
393                         }
394                     }
395                 }
396             }
397             resourceDefinition(name = "device-id", description = "Device Id") {
398                 property(type = "string", required = true) {
399                     sources {
400                         sourceInput(id = "input", description = "Dependency Source") {}
401                     }
402                 }
403             }
404         }
405         runBlocking {
406             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
407             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
408             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
409             every { raRuntimeService.setBluePrintError(any()) } returns Unit
410             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
411             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
412
413             val applicationContext = mockk<ApplicationContext>()
414             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
415             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
416             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
417
418             val sources = arrayListOf<String>("sdno", "sdnc")
419
420             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
421             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
422                 raRuntimeService,
423                 resourceDefinitions, "port-speed", sources
424             )
425             assertNotNull(resolvedResources, "failed to resolve the resources")
426         }
427     }
428 }