0a12540d4445453490e07dd849468fae2bc9ec9e
[ccsdk/cds.git] /
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 list = JacksonUtils.getListFromJson(it, ResolutionSummary::class.java)
260             assertEquals(list.size, 3)
261         }
262     }
263
264     @Test
265     @Throws(Exception::class)
266     fun testResolveResourcesWithoutTemplate() {
267         runBlocking {
268             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
269
270             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
271                     "1234",
272                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
273             )
274
275             val executionServiceInput =
276                     JacksonUtils.readValueFromClassPathFile(
277                             "payload/requests/sample-resourceresolution-request.json",
278                             ExecutionServiceInput::class.java
279                     )!!
280
281             val resourceAssignmentRuntimeService =
282                     ResourceAssignmentUtils.transformToRARuntimeService(
283                             bluePrintRuntimeService,
284                             "testResolveResourcesWithMappingAndTemplate"
285                     )
286
287             val artifactPrefix = "notemplate"
288
289             // Prepare Inputs
290             PayloadUtils.prepareInputsFromWorkflowPayload(
291                     bluePrintRuntimeService,
292                     executionServiceInput.payload,
293                     "resource-assignment"
294             )
295
296             resourceResolutionService.resolveResources(
297                     resourceAssignmentRuntimeService,
298                     "resource-assignment",
299                     artifactPrefix,
300                     props
301             )
302         }.let {
303             assertEquals("""
304                 {
305                   "service-instance-id" : "siid_1234",
306                   "vnf-id" : "vnf_1234",
307                   "vnf_name" : "temp_vnf"
308                 }
309             """.trimIndent(), it)
310         }
311     }
312
313     @Test
314     fun testResolveResourcesWithResourceIdAndResourceType() {
315         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
316         runBlocking {
317             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
318
319             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
320                 "1234",
321                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
322             )
323
324             val executionServiceInput =
325                 JacksonUtils.readValueFromClassPathFile(
326                     "payload/requests/sample-resourceresolution-request.json",
327                     ExecutionServiceInput::class.java
328                 )!!
329
330             val resourceAssignmentRuntimeService =
331                 ResourceAssignmentUtils.transformToRARuntimeService(
332                     bluePrintRuntimeService,
333                     "testResolveResourcesWithMappingAndTemplate"
334                 )
335
336             val artifactPrefix = "another"
337
338             // Prepare Inputs
339             PayloadUtils.prepareInputsFromWorkflowPayload(
340                 bluePrintRuntimeService,
341                 executionServiceInput.payload,
342                 "resource-assignment"
343             )
344
345             assertNotNull(
346                 resourceResolutionService.resolveResources(
347                     resourceAssignmentRuntimeService,
348                     "resource-assignment",
349                     artifactPrefix,
350                     props
351                 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
352             )
353         }
354     }
355
356     @Test
357     fun testResourceResolutionForDefinition() {
358         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
359             resourceDefinition(name = "port-speed", description = "Port Speed") {
360                 property(type = "string", required = true)
361                 sources {
362                     sourceCapability(id = "sdno", description = "SDNO Source") {
363                         definedProperties {
364                             type(BluePrintConstants.SCRIPT_KOTLIN)
365                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
366                             keyDependencies(arrayListOf("device-id"))
367                         }
368                     }
369                     sourceDb(id = "sdnc", description = "SDNC Controller") {
370                         definedProperties {
371                             endpointSelector("processor-db")
372                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
373                             inputKeyMapping {
374                                 map("device_id", "\$device-id")
375                             }
376                             keyDependencies(arrayListOf("device-id"))
377                         }
378                     }
379                 }
380             }
381             resourceDefinition(name = "device-id", description = "Device Id") {
382                 property(type = "string", required = true) {
383                     sources {
384                         sourceInput(id = "input", description = "Dependency Source") {}
385                     }
386                 }
387             }
388         }
389         runBlocking {
390             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
391             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
392             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
393             every { raRuntimeService.setBluePrintError(any()) } returns Unit
394             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
395             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
396
397             val applicationContext = mockk<ApplicationContext>()
398             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
399             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
400             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
401
402             val sources = arrayListOf<String>("sdno", "sdnc")
403
404             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
405             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
406                 raRuntimeService,
407                 resourceDefinitions, "port-speed", sources
408             )
409             assertNotNull(resolvedResources, "failed to resolve the resources")
410         }
411     }
412 }