Merge "CCSDK-3684 ComponentRemotePYthonExecutor eliminate recursive call"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / TemplateResolutionServiceTest.kt
1 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
2
3 import io.mockk.every
4 import io.mockk.mockk
5 import io.mockk.verify
6 import kotlinx.coroutines.runBlocking
7 import org.junit.Before
8 import org.junit.Test
9 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
10 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
11 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
12 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
13 import org.springframework.dao.EmptyResultDataAccessException
14 import kotlin.test.assertEquals
15 import kotlin.test.assertNotEquals
16
17 class TemplateResolutionServiceTest {
18
19     private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
20
21     private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
22
23     private val resolutionKey = "resolutionKey"
24     private val resourceId = "1"
25     private val resourceType = "ServiceInstance"
26     private val occurrence = 0
27     private val artifactPrefix = "template"
28     private val blueprintName = "blueprintName"
29     private val blueprintVersion = "1.0.0"
30     private val result = "result"
31     private val metadata = hashMapOf<String, String>()
32     private val props = hashMapOf<String, Any>()
33     private val bluePrintContext = mockk<BluePrintContext>()
34     private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
35
36     @Before
37     fun setup() {
38         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
39         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
40
41         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
42         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
43         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
44         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
45
46         every { bluePrintContext.metadata } returns metadata
47
48         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
49     }
50
51     @Test
52     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
53         val tr = TemplateResolution()
54         tr.result = "res"
55         runBlocking {
56             every {
57                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
58                     any(), any(), any(), any(), any()
59                 )
60             } returns tr
61             val res =
62                 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
63                     bluePrintRuntimeService, artifactPrefix, resolutionKey
64                 )
65             assertEquals(tr.result, res)
66         }
67     }
68
69     @Test(expected = EmptyResultDataAccessException::class)
70     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
71         val tr = TemplateResolution()
72         runBlocking {
73             every {
74                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
75                     any(), any(), any(), any(), any()
76                 )
77             } returns tr
78             templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
79                 bluePrintRuntimeService, artifactPrefix, resolutionKey
80             )
81         }
82     }
83
84     @Test
85     fun writeWithResolutionKeyTest() {
86         val tr = TemplateResolution()
87         runBlocking {
88             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
89             every {
90                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
91                     any(), any(), any(), any(), any()
92                 )
93             } returns null
94             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
95             assertEquals(tr, res)
96         }
97     }
98
99     @Test
100     fun findFirstNOccurrencesTest() {
101         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
102         val tr1 = TemplateResolution()
103         val tr2 = TemplateResolution()
104         val list = listOf(tr1, tr2)
105         every {
106             templateResolutionRepository.findFirstNOccurrences(
107                 any(), any(), any(), any(), 1
108             )
109         } returns list
110         runBlocking {
111             val res =
112                 templateResolutionService.findFirstNOccurrences(
113                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
114                 )
115             assertEquals(false, res.isEmpty(), "find first N occurrences test failed")
116             assertEquals(1, res.size)
117             assertNotEquals(null, res[1])
118             res[1]?.let { assertEquals(2, it.size) }
119         }
120     }
121
122     @Test
123     fun findLastNOccurrencesTest() {
124         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
125         val tr1 = TemplateResolution()
126         val tr2 = TemplateResolution()
127         val list = listOf(tr1, tr2)
128         every {
129             templateResolutionRepository.findLastNOccurrences(
130                 any(), any(), any(), any(), 1
131             )
132         } returns list
133         runBlocking {
134             val res =
135                 templateResolutionService.findLastNOccurrences(
136                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
137                 )
138             assertEquals(false, res.isEmpty(), "find last N occurrences test failed")
139             assertEquals(1, res.size)
140             assertNotEquals(null, res[1])
141             res[1]?.let { assertEquals(2, it.size) }
142         }
143     }
144
145     @Test
146     fun findOccurrencesWithinRangeTest() {
147         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
148         val tr1 = TemplateResolution()
149         val tr2 = TemplateResolution()
150         val list = listOf(tr1, tr2)
151         every {
152             templateResolutionRepository.findOccurrencesWithinRange(
153                 any(), any(), any(), any(), 0, 1
154             )
155         } returns list
156         runBlocking {
157             val res =
158                 templateResolutionService.findOccurrencesWithinRange(
159                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 0, 1
160                 )
161             assertEquals(false, res.isEmpty(), "find occurrences within a range test failed")
162             assertEquals(1, res.size)
163             assertNotEquals(null, res[1])
164             res[1]?.let { assertEquals(2, it.size) }
165         }
166     }
167
168     @Test
169     fun writeWithResolutionKeyExistingTest() {
170         val tr = TemplateResolution()
171         runBlocking {
172             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
173             every {
174                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
175                     any(), any(), any(), any(), any()
176                 )
177             } returns tr
178             every {
179                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
180                     any(), any(), any(), any(), any()
181                 )
182             } returns Unit
183             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
184             verify {
185                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
186                     eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)
187                 )
188             }
189             assertEquals(tr, res)
190         }
191     }
192
193     @Test
194     fun writeWithResourceIdResourceTypeExistingTest() {
195         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
196         val tr = TemplateResolution()
197         runBlocking {
198             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
199             every {
200                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
201                     any(), any(), any(), any(), any(), any()
202                 )
203             } returns tr
204             every {
205                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
206                     any(), any(), any(), any(), any(), any()
207                 )
208             } returns Unit
209             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
210             verify {
211                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
212                     eq(resourceId),
213                     eq(resourceType),
214                     eq(blueprintName),
215                     eq(blueprintVersion),
216                     eq(artifactPrefix),
217                     eq(occurrence)
218                 )
219             }
220             assertEquals(tr, res)
221         }
222     }
223 }