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