2 * Copyright © 2018 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.MissingNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import io.mockk.coEvery
25 import kotlinx.coroutines.runBlocking
26 import org.junit.Before
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
37 class ResourceResolutionComponentTest {
39 private val resourceResolutionService = mockk<ResourceResolutionService>()
40 private val resourceResolutionComponent = ResourceResolutionComponent(resourceResolutionService)
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"
51 private val executionRequest = ExecutionServiceInput()
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)
64 resourceResolutionComponent.operationInputs = props
65 resourceResolutionComponent.bluePrintRuntimeService = bluePrintRuntimeService
66 resourceResolutionComponent.nodeTemplateName = nodeTemplateName
68 resourceResolutionComponent.executionServiceInput = executionRequest
69 resourceResolutionComponent.processId = "12"
70 resourceResolutionComponent.workflowName = "workflow"
71 resourceResolutionComponent.stepName = "step"
72 resourceResolutionComponent.interfaceName = "interfaceName"
73 resourceResolutionComponent.operationName = "operationName"
77 fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
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.",
91 fun processNBWithResourceIdTestException() {
92 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
93 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
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.",
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()
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.",
127 fun processNBTest() {
128 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
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
137 resourceResolutionService.resolveResources(any(),
140 any<MutableMap<String, Any>>())
141 } returns mutableMapOf()
142 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
146 resourceResolutionComponent.processNB(executionRequest)
149 // FIXME add verification
151 // resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
152 // eq(nodeTemplateName), eq(artifactNames), eq(properties))
159 val blueprintError = BluePrintError()
160 val exception = RuntimeException("message")
161 every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
162 resourceResolutionComponent.recoverNB(exception, executionRequest)
164 assertEquals(1, blueprintError.errors.size)