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"
75 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
79 fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
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.",
93 fun processNBWithResourceIdTestException() {
94 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
95 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = NullNode.getInstance()
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.",
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()
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.",
129 fun processNBTest() {
130 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = NullNode.getInstance()
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
139 resourceResolutionService.resolveResources(any(),
142 any<MutableMap<String, Any>>())
143 } returns mutableMapOf()
147 resourceResolutionComponent.processNB(executionRequest)
150 // FIXME add verification
152 // resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
153 // eq(nodeTemplateName), eq(artifactNames), eq(properties))
160 val blueprintError = BluePrintError()
161 val exception = RuntimeException("message")
162 every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
163 resourceResolutionComponent.recoverNB(exception, executionRequest)
165 assertEquals(1, blueprintError.errors.size)