cfd00ac1daf6e20d031aa1316d0134bc7e13bdd7
[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 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_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         } returns list
77         runBlocking {
78
79             val res =
80                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
81                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix)
82
83             assertEquals(2, res.size)
84         }
85     }
86
87     @Test
88     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrenceTestException() {
89         every {
90             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
91                 any(), any(), any(), any(), any())
92         } throws EmptyResultDataAccessException(1)
93         runBlocking {
94             val res =
95                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
96                     bluePrintRuntimeService, resolutionKey, occurrence, artifactPrefix)
97
98             assert(res.isEmpty())
99         }
100     }
101
102     @Test
103     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTest() {
104
105         val rr1 = ResourceResolution()
106         val rr2 = ResourceResolution()
107         val list = listOf(rr1, rr2)
108         every {
109             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
110                 any(), any(), any(), any(), any(), any())
111         } returns list
112         runBlocking {
113
114             val res =
115                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
116                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix)
117
118             assertEquals(2, res.size)
119         }
120     }
121
122     @Test
123     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrenceTestException() {
124         every {
125             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
126                 any(), any(), any(), any(), any(), any())
127         } throws EmptyResultDataAccessException(1)
128         runBlocking {
129             val res =
130                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
131                     bluePrintRuntimeService, resourceId, resourceType, occurrence, artifactPrefix)
132
133             assert(res.isEmpty())
134         }
135     }
136
137     @Test
138     fun readValueTest() {
139         val rr = ResourceResolution()
140         rr.name = "bob"
141         rr.value = "testValue"
142         every {
143             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
144                 any(), any(), any(), any(), any())
145         } returns rr
146         runBlocking {
147             val res =
148                 resourceResolutionDBService.readValue(
149                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob")
150
151             assertEquals(rr.name, res.name)
152             assertEquals(rr.value, res.value)
153         }
154     }
155
156     @Test
157     fun readWithResolutionKeyTest() {
158         val rr1 = ResourceResolution()
159         val rr2 = ResourceResolution()
160         val list = listOf(rr1, rr2)
161         every {
162             resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
163                 any(), any(), any(), any())
164         } returns list
165         runBlocking {
166             val res =
167                 resourceResolutionDBService.readWithResolutionKey(
168                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
169             assertEquals(2, res.size)
170         }
171     }
172
173     @Test
174     fun readWithResourceIdAndResourceTypeTest() {
175         val rr1 = ResourceResolution()
176         val rr2 = ResourceResolution()
177         val list = listOf(rr1, rr2)
178         every {
179             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
180                 any(), any(), any(), any())
181         } returns list
182         runBlocking {
183             val res =
184                 resourceResolutionDBService.readWithResourceIdAndResourceType(
185                     blueprintName, blueprintVersion, resourceId, resourceType)
186             assertEquals(2, res.size)
187         }
188     }
189
190     @Test
191     fun writeTest() {
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"
200         every {
201             resourceResolutionRepository.saveAndFlush(any<ResourceResolution>())
202         } returns resourceResolution
203         runBlocking {
204             val res =
205                 resourceResolutionDBService.write(
206                     props, bluePrintRuntimeService, artifactPrefix, resourceAssignment)
207
208             assertEquals(resourceResolution, res)
209         }
210     }
211 }