Change SQL query syntax for delete resource/template
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / ResourceResolutionDBServiceTest.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
18
19 import io.mockk.every
20 import io.mockk.mockk
21 import io.mockk.slot
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Before
24 import org.junit.Test
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
31 import org.springframework.dao.EmptyResultDataAccessException
32 import kotlin.test.assertEquals
33 import kotlin.test.assertNotEquals
34
35 open class ResourceResolutionDBServiceTest {
36
37     private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
38
39     private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
40
41     private val resolutionKey = "resolutionKey"
42     private val resourceId = "1"
43     private val resourceType = "ServiceInstance"
44     private val occurrence = 0
45     private val artifactPrefix = "template"
46     private val blueprintName = "blueprintName"
47     private val blueprintVersion = "1.0.0"
48     private val metadata = hashMapOf<String, String>()
49     private val props = hashMapOf<String, Any>()
50     private val bluePrintContext = mockk<BluePrintContext>()
51     private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
52
53     @Before
54     fun setup() {
55         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
56         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
57
58         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
59         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
60         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
61         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
62
63         every { bluePrintContext.metadata } returns metadata
64
65         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
66     }
67
68     @Test
69     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
70
71         val rr1 = ResourceResolution()
72         val rr2 = ResourceResolution()
73
74         val list = listOf(rr1, rr2)
75         every {
76             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
77                 any(), any(), any(), any(), any()
78             )
79         } returns list
80         runBlocking {
81
82             val res =
83                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
84                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
85                 )
86
87             assertEquals(2, res.size)
88         }
89     }
90
91     @Test
92     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
93         every {
94             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
95                 any(), any(), any(), any(), any()
96             )
97         } throws EmptyResultDataAccessException(1)
98         runBlocking {
99             val res =
100                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
101                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
102                 )
103
104             assert(res.isEmpty())
105         }
106     }
107
108     @Test
109     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
110
111         val rr1 = ResourceResolution()
112         val rr2 = ResourceResolution()
113         val list = listOf(rr1, rr2)
114         every {
115             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
116                 any(), any(), any(), any(), any(), any()
117             )
118         } returns list
119         runBlocking {
120
121             val res =
122                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
123                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
124                 )
125
126             assertEquals(2, res.size)
127         }
128     }
129
130     @Test
131     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
132         every {
133             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
134                 any(), any(), any(), any(), any(), any()
135             )
136         } throws EmptyResultDataAccessException(1)
137         runBlocking {
138             val res =
139                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
140                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
141                 )
142
143             assert(res.isEmpty())
144         }
145     }
146
147     @Test
148     fun readValueTest() {
149         val rr = ResourceResolution()
150         rr.name = "bob"
151         rr.value = "testValue"
152         every {
153             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
154                 any(), any(), any(), any(), any()
155             )
156         } returns rr
157         runBlocking {
158             val res =
159                 resourceResolutionDBService.readValue(
160                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
161                 )
162             assertNotEquals(res, null, "resource resolution failed")
163             if (res != null) {
164                 assertEquals(rr.name, res.name)
165                 assertEquals(rr.value, res.value)
166             }
167         }
168     }
169
170     @Test
171     fun readWithResolutionKeyTest() {
172         val rr1 = ResourceResolution()
173         val rr2 = ResourceResolution()
174         val list = listOf(rr1, rr2)
175         every {
176             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
177                 any(), any(), any(), any()
178             )
179         } returns list
180         runBlocking {
181             val res =
182                 resourceResolutionDBService.readWithResolutionKey(
183                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey
184                 )
185             assertEquals(2, res.size)
186         }
187     }
188
189     @Test
190     fun findFirstNOccurrencesTest() {
191         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
192         val rr1 = ResourceResolution()
193         val rr2 = ResourceResolution()
194         val list = listOf(rr1, rr2)
195         every {
196             resourceResolutionRepository.findFirstNOccurrences(
197                 any(), any(), any(), any(), 1
198             )
199         } returns list
200         runBlocking {
201             val res =
202                 resourceResolutionDBService.findFirstNOccurrences(
203                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
204                 )
205             assertEquals(false, res.isEmpty(), "find first N occurrences test failed")
206             assertEquals(1, res.size)
207             assertNotEquals(null, res[0])
208             res[0]?.let { assertEquals(2, it.size) }
209         }
210     }
211
212     @Test
213     fun findLastNOccurrencesTest() {
214         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
215         val rr1 = ResourceResolution()
216         val rr2 = ResourceResolution()
217         val list = listOf(rr1, rr2)
218         every {
219             resourceResolutionRepository.findLastNOccurrences(
220                 any(), any(), any(), any(), 1
221             )
222         } returns list
223         runBlocking {
224             val res =
225                 resourceResolutionDBService.findLastNOccurrences(
226                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
227                 )
228             assertEquals(false, res.isEmpty(), "find last N occurrences test failed")
229             assertEquals(1, res.size)
230             assertNotEquals(null, res[0])
231             res[0]?.let { assertEquals(2, it.size) }
232         }
233     }
234
235     @Test
236     fun findOccurrencesWithinRangeTest() {
237         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
238         val rr1 = ResourceResolution()
239         val rr2 = ResourceResolution()
240         val list = listOf(rr1, rr2)
241         every {
242             resourceResolutionRepository.findOccurrencesWithinRange(
243                 any(), any(), any(), any(), 0, 1
244             )
245         } returns list
246         runBlocking {
247             val res =
248                 resourceResolutionDBService.findOccurrencesWithinRange(
249                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 0, 1
250                 )
251             assertEquals(false, res.isEmpty(), "find occurrences within a range test failed")
252             assertEquals(1, res.size)
253             assertNotEquals(null, res[0])
254             res[0]?.let { assertEquals(2, it.size) }
255         }
256     }
257
258     @Test
259     fun readWithResourceIdAndResourceTypeTest() {
260         val rr1 = ResourceResolution()
261         val rr2 = ResourceResolution()
262         val list = listOf(rr1, rr2)
263         every {
264             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
265                 any(), any(), any(), any()
266             )
267         } returns list
268         runBlocking {
269             val res =
270                 resourceResolutionDBService.readWithResourceIdAndResourceType(
271                     blueprintName, blueprintVersion, resourceId, resourceType
272                 )
273             assertEquals(2, res.size)
274         }
275     }
276
277     @Test
278     fun writeTest() {
279         val resourceResolution = ResourceResolution()
280         val resourceAssignment = ResourceAssignment()
281         resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS
282         resourceAssignment.property?.value = "result".asJsonPrimitive()
283         resourceAssignment.dictionarySource = "ddSource"
284         resourceAssignment.dictionaryName = "ddName"
285         resourceAssignment.version = 1
286         resourceAssignment.name = "test"
287         every {
288             resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
289         } returns resourceResolution
290         runBlocking {
291             val res =
292                 resourceResolutionDBService.write(
293                     props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
294                 )
295
296             assertEquals(resourceResolution, res)
297         }
298     }
299
300     @Test
301     fun writeWithNullValue() {
302         val slot = slot<ResourceResolution>()
303         val resourceAssignment = ResourceAssignment()
304         resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
305         resourceAssignment.dictionarySource = "ddSource"
306         resourceAssignment.dictionaryName = "ddName"
307         resourceAssignment.version = 1
308         resourceAssignment.name = "test"
309         every {
310             resourceResolutionRepository.saveAndFlush(capture(slot))
311         } returns ResourceResolution()
312         runBlocking {
313             resourceResolutionDBService.write(
314                 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
315             )
316
317             val res = slot.captured
318
319             assertEquals("", res.value)
320         }
321     }
322
323     @Test
324     fun deleteResourcesResolutionKeyAll() {
325         every {
326             resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
327         } returns 3
328         runBlocking {
329             val res = resourceResolutionDBService.deleteResources(
330                 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, null
331             )
332             assertEquals(3, res)
333         }
334     }
335
336     @Test
337     fun deleteResourcesResolutionKeyLastN() {
338         every {
339             resourceResolutionRepository.deleteLastNOccurrences(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1)
340         } returns 4
341         runBlocking {
342             val res = resourceResolutionDBService.deleteResources(
343                 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
344             )
345             assertEquals(4, res)
346         }
347     }
348
349     @Test
350     fun deleteResourcesResourceIdAndTypeAll() {
351         every {
352             resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId)
353         } returns 3
354         runBlocking {
355             val res = resourceResolutionDBService.deleteResources(
356                 blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, null
357             )
358             assertEquals(3, res)
359         }
360     }
361
362     @Test
363     fun deleteResourcesResourceIdAndTypeLastN() {
364         every {
365             resourceResolutionRepository.deleteLastNOccurrences(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2)
366         } returns 6
367         runBlocking {
368             val res = resourceResolutionDBService.deleteResources(
369                 blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2
370             )
371             assertEquals(6, res)
372         }
373     }
374 }