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()
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)
63 resourceResolutionComponent.operationInputs = props
64 resourceResolutionComponent.bluePrintRuntimeService = bluePrintRuntimeService
65 resourceResolutionComponent.nodeTemplateName = nodeTemplateName
67 resourceResolutionComponent.executionServiceInput = executionRequest
68 resourceResolutionComponent.processId = "12"
69 resourceResolutionComponent.workflowName = "workflow"
70 resourceResolutionComponent.stepName = "step"
71 resourceResolutionComponent.interfaceName = "interfaceName"
72 resourceResolutionComponent.operationName = "operationName"
74 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
78 fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
81 resourceResolutionComponent.processNB(executionRequest)
82 } catch (e: BlueprintProcessorException) {
84 "Can't proceed with the resolution: either provide resolution-key OR combination of resource-id and resource-type.",
94 fun processNBWithResourceIdTestException() {
95 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
96 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
100 resourceResolutionComponent.processNB(executionRequest)
101 } catch (e: BlueprintProcessorException) {
103 "Can't proceed with the resolution: both resource-id and resource-type should be provided, one of them is missing.",
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()
120 resourceResolutionComponent.processNB(executionRequest)
121 } catch (e: BlueprintProcessorException) {
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.",
134 fun processNBTest() {
135 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
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
144 resourceResolutionService.resolveResources(
148 any<MutableMap<String, Any>>()
150 } returns ResourceResolutionResult(mutableMapOf(), mutableMapOf())
153 resourceResolutionComponent.processNB(executionRequest)
156 // FIXME add verification
158 // resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
159 // eq(nodeTemplateName), eq(artifactNames), eq(properties))
166 val blueprintError = BlueprintError()
167 val exception = RuntimeException("message")
168 every { bluePrintRuntimeService.getBlueprintError() } returns blueprintError
169 resourceResolutionComponent.recoverNB(exception, executionRequest)
171 assertEquals(1, blueprintError.errors.size)