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 / 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     @Before
54     fun setup() {
55
56         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true.asJsonPrimitive()
57         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey.asJsonPrimitive()
58         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
59         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
60         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence.asJsonPrimitive()
61         props[ResourceResolutionConstants.INPUT_ARTIFACT_PREFIX_NAMES] = JacksonUtils.jsonNodeFromObject(artifactNames)
62
63         resourceResolutionComponent.operationInputs = props
64         resourceResolutionComponent.bluePrintRuntimeService = bluePrintRuntimeService
65         resourceResolutionComponent.nodeTemplateName = nodeTemplateName
66
67         resourceResolutionComponent.executionServiceInput = executionRequest
68         resourceResolutionComponent.processId = "12"
69         resourceResolutionComponent.workflowName = "workflow"
70         resourceResolutionComponent.stepName = "step"
71         resourceResolutionComponent.interfaceName = "interfaceName"
72         resourceResolutionComponent.operationName = "operationName"
73
74         every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
75     }
76
77     @Test
78     fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
79         runBlocking {
80             try {
81                 resourceResolutionComponent.processNB(executionRequest)
82             } catch (e: BlueprintProcessorException) {
83                 assertEquals(
84                     "Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.",
85                     e.message
86                 )
87                 return@runBlocking
88             }
89             fail()
90         }
91     }
92
93     @Test
94     fun processNBWithResourceIdTestException() {
95         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
96         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
97
98         runBlocking {
99             try {
100                 resourceResolutionComponent.processNB(executionRequest)
101             } catch (e: BlueprintProcessorException) {
102                 assertEquals(
103                     "Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.",
104                     e.message
105                 )
106                 return@runBlocking
107             }
108             fail()
109         }
110     }
111
112     @Test
113     fun processNBWithEmptyResourceTypeResourceIdResolutionKeyTestException() {
114         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = MissingNode.getInstance()
115         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
116         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = NullNode.getInstance()
117
118         runBlocking {
119             try {
120                 resourceResolutionComponent.processNB(executionRequest)
121             } catch (e: BlueprintProcessorException) {
122                 assertEquals(
123                     "Can't proceed with the resolution: can't persist resolution without a correlation key. " +
124                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.",
125                     e.message
126                 )
127                 return@runBlocking
128             }
129             fail()
130         }
131     }
132
133     @Test
134     fun processNBTest() {
135         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
136
137         val properties = mutableMapOf<String, Any>()
138         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
139         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
140         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
141         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
142
143         coEvery {
144             resourceResolutionService.resolveResources(
145                 any(),
146                 any(),
147                 any<List<String>>(),
148                 any<MutableMap<String, Any>>()
149             )
150         } returns ResourceResolutionResult(mutableMapOf(), mutableMapOf())
151
152         runBlocking {
153             resourceResolutionComponent.processNB(executionRequest)
154         }
155
156         // FIXME add verification
157         //        coVerify {
158         //            resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
159         //                eq(nodeTemplateName), eq(artifactNames), eq(properties))
160         //        }
161     }
162
163     @Test
164     fun testRecover() {
165         runBlocking {
166             val blueprintError = BlueprintError()
167             val exception = RuntimeException("message")
168             every { bluePrintRuntimeService.getBlueprintError() } returns blueprintError
169             resourceResolutionComponent.recoverNB(exception, executionRequest)
170
171             assertEquals(1, blueprintError.errors.size)
172         }
173     }
174 }