48c6f02ef3d3a465b53c566f2b84266564cad0e2
[ccsdk/cds.git] /
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             } returns tr
59             val res =
60                 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
61                     bluePrintRuntimeService, artifactPrefix, resolutionKey)
62             assertEquals(tr.result, res)
63         }
64     }
65
66     @Test(expected = EmptyResultDataAccessException::class)
67     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
68         val tr = TemplateResolution()
69         runBlocking {
70             every {
71                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
72                     any(), any(), any(), any(), any())
73             } returns tr
74             templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
75                 bluePrintRuntimeService, artifactPrefix, resolutionKey)
76         }
77     }
78
79     @Test
80     fun writeWithResolutionKeyTest() {
81         val tr = TemplateResolution()
82         runBlocking {
83             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
84             every {
85                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
86                     any(), any(), any(), any(), any())
87             } returns null
88             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
89             assertEquals(tr, res)
90         }
91     }
92
93     @Test
94     fun writeWithResolutionKeyExistingTest() {
95         val tr = TemplateResolution()
96         runBlocking {
97             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
98             every {
99                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
100                     any(), any(), any(), any(), any())
101             } returns tr
102             every {
103                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
104                     any(), any(), any(), any(), any())
105             } returns Unit
106             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
107             verify {
108                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
109                     eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
110             }
111             assertEquals(tr, res)
112         }
113     }
114
115     @Test
116     fun writeWithResourceIdResourceTypeExistingTest() {
117         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
118         val tr = TemplateResolution()
119         runBlocking {
120             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
121             every {
122                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
123                     any(), any(), any(), any(), any(), any())
124             } returns tr
125             every {
126                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
127                     any(), any(), any(), any(), any(), any())
128             } returns Unit
129             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
130             verify {
131                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
132                     eq(resourceId), eq(resourceType), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
133             }
134             assertEquals(tr, res)
135         }
136     }
137 }