2f338a3a1adbf229263cde189f07ed63fa23f1bf
[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.slf4j.LoggerFactory
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
42 import org.springframework.context.ApplicationContext
43 import org.springframework.context.annotation.ComponentScan
44 import org.springframework.test.context.ContextConfiguration
45 import org.springframework.test.context.TestPropertySource
46 import org.springframework.test.context.junit4.SpringRunner
47 import kotlin.test.assertNotNull
48 import kotlin.test.assertTrue
49
50 /**
51  * ResourceResolutionServiceTest
52  *
53  * @author Brinda Santh DATE : 8/15/2018
54  */
55 @RunWith(SpringRunner::class)
56 @ContextConfiguration(
57     classes = [TestDatabaseConfiguration::class]
58 )
59 @TestPropertySource(locations = ["classpath:application-test.properties"])
60 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
61 @EnableAutoConfiguration
62 class ResourceResolutionServiceTest {
63
64     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
65
66     @Autowired
67     lateinit var resourceResolutionService: ResourceResolutionService
68
69     private val props = hashMapOf<String, Any>()
70     private val resolutionKey = "resolutionKey"
71     private val resourceId = "1"
72     private val resourceType = "ServiceInstance"
73     private val occurrence = 0
74
75     @Before
76     fun setup() {
77         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
78         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
79         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
80         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
81         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
82     }
83
84     @Test
85     fun testRegisteredSource() {
86         val sources = resourceResolutionService.registeredResourceSources()
87         assertNotNull(sources, "failed to get registered sources")
88         assertTrue(
89             sources.containsAll(
90                 arrayListOf(
91                     "source-input", "source-default", "source-db",
92                     "source-rest", "source-capability"
93                 )
94             ), "failed to get registered sources : $sources"
95         )
96     }
97
98     @Test
99     @Throws(Exception::class)
100     fun testResolveResource() {
101         runBlocking {
102
103             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
104
105             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
106                 "1234",
107                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
108             )
109
110             val executionServiceInput =
111                 JacksonUtils.readValueFromClassPathFile(
112                     "payload/requests/sample-resourceresolution-request.json",
113                     ExecutionServiceInput::class.java
114                 )!!
115
116             val resourceAssignmentRuntimeService =
117                 ResourceAssignmentUtils.transformToRARuntimeService(
118                     bluePrintRuntimeService,
119                     "testResolveResource"
120                 )
121
122             // Prepare Inputs
123             PayloadUtils.prepareInputsFromWorkflowPayload(
124                 bluePrintRuntimeService,
125                 executionServiceInput.payload,
126                 "resource-assignment"
127             )
128
129             resourceResolutionService.resolveResources(
130                 resourceAssignmentRuntimeService,
131                 "resource-assignment",
132                 "baseconfig",
133                 props
134             )
135         }
136     }
137
138     @Test
139     @Throws(Exception::class)
140     fun testResolveResources() {
141         runBlocking {
142             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
143
144             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
145                 "1234",
146                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
147             )
148
149             val executionServiceInput =
150                 JacksonUtils.readValueFromClassPathFile(
151                     "payload/requests/sample-resourceresolution-request.json",
152                     ExecutionServiceInput::class.java
153                 )!!
154
155             val artefactNames = listOf("baseconfig", "another")
156
157             // Prepare Inputs
158             PayloadUtils.prepareInputsFromWorkflowPayload(
159                 bluePrintRuntimeService,
160                 executionServiceInput.payload,
161                 "resource-assignment"
162             )
163
164             resourceResolutionService.resolveResources(
165                 bluePrintRuntimeService,
166                 "resource-assignment",
167                 artefactNames,
168                 props
169             )
170         }
171     }
172
173     @Test
174     @Throws(Exception::class)
175     fun testResolveResourcesWithMappingAndTemplate() {
176         runBlocking {
177             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
178
179             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
180                 "1234",
181                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
182             )
183
184             val executionServiceInput =
185                 JacksonUtils.readValueFromClassPathFile(
186                     "payload/requests/sample-resourceresolution-request.json",
187                     ExecutionServiceInput::class.java
188                 )!!
189
190             val resourceAssignmentRuntimeService =
191                 ResourceAssignmentUtils.transformToRARuntimeService(
192                     bluePrintRuntimeService,
193                     "testResolveResourcesWithMappingAndTemplate"
194                 )
195
196             val artifactPrefix = "another"
197
198             // Prepare Inputs
199             PayloadUtils.prepareInputsFromWorkflowPayload(
200                 bluePrintRuntimeService,
201                 executionServiceInput.payload,
202                 "resource-assignment"
203             )
204
205             assertNotNull(
206                 resourceResolutionService.resolveResources(
207                     resourceAssignmentRuntimeService,
208                     "resource-assignment",
209                     artifactPrefix,
210                     props
211                 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
212             )
213         }
214     }
215
216     @Test
217     fun testResolveResourcesWithResourceIdAndResourceType() {
218         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
219         runBlocking {
220             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
221
222             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
223                 "1234",
224                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
225             )
226
227             val executionServiceInput =
228                 JacksonUtils.readValueFromClassPathFile(
229                     "payload/requests/sample-resourceresolution-request.json",
230                     ExecutionServiceInput::class.java
231                 )!!
232
233             val resourceAssignmentRuntimeService =
234                 ResourceAssignmentUtils.transformToRARuntimeService(
235                     bluePrintRuntimeService,
236                     "testResolveResourcesWithMappingAndTemplate"
237                 )
238
239             val artifactPrefix = "another"
240
241             // Prepare Inputs
242             PayloadUtils.prepareInputsFromWorkflowPayload(
243                 bluePrintRuntimeService,
244                 executionServiceInput.payload,
245                 "resource-assignment"
246             )
247
248             assertNotNull(
249                 resourceResolutionService.resolveResources(
250                     resourceAssignmentRuntimeService,
251                     "resource-assignment",
252                     artifactPrefix,
253                     props
254                 ), "Couldn't Resolve Resources for artifact $artifactPrefix"
255             )
256         }
257     }
258
259     @Test
260     fun testResourceResolutionForDefinition() {
261         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
262             resourceDefinition(name = "port-speed", description = "Port Speed") {
263                 property(type = "string", required = true)
264                 sources {
265                     sourceCapability(id = "sdno", description = "SDNO Source") {
266                         definedProperties {
267                             type(BluePrintConstants.SCRIPT_KOTLIN)
268                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
269                             keyDependencies(arrayListOf("device-id"))
270                         }
271                     }
272                     sourceDb(id = "sdnc", description = "SDNC Controller") {
273                         definedProperties {
274                             endpointSelector("processor-db")
275                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
276                             inputKeyMapping {
277                                 map("device_id", "\$device-id")
278                             }
279                             keyDependencies(arrayListOf("device-id"))
280                         }
281                     }
282                 }
283             }
284             resourceDefinition(name = "device-id", description = "Device Id") {
285                 property(type = "string", required = true) {
286                     sources {
287                         sourceInput(id = "input", description = "Dependency Source") {}
288                     }
289                 }
290             }
291         }
292         runBlocking {
293             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
294             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
295             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
296             every { raRuntimeService.setBluePrintError(any()) } returns Unit
297             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
298             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
299
300             val applicationContext = mockk<ApplicationContext>()
301             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
302             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
303             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
304
305             val sources = arrayListOf<String>("sdno", "sdnc")
306
307             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
308             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
309                 raRuntimeService,
310                 resourceDefinitions, "port-speed", sources
311             )
312             assertNotNull(resolvedResources, "failed to resolve the resources")
313         }
314     }
315 }