aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt
blob: a1de557d5318ca536f005d602cde1890785af5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 *
 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution

import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.MockCapabilityScriptRA
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintError
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
import org.onap.ccsdk.cds.controllerblueprints.core.utils.BlueprintMetadataUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.ComponentScan
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit4.SpringRunner
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

/**
 * ResourceResolutionServiceTest
 *
 * @author Brinda Santh DATE : 8/15/2018
 */
@RunWith(SpringRunner::class)
@ContextConfiguration(
    classes = [TestDatabaseConfiguration::class]
)
@TestPropertySource(locations = ["classpath:application-test.properties"])
@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
@EnableAutoConfiguration
class ResourceResolutionServiceTest {

    private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)

    @Autowired
    lateinit var resourceResolutionService: ResourceResolutionService

    private val props = hashMapOf<String, Any>()
    private val resolutionKey = "resolutionKey"
    private val resourceId = "1"
    private val resourceType = "ServiceInstance"
    private val occurrence = 0

    @Before
    fun setup() {
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = false
    }

    @Test
    fun testRegisteredSource() {
        val sources = resourceResolutionService.registeredResourceSources()
        assertNotNull(sources, "failed to get registered sources")
        assertTrue(
            sources.containsAll(
                arrayListOf(
                    "source-input", "source-default", "source-db",
                    "source-rest", "source-capability"
                )
            ),
            "failed to get registered sources : $sources"
        )
    }

    @Test
    @Throws(Exception::class)
    fun testResolveResource() {
        runBlocking {

            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            val resourceAssignmentRuntimeService =
                ResourceAssignmentUtils.transformToRARuntimeService(
                    bluePrintRuntimeService,
                    "testResolveResource"
                )

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            resourceResolutionService.resolveResources(
                resourceAssignmentRuntimeService,
                "resource-assignment",
                "baseconfig",
                props
            )
        }.let { (templateMap, assignmentList) ->
            assertEquals("This is Sample Velocity Template", templateMap)

            val expectedAssignmentList = mutableListOf(
                "service-instance-id" to "siid_1234",
                "vnf-id" to "vnf_1234",
                "vnf_name" to "temp_vnf"
            )
            assertEquals(expectedAssignmentList.size, assignmentList.size)

            val areEqual = expectedAssignmentList.zip(assignmentList).all { (it1, it2) ->
                it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
            }
            assertEquals(true, areEqual)
        }
    }

    @Test
    @Throws(Exception::class)
    fun testResolveResources() {
        val artifactNames = listOf("baseconfig", "another")
        runBlocking {
            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            resourceResolutionService.resolveResources(
                bluePrintRuntimeService,
                "resource-assignment",
                artifactNames,
                props,
                "mockStep"
            )
        }.let {
            assertEquals(artifactNames.toSet(), it.templateMap.keys)
            assertEquals(artifactNames.toSet(), it.assignmentMap.keys)

            assertEquals("This is Sample Velocity Template", it.templateMap["another"])
            assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
        }
    }

    @Test
    @Throws(Exception::class)
    fun testResolveResourcesWithMappingAndTemplate() {
        runBlocking {
            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            val resourceAssignmentRuntimeService =
                ResourceAssignmentUtils.transformToRARuntimeService(
                    bluePrintRuntimeService,
                    "testResolveResourcesWithMappingAndTemplate"
                )

            val artifactPrefix = "another"

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            assertNotNull(
                resourceResolutionService.resolveResources(
                    resourceAssignmentRuntimeService,
                    "resource-assignment",
                    artifactPrefix,
                    props
                ),
                "Couldn't Resolve Resources for artifact $artifactPrefix"
            )
        }
    }

    @Test
    @Throws(Exception::class)
    fun testResolveResourcesResolutionSummary() {
        runBlocking {
            props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            val resourceAssignmentRuntimeService =
                ResourceAssignmentUtils.transformToRARuntimeService(
                    bluePrintRuntimeService,
                    "testResolveResourcesWithMappingAndTemplate"
                )

            val artifactPrefix = "notemplate"

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            resourceResolutionService.resolveResources(
                resourceAssignmentRuntimeService,
                "resource-assignment",
                artifactPrefix,
                props
            )
        }.let {
            val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
            val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
            assertEquals(list.size, 3)
        }
    }

    @Test
    @Throws(Exception::class)
    fun testResolveResourcesWithoutTemplate() {
        val artifactPrefix = "notemplate"
        runBlocking {
            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            val resourceAssignmentRuntimeService =
                ResourceAssignmentUtils.transformToRARuntimeService(
                    bluePrintRuntimeService,
                    "testResolveResourcesWithMappingAndTemplate"
                )

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            resourceResolutionService.resolveResources(
                resourceAssignmentRuntimeService,
                "resource-assignment",
                artifactPrefix,
                props
            )
        }.let {
            assertEquals(
                """
                {
                  "service-instance-id" : "siid_1234",
                  "vnf-id" : "vnf_1234",
                  "vnf_name" : "temp_vnf"
                }
                """.trimIndent(),
                it.first
            )
            val areEqual = it.second.first().name == "service-instance-id" &&
                "siid_1234" == it.second.first().property?.value?.asText() ?: null
            assertEquals(true, areEqual)
        }
    }

    @Test
    fun testResolveResourcesWithResourceIdAndResourceType() {
        props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
        runBlocking {
            Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)

            val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
                "1234",
                "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
            )

            val executionServiceInput =
                JacksonUtils.readValueFromClassPathFile(
                    "payload/requests/sample-resourceresolution-request.json",
                    ExecutionServiceInput::class.java
                )!!

            val resourceAssignmentRuntimeService =
                ResourceAssignmentUtils.transformToRARuntimeService(
                    bluePrintRuntimeService,
                    "testResolveResourcesWithMappingAndTemplate"
                )

            val artifactPrefix = "another"

            // Prepare Inputs
            PayloadUtils.prepareInputsFromWorkflowPayload(
                bluePrintRuntimeService,
                executionServiceInput.payload,
                "resource-assignment"
            )

            assertNotNull(
                resourceResolutionService.resolveResources(
                    resourceAssignmentRuntimeService,
                    "resource-assignment",
                    artifactPrefix,
                    props
                ),
                "Couldn't Resolve Resources for artifact $artifactPrefix"
            )
        }
    }

    @Test
    fun testResourceResolutionForDefinition() {
        val resourceDefinitions = BlueprintTypes.resourceDefinitions {
            resourceDefinition(name = "port-speed", description = "Port Speed") {
                property(type = "string", required = true)
                sources {
                    sourceCapability(id = "sdno", description = "SDNO Source") {
                        definedProperties {
                            type(BlueprintConstants.SCRIPT_KOTLIN)
                            scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
                            keyDependencies(arrayListOf("device-id"))
                        }
                    }
                    sourceDb(id = "sdnc", description = "SDNC Controller") {
                        definedProperties {
                            endpointSelector("processor-db")
                            query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
                            inputKeyMapping {
                                map("device_id", "\$device-id")
                            }
                            keyDependencies(arrayListOf("device-id"))
                        }
                    }
                }
            }
            resourceDefinition(name = "device-id", description = "Device Id") {
                property(type = "string", required = true) {
                    sources {
                        sourceInput(id = "input", description = "Dependency Source") {}
                    }
                }
            }
        }
        runBlocking {
            val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
            every { raRuntimeService.bluePrintContext() } returns mockk<BlueprintContext>()
            every { raRuntimeService.getBlueprintError() } returns BlueprintError()
            every { raRuntimeService.setBlueprintError(any()) } returns Unit
            every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
            every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit

            val applicationContext = mockk<ApplicationContext>()
            every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
            every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
            every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()

            val sources = arrayListOf<String>("sdno", "sdnc")

            val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
            val resolvedResources = resourceResolutionService.resolveResourceDefinition(
                raRuntimeService,
                resourceDefinitions, "port-speed", sources
            )
            assertNotNull(resolvedResources, "failed to resolve the resources")
        }
    }
}