560bc4142d6020deadeb9a85d9f688166338b353
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionComponentTest.kt
1 /*
2  * Copyright © 2018 Bell Canada
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.MissingNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import io.mockk.coEvery
23 import io.mockk.every
24 import io.mockk.mockk
25 import kotlinx.coroutines.runBlocking
26 import org.junit.Before
27 import org.junit.Test
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
34 import kotlin.test.assertEquals
35 import kotlin.test.fail
36
37 class ResourceResolutionComponentTest {
38
39     private val resourceResolutionService = mockk<ResourceResolutionService>()
40     private val resourceResolutionComponent = ResourceResolutionComponent(resourceResolutionService)
41
42     private val resolutionKey = "resolutionKey"
43     private val resourceId = "1"
44     private val resourceType = "ServiceInstance"
45     private val occurrence = 1
46     private val props = mutableMapOf<String, JsonNode>()
47     private val bluePrintRuntimeService = mockk<BluePrintRuntimeService<*>>()
48     private val artifactNames = listOf("template")
49     private val nodeTemplateName = "nodeTemplateName"
50
51     private val executionRequest = ExecutionServiceInput()
52
53
54     @Before
55     fun setup() {
56
57         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true.asJsonPrimitive()
58         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
59         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
60         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
61         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence.asJsonPrimitive()
62         props[ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES] = JacksonUtils.jsonNodeFromObject(artifactNames)
63
64         resourceResolutionComponent.operationInputs = props
65         resourceResolutionComponent.bluePrintRuntimeService = bluePrintRuntimeService
66         resourceResolutionComponent.nodeTemplateName = nodeTemplateName
67
68         resourceResolutionComponent.executionServiceInput = executionRequest
69         resourceResolutionComponent.processId = "12"
70         resourceResolutionComponent.workflowName = "workflow"
71         resourceResolutionComponent.stepName = "step"
72         resourceResolutionComponent.interfaceName = "interfaceName"
73         resourceResolutionComponent.operationName = "operationName"
74     }
75
76     @Test
77     fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
78         runBlocking {
79             try {
80                 resourceResolutionComponent.processNB(executionRequest)
81             } catch (e: BluePrintProcessorException) {
82                 assertEquals("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.",
83                         e.message)
84                 return@runBlocking
85             }
86             fail()
87         }
88     }
89
90     @Test
91     fun processNBWithResourceIdTestException() {
92         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
93         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
94
95         runBlocking {
96             try {
97                 resourceResolutionComponent.processNB(executionRequest)
98             } catch (e: BluePrintProcessorException) {
99                 assertEquals("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.",
100                         e.message)
101                 return@runBlocking
102             }
103             fail()
104         }
105     }
106
107     @Test
108     fun processNBWithEmptyResourceTypeResourceIdResolutionKeyTestException() {
109         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = MissingNode.getInstance()
110         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
111         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = NullNode.getInstance()
112
113         runBlocking {
114             try {
115                 resourceResolutionComponent.processNB(executionRequest)
116             } catch (e: BluePrintProcessorException) {
117                 assertEquals("Can't proceed with the resolution: can't persist resolution without a correlation key. " +
118                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.",
119                         e.message)
120                 return@runBlocking
121             }
122             fail()
123         }
124     }
125
126     @Test
127     fun processNBTest() {
128         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
129
130         val properties = mutableMapOf<String, Any>()
131         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
132         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
133         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
134         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
135
136         coEvery {
137             resourceResolutionService.resolveResources(any(),
138                     any(),
139                     any<List<String>>(),
140                     any<MutableMap<String, Any>>())
141         } returns mutableMapOf()
142         every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
143
144
145         runBlocking {
146             resourceResolutionComponent.processNB(executionRequest)
147         }
148
149 // FIXME add verification
150 //        coVerify {
151 //            resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
152 //                eq(nodeTemplateName), eq(artifactNames), eq(properties))
153 //        }
154     }
155
156     @Test
157     fun testRecover() {
158         runBlocking {
159             val blueprintError = BluePrintError()
160             val exception = RuntimeException("message")
161             every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
162             resourceResolutionComponent.recoverNB(exception, executionRequest)
163
164             assertEquals(1, blueprintError.errors.size)
165         }
166     }
167 }