b63fa679822320bba1e13f3bb50b3735d22763ed
[ccsdk/cds.git] /
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         every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
76     }
77
78     @Test
79     fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
80         runBlocking {
81             try {
82                 resourceResolutionComponent.processNB(executionRequest)
83             } catch (e: BluePrintProcessorException) {
84                 assertEquals("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.",
85                         e.message)
86                 return@runBlocking
87             }
88             fail()
89         }
90     }
91
92     @Test
93     fun processNBWithResourceIdTestException() {
94         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
95         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
96
97         runBlocking {
98             try {
99                 resourceResolutionComponent.processNB(executionRequest)
100             } catch (e: BluePrintProcessorException) {
101                 assertEquals("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.",
102                         e.message)
103                 return@runBlocking
104             }
105             fail()
106         }
107     }
108
109     @Test
110     fun processNBWithEmptyResourceTypeResourceIdResolutionKeyTestException() {
111         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = MissingNode.getInstance()
112         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
113         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = NullNode.getInstance()
114
115         runBlocking {
116             try {
117                 resourceResolutionComponent.processNB(executionRequest)
118             } catch (e: BluePrintProcessorException) {
119                 assertEquals("Can't proceed with the resolution: can't persist resolution without a correlation key. " +
120                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.",
121                         e.message)
122                 return@runBlocking
123             }
124             fail()
125         }
126     }
127
128     @Test
129     fun processNBTest() {
130         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
131
132         val properties = mutableMapOf<String, Any>()
133         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
134         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
135         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
136         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
137
138         coEvery {
139             resourceResolutionService.resolveResources(any(),
140                     any(),
141                     any<List<String>>(),
142                     any<MutableMap<String, Any>>())
143         } returns mutableMapOf()
144
145
146         runBlocking {
147             resourceResolutionComponent.processNB(executionRequest)
148         }
149
150 // FIXME add verification
151 //        coVerify {
152 //            resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
153 //                eq(nodeTemplateName), eq(artifactNames), eq(properties))
154 //        }
155     }
156
157     @Test
158     fun testRecover() {
159         runBlocking {
160             val blueprintError = BluePrintError()
161             val exception = RuntimeException("message")
162             every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
163             resourceResolutionComponent.recoverNB(exception, executionRequest)
164
165             assertEquals(1, blueprintError.errors.size)
166         }
167     }
168 }