2 * Copyright (C) 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Before
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
35 open class ResourceResolutionDBServiceTest {
37 private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
39 private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
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>()
55 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
56 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
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
63 every { bluePrintContext.metadata } returns metadata
65 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
69 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
71 val rr1 = ResourceResolution()
72 val rr2 = ResourceResolution()
74 val list = listOf(rr1, rr2)
76 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
77 any(), any(), any(), any(), any()
83 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
84 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
87 assertEquals(2, res.size)
92 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
94 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
95 any(), any(), any(), any(), any()
97 } throws EmptyResultDataAccessException(1)
100 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
101 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
104 assert(res.isEmpty())
109 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
111 val rr1 = ResourceResolution()
112 val rr2 = ResourceResolution()
113 val list = listOf(rr1, rr2)
115 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
116 any(), any(), any(), any(), any(), any()
122 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
123 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
126 assertEquals(2, res.size)
131 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
133 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
134 any(), any(), any(), any(), any(), any()
136 } throws EmptyResultDataAccessException(1)
139 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
140 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
143 assert(res.isEmpty())
148 fun readValueTest() {
149 val rr = ResourceResolution()
151 rr.value = "testValue"
153 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
154 any(), any(), any(), any(), any()
159 resourceResolutionDBService.readValue(
160 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
162 assertNotEquals(res, null, "resource resolution failed")
164 assertEquals(rr.name, res.name)
165 assertEquals(rr.value, res.value)
171 fun readWithResolutionKeyTest() {
172 val rr1 = ResourceResolution()
173 val rr2 = ResourceResolution()
174 val list = listOf(rr1, rr2)
176 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
177 any(), any(), any(), any()
182 resourceResolutionDBService.readWithResolutionKey(
183 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
185 assertEquals(2, res.size)
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)
196 resourceResolutionRepository.findFirstNOccurrences(
197 any(), any(), any(), any(), 1
202 resourceResolutionDBService.findFirstNOccurrences(
203 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
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) }
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)
219 resourceResolutionRepository.findLastNOccurrences(
220 any(), any(), any(), any(), 1
225 resourceResolutionDBService.findLastNOccurrences(
226 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
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) }
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)
242 resourceResolutionRepository.findOccurrencesWithinRange(
243 any(), any(), any(), any(), 0, 1
248 resourceResolutionDBService.findOccurrencesWithinRange(
249 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 0, 1
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) }
259 fun readWithResourceIdAndResourceTypeTest() {
260 val rr1 = ResourceResolution()
261 val rr2 = ResourceResolution()
262 val list = listOf(rr1, rr2)
264 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
265 any(), any(), any(), any()
270 resourceResolutionDBService.readWithResourceIdAndResourceType(
271 blueprintName, blueprintVersion, resourceId, resourceType
273 assertEquals(2, res.size)
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"
288 resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
289 } returns resourceResolution
292 resourceResolutionDBService.write(
293 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
296 assertEquals(resourceResolution, res)
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"
310 resourceResolutionRepository.saveAndFlush(capture(slot))
311 } returns ResourceResolution()
313 resourceResolutionDBService.write(
314 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
317 val res = slot.captured
319 assertEquals("", res.value)
324 fun deleteResourcesResolutionKeyAll() {
326 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
329 val res = resourceResolutionDBService.deleteResources(
330 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, null
337 fun deleteResourcesResolutionKeyLastN() {
339 resourceResolutionRepository.deleteLastNOccurrences(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1)
342 val res = resourceResolutionDBService.deleteResources(
343 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
350 fun deleteResourcesResourceIdAndTypeAll() {
352 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId)
355 val res = resourceResolutionDBService.deleteResources(
356 blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, null
363 fun deleteResourcesResourceIdAndTypeLastN() {
365 resourceResolutionRepository.deleteLastNOccurrences(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2)
368 val res = resourceResolutionDBService.deleteResources(
369 blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2