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())
60 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
61 bluePrintRuntimeService, artifactPrefix, resolutionKey)
62 assertEquals(tr.result, res)
66 @Test(expected = EmptyResultDataAccessException::class)
67 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
68 val tr = TemplateResolution()
71 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
72 any(), any(), any(), any(), any())
74 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
75 bluePrintRuntimeService, artifactPrefix, resolutionKey)
80 fun writeWithResolutionKeyTest() {
81 val tr = TemplateResolution()
83 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
85 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
86 any(), any(), any(), any(), any())
88 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
94 fun writeWithResolutionKeyExistingTest() {
95 val tr = TemplateResolution()
97 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
99 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
100 any(), any(), any(), any(), any())
103 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
104 any(), any(), any(), any(), any())
106 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
108 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
109 eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
111 assertEquals(tr, res)
116 fun writeWithResourceIdResourceTypeExistingTest() {
117 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
118 val tr = TemplateResolution()
120 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
122 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
123 any(), any(), any(), any(), any(), any())
126 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
127 any(), any(), any(), any(), any(), any())
129 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
131 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
132 eq(resourceId), eq(resourceType), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
134 assertEquals(tr, res)