Optimize spring data JPA UT.
[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.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             resourceResolutionService.resolveResources(
206                 resourceAssignmentRuntimeService,
207                 "resource-assignment",
208                 artifactPrefix,
209                 props
210             )
211         }
212     }
213
214     @Test
215     fun testResolveResourcesWithResourceIdAndResourceType() {
216
217         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
218
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             resourceResolutionService.resolveResources(
249                 resourceAssignmentRuntimeService,
250                 "resource-assignment",
251                 artifactPrefix,
252                 props
253             )
254         }
255     }
256
257     @Test
258     fun testResourceResolutionForDefinition() {
259         val resourceDefinitions = BluePrintTypes.resourceDefinitions {
260             resourceDefinition(name = "port-speed", description = "Port Speed") {
261                 property(type = "string", required = true)
262                 sources {
263                     sourceCapability(id = "sdno", description = "SDNO Source") {
264                         definedProperties {
265                             type(BluePrintConstants.SCRIPT_KOTLIN)
266                             scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
267                             keyDependencies(arrayListOf("device-id"))
268                         }
269                     }
270                     sourceDb(id = "sdnc", description = "SDNC Controller") {
271                         definedProperties {
272                             endpointSelector("processor-db")
273                             query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
274                             inputKeyMapping {
275                                 map("device_id", "\$device-id")
276                             }
277                             keyDependencies(arrayListOf("device-id"))
278                         }
279                     }
280                 }
281             }
282             resourceDefinition(name = "device-id", description = "Device Id") {
283                 property(type = "string", required = true) {
284                     sources {
285                         sourceInput(id = "input", description = "Dependency Source") {}
286                     }
287                 }
288             }
289         }
290         runBlocking {
291             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
292             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
293             every { raRuntimeService.getBluePrintError() } returns BluePrintError()
294             every { raRuntimeService.setBluePrintError(any()) } returns Unit
295             every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
296             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
297
298             val applicationContext = mockk<ApplicationContext>()
299             every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
300             every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
301             every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
302
303             val sources = arrayListOf<String>("sdno", "sdnc")
304
305             val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
306             val resolvedResources = resourceResolutionService.resolveResourceDefinition(
307                 raRuntimeService,
308                 resourceDefinitions, "port-speed", sources
309             )
310             assertNotNull(resolvedResources, "failed to resolve the resources")
311         }
312     }
313 }