2 * Copyright © 2019 IBM, 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.
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
18 import com.fasterxml.jackson.databind.JsonNode
19 import kotlinx.coroutines.runBlocking
21 import org.junit.runner.RunWith
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock.MockBluePrintRestLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock.MockBlueprintWebClientService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.mock.MockRestResourceResolutionProcessor
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
29 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestClientProperties
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import kotlin.test.AfterTest
40 import kotlin.test.BeforeTest
41 import kotlin.test.assertEquals
42 import kotlin.test.assertFalse
43 import kotlin.test.assertTrue
45 @RunWith(SpringRunner::class)
46 @ContextConfiguration(
48 MockRestResourceResolutionProcessor::class, MockBluePrintRestLibPropertyService::class,
49 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, RestClientProperties::class
52 @TestPropertySource(locations = ["classpath:application-test.properties"])
53 class RestResourceResolutionProcessorTest {
56 lateinit var bluePrintRestLibPropertyService: MockBluePrintRestLibPropertyService
58 private lateinit var restResourceResolutionProcessor: MockRestResourceResolutionProcessor
62 restResourceResolutionProcessor = MockRestResourceResolutionProcessor(bluePrintRestLibPropertyService)
64 val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
65 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
68 val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext)
70 restResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService
71 restResourceResolutionProcessor.resourceDictionaries = ResourceAssignmentUtils
72 .resourceDefinitions(bluePrintContext.rootPath)
74 val scriptPropertyInstances: MutableMap<String, Any> = mutableMapOf()
75 scriptPropertyInstances["mock-service1"] = MockCapabilityService()
76 scriptPropertyInstances["mock-service2"] = MockCapabilityService()
78 restResourceResolutionProcessor.scriptPropertyInstances = scriptPropertyInstances
84 bluePrintRestLibPropertyService.tearDown()
87 private fun getExpectedJsonResponse(field: String? = null): JsonNode {
88 val node = JacksonUtils.jsonNode(MockBlueprintWebClientService.JSON_OUTPUT)
89 return if (field != null)
96 fun `test rest resource resolution sdnc`() {
98 val resourceAssignment = ResourceAssignment().apply {
100 dictionaryName = "vnf_name"
101 dictionarySource = "sdnc"
102 property = PropertyDefinition().apply {
108 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
109 assertTrue(result, "get Rest resource assignment failed")
111 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
112 "get Rest resource assignment failed"
114 val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
115 println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.asText()}")
117 getExpectedJsonResponse(resourceAssignment.name).asText(),
119 "get Rest resource assignment failed - enexpected value"
125 fun `test rest resource resolution get required fails`() {
127 val resourceAssignment = ResourceAssignment().apply {
128 name = "rr-aai-empty"
129 dictionaryName = "aai-get-resource-null"
130 dictionarySource = "aai-data"
131 property = PropertyDefinition().apply {
137 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
138 assertFalse(result, "get Rest resource assignment succeeded while it should fail")
140 resourceAssignment.status, BluePrintConstants.STATUS_FAILURE,
141 "get Rest resource assignment succeeded while it should fail"
143 println("Resolution result: $result, status: ${resourceAssignment.status}")
148 fun `test rest resource resolution get with wrong mapping fails`() {
150 val resourceAssignment = ResourceAssignment().apply {
151 name = "rr-aai-wrong-mapping"
152 dictionaryName = "aai-get-resource-wrong-mapping"
153 dictionarySource = "aai-data"
154 property = PropertyDefinition().apply {
160 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
161 assertFalse(result, "get Rest resource assignment succeeded while it should fail")
163 resourceAssignment.status, BluePrintConstants.STATUS_FAILURE,
164 "get Rest resource assignment succeeded while it should fail"
166 println("Resolution result: $result, status: ${resourceAssignment.status}")
171 fun `test rest resource resolution get without output mapping`() {
173 val resourceAssignment = ResourceAssignment().apply {
174 name = "rr-aai-empty"
175 dictionaryName = "aai-get-resource-null"
176 dictionarySource = "aai-data"
177 property = PropertyDefinition().apply {
183 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
184 assertTrue(result, "get Rest resource assignment failed")
186 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
187 "get Rest resource assignment failed"
189 println("Resolution result: $result, status: ${resourceAssignment.status}")
194 fun `test rest resource resolution aai get string`() {
196 val resourceAssignment = ResourceAssignment().apply {
198 dictionaryName = "aai-get-resource"
199 dictionarySource = "aai-data"
200 property = PropertyDefinition().apply {
206 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
207 assertTrue(result, "get AAI string Rest resource assignment failed")
209 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
210 "get AAI string Rest resource assignment failed"
212 val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
213 println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.asText()}")
215 getExpectedJsonResponse(resourceAssignment.name).asText(),
217 "get Rest resource assignment failed - enexpected value"
223 fun `test rest resource resolution aai get json`() {
225 val resourceAssignment = ResourceAssignment().apply {
227 dictionaryName = "aai-get-json-resource"
228 dictionarySource = "aai-data"
229 property = PropertyDefinition().apply {
235 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
236 assertTrue(result, "get AAI json Rest resource assignment failed")
238 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
239 "get AAI json Rest resource assignment failed"
241 val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
242 println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.toPrettyString()}")
244 getExpectedJsonResponse().toPrettyString(),
245 value.toPrettyString(),
246 "get Rest resource assignment failed - enexpected value"
252 fun `test rest resource resolution aai put`() {
254 val resourceAssignment = ResourceAssignment().apply {
256 dictionaryName = "aai-put-resource"
257 dictionarySource = "aai-data"
258 property = PropertyDefinition().apply {
263 val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
264 assertTrue(result, "put AAI Rest resource assignment failed")
266 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
267 "put AAI json Rest resource assignment failed"
269 println("Resolution result: $result, status: ${resourceAssignment.status}")