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