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 io.mockk.coEvery
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Before
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
36 class ResourceResolutionComponentTest {
38 private val resourceResolutionService = mockk<ResourceResolutionService>()
39 private val resourceResolutionComponent = ResourceResolutionComponent(resourceResolutionService)
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"
50 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"
76 fun processNBWithResolutionKeyAndResourceIdAndResourceTypeTestException() {
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.",
90 fun processNBWithResourceIdTestException() {
91 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = "".asJsonPrimitive()
92 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = "".asJsonPrimitive()
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.",
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()
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.",
126 fun processNBTest() {
127 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = "".asJsonPrimitive()
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
136 resourceResolutionService.resolveResources(any<BluePrintRuntimeService<*>>(),
139 any<MutableMap<String, Any>>())
140 } returns mutableMapOf()
141 every { bluePrintRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
145 resourceResolutionComponent.processNB(executionRequest)
148 // FIXME add verification
150 // resourceResolutionService.resolveResources(eq(bluePrintRuntimeService),
151 // eq(nodeTemplateName), eq(artifactNames), eq(properties))
158 val blueprintError = BluePrintError()
159 val exception = RuntimeException("message")
160 every { bluePrintRuntimeService.getBluePrintError() } returns blueprintError
161 resourceResolutionComponent.recoverNB(exception, executionRequest)
163 assertEquals(1, blueprintError.errors.size)