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
34 open class ResourceResolutionDBServiceTest {
36 private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
38 private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
40 private val resolutionKey = "resolutionKey"
41 private val resourceId = "1"
42 private val resourceType = "ServiceInstance"
43 private val occurrence = 0
44 private val artifactPrefix = "template"
45 private val blueprintName = "blueprintName"
46 private val blueprintVersion = "1.0.0"
47 private val metadata = hashMapOf<String, String>()
48 private val props = hashMapOf<String, Any>()
49 private val bluePrintContext = mockk<BluePrintContext>()
50 private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
54 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
55 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
57 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
58 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
59 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
60 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
62 every { bluePrintContext.metadata } returns metadata
64 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
68 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
70 val rr1 = ResourceResolution()
71 val rr2 = ResourceResolution()
73 val list = listOf(rr1, rr2)
75 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
76 any(), any(), any(), any(), any()
82 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
83 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
86 assertEquals(2, res.size)
91 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
93 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
94 any(), any(), any(), any(), any()
96 } throws EmptyResultDataAccessException(1)
99 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
100 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
103 assert(res.isEmpty())
108 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
110 val rr1 = ResourceResolution()
111 val rr2 = ResourceResolution()
112 val list = listOf(rr1, rr2)
114 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
115 any(), any(), any(), any(), any(), any()
121 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
122 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
125 assertEquals(2, res.size)
130 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
132 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
133 any(), any(), any(), any(), any(), any()
135 } throws EmptyResultDataAccessException(1)
138 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
139 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
142 assert(res.isEmpty())
147 fun readValueTest() {
148 val rr = ResourceResolution()
150 rr.value = "testValue"
152 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
153 any(), any(), any(), any(), any()
158 resourceResolutionDBService.readValue(
159 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
162 assertEquals(rr.name, res.name)
163 assertEquals(rr.value, res.value)
168 fun readWithResolutionKeyTest() {
169 val rr1 = ResourceResolution()
170 val rr2 = ResourceResolution()
171 val list = listOf(rr1, rr2)
173 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
174 any(), any(), any(), any()
179 resourceResolutionDBService.readWithResolutionKey(
180 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
182 assertEquals(2, res.size)
187 fun readWithResourceIdAndResourceTypeTest() {
188 val rr1 = ResourceResolution()
189 val rr2 = ResourceResolution()
190 val list = listOf(rr1, rr2)
192 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
193 any(), any(), any(), any()
198 resourceResolutionDBService.readWithResourceIdAndResourceType(
199 blueprintName, blueprintVersion, resourceId, resourceType
201 assertEquals(2, res.size)
207 val resourceResolution = ResourceResolution()
208 val resourceAssignment = ResourceAssignment()
209 resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS
210 resourceAssignment.property?.value = "result".asJsonPrimitive()
211 resourceAssignment.dictionarySource = "ddSource"
212 resourceAssignment.dictionaryName = "ddName"
213 resourceAssignment.version = 1
214 resourceAssignment.name = "test"
216 resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
217 } returns resourceResolution
220 resourceResolutionDBService.write(
221 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
224 assertEquals(resourceResolution, res)
229 fun writeWithNullValue() {
230 val slot = slot<ResourceResolution>()
231 val resourceAssignment = ResourceAssignment()
232 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
233 resourceAssignment.dictionarySource = "ddSource"
234 resourceAssignment.dictionaryName = "ddName"
235 resourceAssignment.version = 1
236 resourceAssignment.name = "test"
238 resourceResolutionRepository.saveAndFlush(capture(slot))
239 } returns ResourceResolution()
241 resourceResolutionDBService.write(
242 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
245 val res = slot.captured
247 assertEquals("", res.value)
252 fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyTest() {
254 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(any(), any(), any(), any())
257 val res = resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
258 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
260 assertEquals(Unit, res)