39076b4f55bd9d947d82ee08a5a37e8abb648835
[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 io.mockk.coEvery
21 import io.mockk.every
22 import io.mockk.mockk
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Before
25 import org.junit.Test
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import java.lang.RuntimeException
33 import kotlin.test.assertEquals
34 import kotlin.test.fail
35
36 class ResourceResolutionComponentTest {
37
38     private val resourceResolutionService = mockk<ResourceResolutionService>()
39     private val resourceResolutionComponent = ResourceResolutionComponent(resourceResolutionService)
40
41     private val resolutionKey = "resolutionKey"
42     private val resourceId = "1"
43     private val resourceType = "ServiceInstance"
44     private val occurrence = 1
45     private val props = mutableMapOf<String, JsonNode>()
46     private val bluePrintRuntimeService = mockk<BluePrintRuntimeService<*>>()
47     private val artifactNames = listOf("template")
48     private val nodeTemplateName = "nodeTemplateName"
49
50     private val executionRequest = ExecutionServiceInput()
51
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
75     @Test
76     fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
77         runBlocking {
78             try {
79                 resourceResolutionComponent.processNB(executionRequest)
80             } catch (e: BluePrintProcessorException) {
81                 assertEquals("Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.",
82                     e.message)
83                 return@runBlocking
84             }
85             fail()
86         }
87     }
88
89     @Test
90     fun processNBWithResourceIdTestException() {
91         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = "".asJsonPrimitive()
92         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = "".asJsonPrimitive()
93
94         runBlocking {
95             try {
96                 resourceResolutionComponent.processNB(executionRequest)
97             } catch (e: BluePrintProcessorException) {
98                 assertEquals("Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.",
99                     e.message)
100                 return@runBlocking
101             }
102             fail()
103         }
104     }
105
106     @Test
107     fun processNBWithEmptyResourceTypeResourceIdResolutionKeyTestException() {
108         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = "".asJsonPrimitive()
109         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = "".asJsonPrimitive()
110         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = "".asJsonPrimitive()
111
112         runBlocking {
113             try {
114                 resourceResolutionComponent.processNB(executionRequest)
115             } catch (e: BluePrintProcessorException) {
116                 assertEquals("Can't proceed with the resolution: can't persist resolution without a correlation key. " +
117                         "Either provide a resolution-key OR combination of resource-id and resource-type OR set `storeResult` to false.",
118                     e.message)
119                 return@runBlocking
120             }
121             fail()
122         }
123     }
124
125     @Test
126     fun processNBTest() {
127         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = "".asJsonPrimitive()
128
129         val properties = mutableMapOf<String, Any>()
130         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
131         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
132         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
133         properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
134
135         coEvery {
136             resourceResolutionService.resolveResources(any<BluePrintRuntimeService<*>>(),
137                 any<String>(),
138                 any<List<String>>(),
139                 any<MutableMap<String, Any>>())
140         } returns mutableMapOf()
141         every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
142
143
144         runBlocking {
145             resourceResolutionComponent.processNB(executionRequest)
146         }
147
148 // FIXME add verification
149 //        coVerify {
150 //            resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
151 //                eq(nodeTemplateName), eq(artifactNames), eq(properties))
152 //        }
153     }
154
155     @Test
156     fun testRecover() {
157         runBlocking {
158             val blueprintError = BluePrintError()
159             val exception = RuntimeException("message")
160             every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
161             resourceResolutionComponent.recoverNB(exception, executionRequest)
162
163             assertEquals(1, blueprintError.errors.size)
164         }
165     }
166 }