2 * Copyright © 2022 Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.NullNode
21 import io.mockk.coEvery
26 import junit.framework.Assert.assertEquals
27 import junit.framework.Assert.assertTrue
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Before
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceDeletionComponent.Companion.ATTRIBUTE_RESULT
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceDeletionComponent.Companion.ATTRIBUTE_SUCCESS
34 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceDeletionComponent.Companion.INPUT_FAIL_ON_EMPTY
35 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceDeletionComponent.Companion.INPUT_LAST_N_OCCURRENCES
36 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY
37 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID
38 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE
39 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
40 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
41 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
42 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
43 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
44 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
45 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
46 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
47 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
48 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
49 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
51 class ResourceDeletionComponentTest {
53 private val blueprintName = "testCBA"
54 private val blueprintVersion = "1.0.0"
55 private val artifactNames = listOf("artifact-a", "artifact-b")
56 private val resolutionKey = "resolutionKey"
57 private val resourceId = "1"
58 private val resourceType = "ServiceInstance"
59 private val nodetemplateName = "resource-deletion"
60 private val executionRequest = ExecutionServiceInput()
62 private lateinit var resourceResolutionDBService: ResourceResolutionDBService
63 private lateinit var templateResolutionService: TemplateResolutionService
64 private lateinit var resourceDeletionComponent: ResourceDeletionComponent
65 private lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
67 private val props = mutableMapOf<String, JsonNode>()
69 private var success = slot<JsonNode>()
70 private var result = slot<JsonNode>()
74 bluePrintRuntimeService = spyk()
75 every { bluePrintRuntimeService.bluePrintContext() }.returns(
77 ServiceTemplate().apply {
78 this.metadata = mutableMapOf(
79 BluePrintConstants.METADATA_TEMPLATE_VERSION to blueprintVersion,
80 BluePrintConstants.METADATA_TEMPLATE_NAME to blueprintName
85 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(nodetemplateName, ATTRIBUTE_SUCCESS, capture(success)) }
87 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(nodetemplateName, ATTRIBUTE_RESULT, capture(result)) }
90 resourceResolutionDBService = mockk()
91 templateResolutionService = mockk()
92 resourceDeletionComponent = ResourceDeletionComponent(resourceResolutionDBService, templateResolutionService)
93 resourceDeletionComponent.bluePrintRuntimeService = bluePrintRuntimeService
94 resourceDeletionComponent.nodeTemplateName = nodetemplateName
95 resourceDeletionComponent.executionServiceInput = executionRequest
96 resourceDeletionComponent.processId = "12"
97 resourceDeletionComponent.workflowName = "workflow"
98 resourceDeletionComponent.stepName = "step"
99 resourceDeletionComponent.interfaceName = "interfaceName"
100 resourceDeletionComponent.operationName = "operationName"
102 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
103 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = NullNode.getInstance()
104 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
105 props[ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES] = artifactNames.asJsonType()
106 props[INPUT_FAIL_ON_EMPTY] = NullNode.getInstance()
107 props[INPUT_LAST_N_OCCURRENCES] = NullNode.getInstance()
108 resourceDeletionComponent.operationInputs = props
112 fun `using resolution-key`() {
113 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
116 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resolutionKey, null)
120 resourceResolutionDBService.deleteResources(blueprintName, blueprintVersion, any(), resolutionKey, null)
123 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
125 val expected = ResourceDeletionComponent.DeletionResult(1, 2).asJsonType()
126 val result: JsonNode = result.captured
127 assertEquals(expected, result[artifactNames[0]])
128 assertEquals(expected, result[artifactNames[1]])
129 assertEquals(true.asJsonPrimitive(), success.captured)
133 fun `using resource-type and resource-id`() {
134 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
135 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
138 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resourceType, resourceId, null)
142 resourceResolutionDBService.deleteResources(blueprintName, blueprintVersion, any(), resourceType, resourceId, null)
145 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
147 val expected = ResourceDeletionComponent.DeletionResult(2, 4).asJsonType()
148 val result: JsonNode = result.captured
149 assertEquals(expected, result[artifactNames[0]])
150 assertEquals(expected, result[artifactNames[1]])
151 assertEquals(true.asJsonPrimitive(), success.captured)
154 @Test(expected = BluePrintProcessorException::class)
155 fun `using resource-type missing resource-id`() {
156 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
157 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
160 @Test(expected = BluePrintProcessorException::class)
161 fun `using resource-id missing resource-type`() {
162 props[RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
163 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
167 fun `attributes present when failing`() {
168 val threwException = runBlocking {
170 resourceDeletionComponent.processNB(executionRequest)
172 } catch (e: Exception) {
176 assertTrue(threwException)
177 assertEquals(false.asJsonPrimitive(), success.captured)
178 assertEquals(emptyMap<String, Any>().asJsonNode(), result.captured)
182 fun `last-n-occurrences`() {
183 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
184 props[INPUT_LAST_N_OCCURRENCES] = JacksonUtils.jsonNodeFromObject(3)
187 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resolutionKey, 3)
191 resourceResolutionDBService.deleteResources(blueprintName, blueprintVersion, any(), resolutionKey, 3)
194 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
196 val expected = ResourceDeletionComponent.DeletionResult(3, 6).asJsonType()
197 val result: JsonNode = result.captured
198 assertEquals(expected, result[artifactNames[0]])
199 assertEquals(expected, result[artifactNames[1]])
200 assertEquals(true.asJsonPrimitive(), success.captured)
204 fun `fail-on-empty nothing deleted`() {
205 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
206 props[INPUT_FAIL_ON_EMPTY] = true.asJsonPrimitive()
209 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resolutionKey, null)
213 resourceResolutionDBService.deleteResources(blueprintName, blueprintVersion, any(), resolutionKey, null)
216 val threwException = runBlocking {
218 resourceDeletionComponent.processNB(executionRequest)
220 } catch (e: BluePrintProcessorException) {
225 val expected = ResourceDeletionComponent.DeletionResult(0, 0).asJsonType()
226 val result: JsonNode = result.captured
227 assertTrue(threwException)
228 assertEquals(expected, result[artifactNames[0]])
229 assertEquals(expected, result[artifactNames[1]])
230 assertEquals(false.asJsonPrimitive(), success.captured)
234 fun `fail-on-empty something deleted`() {
235 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
236 props[INPUT_FAIL_ON_EMPTY] = true.asJsonPrimitive()
239 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resolutionKey, null)
243 resourceResolutionDBService.deleteResources(blueprintName, blueprintVersion, any(), resolutionKey, null)
246 runBlocking { resourceDeletionComponent.processNB(executionRequest) }
248 val expected = ResourceDeletionComponent.DeletionResult(1, 1).asJsonType()
249 val result: JsonNode = result.captured
250 assertEquals(expected, result[artifactNames[0]])
251 assertEquals(expected, result[artifactNames[1]])
252 assertEquals(true.asJsonPrimitive(), success.captured)
256 fun `db throws exception`() {
257 props[RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
260 templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, any(), resolutionKey, null)
261 }.throws(RuntimeException("DB failure!"))
263 val threwException = runBlocking {
265 resourceDeletionComponent.processNB(executionRequest)
267 } catch (e: Exception) {
272 assertTrue(threwException)
273 assertEquals(false.asJsonPrimitive(), success.captured)
274 assertEquals(emptyMap<String, Any>().asJsonNode(), result.captured)