Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / ResourceResolutionDBServiceTest.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
18
19 import io.mockk.every
20 import io.mockk.mockk
21 import io.mockk.slot
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Before
24 import org.junit.Test
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
34 open class ResourceResolutionDBServiceTest {
35
36     private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
37
38     private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
39
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>()
51
52     @Before
53     fun setup() {
54         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
55         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
56
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
61
62         every { bluePrintContext.metadata } returns metadata
63
64         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
65     }
66
67     @Test
68     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
69
70         val rr1 = ResourceResolution()
71         val rr2 = ResourceResolution()
72
73         val list = listOf(rr1, rr2)
74         every {
75             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
76                 any(), any(), any(), any(), any()
77             )
78         } returns list
79         runBlocking {
80
81             val res =
82                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
83                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
84                 )
85
86             assertEquals(2, res.size)
87         }
88     }
89
90     @Test
91     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
92         every {
93             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
94                 any(), any(), any(), any(), any()
95             )
96         } throws EmptyResultDataAccessException(1)
97         runBlocking {
98             val res =
99                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
100                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
101                 )
102
103             assert(res.isEmpty())
104         }
105     }
106
107     @Test
108     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
109
110         val rr1 = ResourceResolution()
111         val rr2 = ResourceResolution()
112         val list = listOf(rr1, rr2)
113         every {
114             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
115                 any(), any(), any(), any(), any(), any()
116             )
117         } returns list
118         runBlocking {
119
120             val res =
121                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
122                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
123                 )
124
125             assertEquals(2, res.size)
126         }
127     }
128
129     @Test
130     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
131         every {
132             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
133                 any(), any(), any(), any(), any(), any()
134             )
135         } throws EmptyResultDataAccessException(1)
136         runBlocking {
137             val res =
138                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
139                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
140                 )
141
142             assert(res.isEmpty())
143         }
144     }
145
146     @Test
147     fun readValueTest() {
148         val rr = ResourceResolution()
149         rr.name = "bob"
150         rr.value = "testValue"
151         every {
152             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
153                 any(), any(), any(), any(), any()
154             )
155         } returns rr
156         runBlocking {
157             val res =
158                 resourceResolutionDBService.readValue(
159                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
160                 )
161
162             assertEquals(rr.name, res.name)
163             assertEquals(rr.value, res.value)
164         }
165     }
166
167     @Test
168     fun readWithResolutionKeyTest() {
169         val rr1 = ResourceResolution()
170         val rr2 = ResourceResolution()
171         val list = listOf(rr1, rr2)
172         every {
173             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
174                 any(), any(), any(), any()
175             )
176         } returns list
177         runBlocking {
178             val res =
179                 resourceResolutionDBService.readWithResolutionKey(
180                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey
181                 )
182             assertEquals(2, res.size)
183         }
184     }
185
186     @Test
187     fun readWithResourceIdAndResourceTypeTest() {
188         val rr1 = ResourceResolution()
189         val rr2 = ResourceResolution()
190         val list = listOf(rr1, rr2)
191         every {
192             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
193                 any(), any(), any(), any()
194             )
195         } returns list
196         runBlocking {
197             val res =
198                 resourceResolutionDBService.readWithResourceIdAndResourceType(
199                     blueprintName, blueprintVersion, resourceId, resourceType
200                 )
201             assertEquals(2, res.size)
202         }
203     }
204
205     @Test
206     fun writeTest() {
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"
215         every {
216             resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
217         } returns resourceResolution
218         runBlocking {
219             val res =
220                 resourceResolutionDBService.write(
221                     props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
222                 )
223
224             assertEquals(resourceResolution, res)
225         }
226     }
227
228     @Test
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"
237         every {
238             resourceResolutionRepository.saveAndFlush(capture(slot))
239         } returns ResourceResolution()
240         runBlocking {
241             resourceResolutionDBService.write(
242                 props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
243             )
244
245             val res = slot.captured
246
247             assertEquals("", res.value)
248         }
249     }
250
251     @Test
252     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyTest() {
253         every {
254             resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(any(), any(), any(), any())
255         } returns Unit
256         runBlocking {
257             val res = resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
258                 blueprintName, blueprintVersion, artifactPrefix, resolutionKey
259             )
260             assertEquals(Unit, res)
261         }
262     }
263 }