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
16 class TemplateResolutionServiceTest {
18 private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
20 private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
22 private val resolutionKey = "resolutionKey"
23 private val resourceId = "1"
24 private val resourceType = "ServiceInstance"
25 private val occurrence = 0
26 private val artifactPrefix = "template"
27 private val blueprintName = "blueprintName"
28 private val blueprintVersion = "1.0.0"
29 private val result = "result"
30 private val metadata = hashMapOf<String, String>()
31 private val props = hashMapOf<String, Any>()
32 private val bluePrintContext = mockk<BluePrintContext>()
33 private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
37 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
38 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
40 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
41 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
42 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
43 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
45 every { bluePrintContext.metadata } returns metadata
47 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
51 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
52 val tr = TemplateResolution()
56 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
57 any(), any(), any(), any(), any()
61 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
62 bluePrintRuntimeService, artifactPrefix, resolutionKey
64 assertEquals(tr.result, res)
68 @Test(expected = EmptyResultDataAccessException::class)
69 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
70 val tr = TemplateResolution()
73 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
74 any(), any(), any(), any(), any()
77 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
78 bluePrintRuntimeService, artifactPrefix, resolutionKey
84 fun writeWithResolutionKeyTest() {
85 val tr = TemplateResolution()
87 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
89 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
90 any(), any(), any(), any(), any()
93 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
99 fun writeWithResolutionKeyExistingTest() {
100 val tr = TemplateResolution()
102 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
104 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
105 any(), any(), any(), any(), any()
109 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
110 any(), any(), any(), any(), any()
113 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
115 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
116 eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)
119 assertEquals(tr, res)
124 fun writeWithResourceIdResourceTypeExistingTest() {
125 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
126 val tr = TemplateResolution()
128 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
130 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
131 any(), any(), any(), any(), any(), any()
135 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
136 any(), any(), any(), any(), any(), any()
139 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
141 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
145 eq(blueprintVersion),
150 assertEquals(tr, res)