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 readWithResourceIdAndResourceTypeTest() {
191 val rr1 = ResourceResolution()
192 val rr2 = ResourceResolution()
193 val list = listOf(rr1, rr2)
195 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
196 any(), any(), any(), any()
201 resourceResolutionDBService.readWithResourceIdAndResourceType(
202 blueprintName, blueprintVersion, resourceId, resourceType
204 assertEquals(2, res.size)
210 val resourceResolution = ResourceResolution()
211 val resourceAssignment = ResourceAssignment()
212 resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS
213 resourceAssignment.property?.value = "result".asJsonPrimitive()
214 resourceAssignment.dictionarySource = "ddSource"
215 resourceAssignment.dictionaryName = "ddName"
216 resourceAssignment.version = 1
217 resourceAssignment.name = "test"
219 resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
220 } returns resourceResolution
223 resourceResolutionDBService.write(
224 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
227 assertEquals(resourceResolution, res)
232 fun writeWithNullValue() {
233 val slot = slot<ResourceResolution>()
234 val resourceAssignment = ResourceAssignment()
235 resourceAssignment.status = BluePrintConstants.STATUS_SUCCESS
236 resourceAssignment.dictionarySource = "ddSource"
237 resourceAssignment.dictionaryName = "ddName"
238 resourceAssignment.version = 1
239 resourceAssignment.name = "test"
241 resourceResolutionRepository.saveAndFlush(capture(slot))
242 } returns ResourceResolution()
244 resourceResolutionDBService.write(
245 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
248 val res = slot.captured
250 assertEquals("", res.value)
255 fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyTest() {
257 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(any(), any(), any(), any())
260 val res = resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
261 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
263 assertEquals(Unit, res)