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
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Before
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
29 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
30 import org.springframework.dao.EmptyResultDataAccessException
31 import kotlin.test.assertEquals
33 open class ResourceResolutionDBServiceTest {
35 private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
37 private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
39 private val resolutionKey = "resolutionKey"
40 private val resourceId = "1"
41 private val resourceType = "ServiceInstance"
42 private val occurrence = 0
43 private val artifactPrefix = "template"
44 private val blueprintName = "blueprintName"
45 private val blueprintVersion = "1.0.0"
46 private val metadata = hashMapOf<String, String>()
47 private val props = hashMapOf<String, Any>()
48 private val bluePrintContext = mockk<BluePrintContext>()
49 private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
53 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
54 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
56 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
57 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
58 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
59 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
61 every { bluePrintContext.metadata } returns metadata
63 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
67 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
69 val rr1 = ResourceResolution()
70 val rr2 = ResourceResolution()
72 val list = listOf(rr1, rr2)
74 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
75 any(), any(), any(), any(), any()
81 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
82 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
85 assertEquals(2, res.size)
90 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
92 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
93 any(), any(), any(), any(), any()
95 } throws EmptyResultDataAccessException(1)
98 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
99 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
102 assert(res.isEmpty())
107 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
109 val rr1 = ResourceResolution()
110 val rr2 = ResourceResolution()
111 val list = listOf(rr1, rr2)
113 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
114 any(), any(), any(), any(), any(), any()
120 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
121 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
124 assertEquals(2, res.size)
129 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
131 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
132 any(), any(), any(), any(), any(), any()
134 } throws EmptyResultDataAccessException(1)
137 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
138 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
141 assert(res.isEmpty())
146 fun readValueTest() {
147 val rr = ResourceResolution()
149 rr.value = "testValue"
151 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
152 any(), any(), any(), any(), any()
157 resourceResolutionDBService.readValue(
158 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
161 assertEquals(rr.name, res.name)
162 assertEquals(rr.value, res.value)
167 fun readWithResolutionKeyTest() {
168 val rr1 = ResourceResolution()
169 val rr2 = ResourceResolution()
170 val list = listOf(rr1, rr2)
172 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
173 any(), any(), any(), any()
178 resourceResolutionDBService.readWithResolutionKey(
179 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
181 assertEquals(2, res.size)
186 fun readWithResourceIdAndResourceTypeTest() {
187 val rr1 = ResourceResolution()
188 val rr2 = ResourceResolution()
189 val list = listOf(rr1, rr2)
191 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
192 any(), any(), any(), any()
197 resourceResolutionDBService.readWithResourceIdAndResourceType(
198 blueprintName, blueprintVersion, resourceId, resourceType
200 assertEquals(2, res.size)
206 val resourceResolution = ResourceResolution()
207 val resourceAssignment = ResourceAssignment()
208 resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS
209 resourceAssignment.property?.value = "result".asJsonPrimitive()
210 resourceAssignment.dictionarySource = "ddSource"
211 resourceAssignment.dictionaryName = "ddName"
212 resourceAssignment.version = 1
213 resourceAssignment.name = "test"
215 resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
216 } returns resourceResolution
219 resourceResolutionDBService.write(
220 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
223 assertEquals(resourceResolution, res)
228 fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyTest() {
230 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(any(), any(), any(), any())
233 val res = resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
234 blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
235 assertEquals(Unit, res)