Renaming Files having BluePrint to have Blueprint
[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
16 class TemplateResolutionServiceTest {
17
18     private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
19
20     private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
21
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>()
34
35     @Before
36     fun setup() {
37         metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
38         metadata[BlueprintConstants.METADATA_TEMPLATE_NAME] = blueprintName
39
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
44
45         every { bluePrintContext.metadata } returns metadata
46
47         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
48     }
49
50     @Test
51     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
52         val tr = TemplateResolution()
53         tr.result = "res"
54         runBlocking {
55             every {
56                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
57                     any(), any(), any(), any(), any()
58                 )
59             } returns tr
60             val res =
61                 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
62                     bluePrintRuntimeService, artifactPrefix, resolutionKey
63                 )
64             assertEquals(tr.result, res)
65         }
66     }
67
68     @Test(expected = EmptyResultDataAccessException::class)
69     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
70         val tr = TemplateResolution()
71         runBlocking {
72             every {
73                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
74                     any(), any(), any(), any(), any()
75                 )
76             } returns tr
77             templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
78                 bluePrintRuntimeService, artifactPrefix, resolutionKey
79             )
80         }
81     }
82
83     @Test
84     fun writeWithResolutionKeyTest() {
85         val tr = TemplateResolution()
86         runBlocking {
87             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
88             every {
89                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
90                     any(), any(), any(), any(), any()
91                 )
92             } returns null
93             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
94             assertEquals(tr, res)
95         }
96     }
97
98     @Test
99     fun writeWithResolutionKeyExistingTest() {
100         val tr = TemplateResolution()
101         runBlocking {
102             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
103             every {
104                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
105                     any(), any(), any(), any(), any()
106                 )
107             } returns tr
108             every {
109                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
110                     any(), any(), any(), any(), any()
111                 )
112             } returns Unit
113             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
114             verify {
115                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
116                     eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence)
117                 )
118             }
119             assertEquals(tr, res)
120         }
121     }
122
123     @Test
124     fun writeWithResourceIdResourceTypeExistingTest() {
125         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
126         val tr = TemplateResolution()
127         runBlocking {
128             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
129             every {
130                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
131                     any(), any(), any(), any(), any(), any()
132                 )
133             } returns tr
134             every {
135                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
136                     any(), any(), any(), any(), any(), any()
137                 )
138             } returns Unit
139             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
140             verify {
141                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
142                     eq(resourceId),
143                     eq(resourceType),
144                     eq(blueprintName),
145                     eq(blueprintVersion),
146                     eq(artifactPrefix),
147                     eq(occurrence)
148                 )
149             }
150             assertEquals(tr, res)
151         }
152     }
153 }