1 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
6 import kotlinx.coroutines.runBlocking
7 import org.junit.Before
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
17 class TemplateResolutionServiceTest {
19 private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
21 private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
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>()
38 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
39 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
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
46 every { bluePrintContext.metadata } returns metadata
48 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
52 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
53 val tr = TemplateResolution()
57 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
58 any(), any(), any(), any(), any()
62 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
63 bluePrintRuntimeService, artifactPrefix, resolutionKey
65 assertEquals(tr.result, res)
69 @Test(expected = EmptyResultDataAccessException::class)
70 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
71 val tr = TemplateResolution()
74 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
75 any(), any(), any(), any(), any()
78 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
79 bluePrintRuntimeService, artifactPrefix, resolutionKey
85 fun writeWithResolutionKeyTest() {
86 val tr = TemplateResolution()
88 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
90 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
91 any(), any(), any(), any(), any()
94 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
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)
106 templateResolutionRepository.findFirstNOccurrences(
107 any(), any(), any(), any(), 1
112 templateResolutionService.findFirstNOccurrences(
113 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
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) }
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)
129 templateResolutionRepository.findLastNOccurrences(
130 any(), any(), any(), any(), 1
135 templateResolutionService.findLastNOccurrences(
136 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1
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) }
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)
152 templateResolutionRepository.findOccurrencesWithinRange(
153 any(), any(), any(), any(), 0, 1
158 templateResolutionService.findOccurrencesWithinRange(
159 blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 0, 1
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) }
169 fun writeWithResolutionKeyExistingTest() {
170 val tr = TemplateResolution()
172 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
174 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
175 any(), any(), any(), any(), any()
179 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
180 any(), any(), any(), any(), any()
183 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
185 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
186 eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)
189 assertEquals(tr, res)
194 fun writeWithResourceIdResourceTypeExistingTest() {
195 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
196 val tr = TemplateResolution()
198 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
200 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
201 any(), any(), any(), any(), any(), any()
205 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
206 any(), any(), any(), any(), any(), any()
209 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
211 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
215 eq(blueprintVersion),
220 assertEquals(tr, res)