da1957190cf7ba9fba9b797030802523a8af5c67
[ccsdk/cds.git] /
1 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
2
3 import io.mockk.confirmVerified
4 import io.mockk.every
5 import io.mockk.mockk
6 import io.mockk.verify
7 import kotlinx.coroutines.runBlocking
8 import org.junit.Before
9 import org.junit.Test
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
18
19 class TemplateResolutionServiceTest {
20
21     private val templateResolutionRepository = mockk<TemplateResolutionRepository>()
22
23     private val templateResolutionService = TemplateResolutionService(templateResolutionRepository)
24
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>()
37
38     @Before
39     fun setup() {
40         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = blueprintVersion
41         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = blueprintName
42
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
47
48         every { bluePrintContext.metadata } returns metadata
49
50         every { bluePrintRuntimeService.bluePrintContext() } returns bluePrintContext
51     }
52
53     @Test
54     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTest() {
55         val tr = TemplateResolution()
56         tr.result = "res"
57         runBlocking {
58             every {
59                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
60                     any(), any(), any(), any(), any())
61             } returns tr
62             val res =
63                 templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
64                     bluePrintRuntimeService, artifactPrefix, resolutionKey)
65             assertEquals(tr.result, res)
66         }
67     }
68
69     @Test(expected = EmptyResultDataAccessException::class)
70     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameTestException() {
71         val tr = TemplateResolution()
72         runBlocking {
73             every {
74                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
75                     any(), any(), any(), any(), any())
76             } returns tr
77             templateResolutionService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
78                 bluePrintRuntimeService, artifactPrefix, resolutionKey)
79         }
80     }
81
82     @Test
83     fun writeWithResolutionKeyTest() {
84         val tr = TemplateResolution()
85         runBlocking {
86             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
87             every {
88                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
89                     any(), any(), any(), any(), any())
90             } returns null
91             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
92             assertEquals(tr, res)
93         }
94     }
95
96     @Test
97     fun writeWithResolutionKeyExistingTest() {
98         val tr = TemplateResolution()
99         runBlocking {
100             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
101             every {
102                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
103                     any(), any(), any(), any(), any())
104             } returns tr
105             every {
106                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
107                     any(), any(), any(), any(), any())
108             } returns Unit
109             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
110             verify {
111                 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
112                     eq(resolutionKey), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
113             }
114             assertEquals(tr, res)
115         }
116     }
117
118     @Test
119     fun writeWithResourceIdResourceTypeExistingTest() {
120         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] = ""
121         val tr = TemplateResolution()
122         runBlocking {
123             every { templateResolutionRepository.saveAndFlush(any<TemplateResolution>()) } returns tr
124             every {
125                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
126                     any(), any(), any(), any(), any(), any())
127             } returns tr
128             every {
129                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
130                     any(), any(), any(), any(), any(), any())
131             } returns Unit
132             val res = templateResolutionService.write(props, result, bluePrintRuntimeService, artifactPrefix)
133             verify {
134                 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
135                     eq(resourceId), eq(resourceType), eq(blueprintName), eq(blueprintVersion), eq(artifactPrefix), eq(occurrence))
136             }
137             assertEquals(tr, res)
138         }
139     }
140 }