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_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())
80 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
81 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix)
83 assertEquals(2, res.size)
88 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
90 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
91 any(), any(), any(), any(), any())
92 } throws EmptyResultDataAccessException(1)
95 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
96 bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix)
103 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
105 val rr1 = ResourceResolution()
106 val rr2 = ResourceResolution()
107 val list = listOf(rr1, rr2)
109 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
110 any(), any(), any(), any(), any(), any())
115 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
116 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix)
118 assertEquals(2, res.size)
123 fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
125 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
126 any(), any(), any(), any(), any(), any())
127 } throws EmptyResultDataAccessException(1)
130 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
131 bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix)
133 assert(res.isEmpty())
138 fun readValueTest() {
139 val rr = ResourceResolution()
141 rr.value = "testValue"
143 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
144 any(), any(), any(), any(), any())
148 resourceResolutionDBService.readValue(
149 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob")
151 assertEquals(rr.name, res.name)
152 assertEquals(rr.value, res.value)
157 fun readWithResolutionKeyTest() {
158 val rr1 = ResourceResolution()
159 val rr2 = ResourceResolution()
160 val list = listOf(rr1, rr2)
162 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
163 any(), any(), any(), any())
167 resourceResolutionDBService.readWithResolutionKey(
168 blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
169 assertEquals(2, res.size)
174 fun readWithResourceIdAndResourceTypeTest() {
175 val rr1 = ResourceResolution()
176 val rr2 = ResourceResolution()
177 val list = listOf(rr1, rr2)
179 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
180 any(), any(), any(), any())
184 resourceResolutionDBService.readWithResourceIdAndResourceType(
185 blueprintName, blueprintVersion, resourceId, resourceType)
186 assertEquals(2, res.size)
192 val resourceResolution = ResourceResolution()
193 val resourceAssignment = ResourceAssignment()
194 resourceAssignment.property?.status = BluePrintConstants.STATUS_SUCCESS
195 resourceAssignment.property?.value = "result".asJsonPrimitive()
196 resourceAssignment.dictionarySource = "ddSource"
197 resourceAssignment.dictionaryName = "ddName"
198 resourceAssignment.version = 1
199 resourceAssignment.name = "test"
201 resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
202 } returns resourceResolution
205 resourceResolutionDBService.write(
206 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment)
208 assertEquals(resourceResolution, res)