Formatting Code base with ktlint
[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 kotlinx.coroutines.runBlocking
22 import org.junit.Before
23 import org.junit.Test
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
32
33 open class ResourceResolutionDBServiceTest {
34
35     private val resourceResolutionRepository = mockk<ResourceResolutionRepository>()
36
37     private val resourceResolutionDBService = ResourceResolutionDBService(resourceResolutionRepository)
38
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>()
50
51     @Before
52     fun setup() {
53         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
54         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
55
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
60
61         every { bluePrintContext.metadata } returns metadata
62
63         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
64     }
65
66     @Test
67     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTest() {
68
69         val rr1 = ResourceResolution()
70         val rr2 = ResourceResolution()
71
72         val list = listOf(rr1, rr2)
73         every {
74             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
75                 any(), any(), any(), any(), any()
76             )
77         } returns list
78         runBlocking {
79
80             val res =
81                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
82                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
83                 )
84
85             assertEquals(2, res.size)
86         }
87     }
88
89     @Test
90     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
91         every {
92             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
93                 any(), any(), any(), any(), any()
94             )
95         } throws EmptyResultDataAccessException(1)
96         runBlocking {
97             val res =
98                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
99                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix
100                 )
101
102             assert(res.isEmpty())
103         }
104     }
105
106     @Test
107     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
108
109         val rr1 = ResourceResolution()
110         val rr2 = ResourceResolution()
111         val list = listOf(rr1, rr2)
112         every {
113             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
114                 any(), any(), any(), any(), any(), any()
115             )
116         } returns list
117         runBlocking {
118
119             val res =
120                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
121                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
122                 )
123
124             assertEquals(2, res.size)
125         }
126     }
127
128     @Test
129     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
130         every {
131             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
132                 any(), any(), any(), any(), any(), any()
133             )
134         } throws EmptyResultDataAccessException(1)
135         runBlocking {
136             val res =
137                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
138                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix
139                 )
140
141             assert(res.isEmpty())
142         }
143     }
144
145     @Test
146     fun readValueTest() {
147         val rr = ResourceResolution()
148         rr.name = "bob"
149         rr.value = "testValue"
150         every {
151             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
152                 any(), any(), any(), any(), any()
153             )
154         } returns rr
155         runBlocking {
156             val res =
157                 resourceResolutionDBService.readValue(
158                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
159                 )
160
161             assertEquals(rr.name, res.name)
162             assertEquals(rr.value, res.value)
163         }
164     }
165
166     @Test
167     fun readWithResolutionKeyTest() {
168         val rr1 = ResourceResolution()
169         val rr2 = ResourceResolution()
170         val list = listOf(rr1, rr2)
171         every {
172             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
173                 any(), any(), any(), any()
174             )
175         } returns list
176         runBlocking {
177             val res =
178                 resourceResolutionDBService.readWithResolutionKey(
179                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey
180                 )
181             assertEquals(2, res.size)
182         }
183     }
184
185     @Test
186     fun readWithResourceIdAndResourceTypeTest() {
187         val rr1 = ResourceResolution()
188         val rr2 = ResourceResolution()
189         val list = listOf(rr1, rr2)
190         every {
191             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
192                 any(), any(), any(), any()
193             )
194         } returns list
195         runBlocking {
196             val res =
197                 resourceResolutionDBService.readWithResourceIdAndResourceType(
198                     blueprintName, blueprintVersion, resourceId, resourceType
199                 )
200             assertEquals(2, res.size)
201         }
202     }
203
204     @Test
205     fun writeTest() {
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"
214         every {
215             resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
216         } returns resourceResolution
217         runBlocking {
218             val res =
219                 resourceResolutionDBService.write(
220                     props, bluePrintRuntimeService, artifactPrefix, resourceAssignment
221                 )
222
223             assertEquals(resourceResolution, res)
224         }
225     }
226 }