1 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
3 import io.mockk.confirmVerified
7 import kotlinx.coroutines.runBlocking
8 import org.junit.Before
10 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
11 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
12 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
13 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
14 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
15 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
16 import org.springframework.dao.EmptyResultDataAccessException
17 import kotlin.test.assertEquals
19 class TemplateResolutionServiceTest {
21 private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
23 private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
25 private val resolutionKey = "resolutionKey"
26 private val resourceId = "1"
27 private val resourceType = "ServiceInstance"
28 private val occurrence = 0
29 private val artifactPrefix = "template"
30 private val blueprintName = "blueprintName"
31 private val blueprintVersion = "1.0.0"
32 private val result = "result"
33 private val metadata = hashMapOf<String, String>()
34 private val props = hashMapOf<String, Any>()
35 private val bluePrintContext = mockk<BluePrintContext>()
36 private val bluePrintRuntimeService = mockk<DefaultBluePrintRuntimeService>()
40 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
41 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
43 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = resolutionKey
44 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
45 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
46 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
48 every { bluePrintContext.metadata } returns metadata
50 every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
54 fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
55 val tr = TemplateResolution()
59 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
60 any(), any(), any(), any(), any())
63 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
64 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())
77 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
78 bluePrintRuntimeService, artifactPrefix, resolutionKey)
83 fun writeWithResolutionKeyTest() {
84 val tr = TemplateResolution()
86 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
88 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
89 any(), any(), any(), any(), any())
91 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
97 fun writeWithResolutionKeyExistingTest() {
98 val tr = TemplateResolution()
100 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
102 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
103 any(), any(), any(), any(), any())
106 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
107 any(), any(), any(), any(), any())
109 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
111 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
112 eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
114 assertEquals(tr, res)
119 fun writeWithResourceIdResourceTypeExistingTest() {
120 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = ""
121 val tr = TemplateResolution()
123 every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
125 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
126 any(), any(), any(), any(), any(), any())
129 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
130 any(), any(), any(), any(), any(), any())
132 val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
134 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
135 eq(resourceId), eq(resourceType), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
137 assertEquals(tr, res)