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