56ce3f65de611f933ca40568439da4ad3b057329
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / RestResourceResolutionProcessorTest.kt
1 /*
2  * Copyright © 2019 IBM, Bell Canada.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
17
18 import com.fasterxml.jackson.databind.JsonNode
19 import kotlinx.coroutines.runBlocking
20 import org.junit.Test
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
44
45 @RunWith(SpringRunner::class)
46 @ContextConfiguration(
47     classes = [
48         MockRestResourceResolutionProcessor::class, MockBluePrintRestLibPropertyService::class,
49         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, RestClientProperties::class
50     ]
51 )
52 @TestPropertySource(locations = ["classpath:application-test.properties"])
53 class RestResourceResolutionProcessorTest {
54
55     @Autowired
56     lateinit var bluePrintRestLibPropertyService: MockBluePrintRestLibPropertyService
57
58     private lateinit var restResourceResolutionProcessor: MockRestResourceResolutionProcessor
59
60     @BeforeTest
61     fun init() {
62         restResourceResolutionProcessor = MockRestResourceResolutionProcessor(bluePrintRestLibPropertyService)
63         runBlocking {
64             val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext(
65                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
66             )
67
68             val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext)
69
70             restResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService
71             restResourceResolutionProcessor.resourceDictionaries = ResourceAssignmentUtils
72                 .resourceDefinitions(bluePrintContext.rootPath)
73
74             val scriptPropertyInstances: MutableMap<String, Any> = mutableMapOf()
75             scriptPropertyInstances["mock-service1"] = MockCapabilityService()
76             scriptPropertyInstances["mock-service2"] = MockCapabilityService()
77
78             restResourceResolutionProcessor.scriptPropertyInstances = scriptPropertyInstances
79         }
80     }
81
82     @AfterTest
83     fun tearDown() {
84         bluePrintRestLibPropertyService.tearDown()
85     }
86
87     private fun getExpectedJsonResponse(field: String? = null): JsonNode {
88         val node = JacksonUtils.jsonNode(MockBlueprintWebClientService.JSON_OUTPUT)
89         return if (field != null)
90             node.get(field)
91         else
92             node
93     }
94
95     @Test
96     fun `test rest resource resolution sdnc`() {
97         runBlocking {
98             val resourceAssignment = ResourceAssignment().apply {
99                 name = "vnf_name"
100                 dictionaryName = "vnf_name"
101                 dictionarySource = "sdnc"
102                 property = PropertyDefinition().apply {
103                     type = "string"
104                     required = true
105                 }
106             }
107
108             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
109             assertTrue(result, "get Rest resource assignment failed")
110             assertEquals(
111                 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
112                 "get Rest resource assignment failed"
113             )
114             val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
115             println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.asText()}")
116             assertEquals(
117                 getExpectedJsonResponse(resourceAssignment.name).asText(),
118                 value.asText(),
119                 "get Rest resource assignment failed - enexpected value"
120             )
121         }
122     }
123
124     @Test
125     fun `test rest resource resolution get required fails`() {
126         runBlocking {
127             val resourceAssignment = ResourceAssignment().apply {
128                 name = "rr-aai-empty"
129                 dictionaryName = "aai-get-resource-null"
130                 dictionarySource = "aai-data"
131                 property = PropertyDefinition().apply {
132                     type = "string"
133                     required = true
134                 }
135             }
136
137             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
138             assertFalse(result, "get Rest resource assignment succeeded while it should fail")
139             assertEquals(
140                 resourceAssignment.status, BluePrintConstants.STATUS_FAILURE,
141                 "get Rest resource assignment succeeded while it should fail"
142             )
143             println("Resolution result: $result, status: ${resourceAssignment.status}")
144         }
145     }
146
147     @Test
148     fun `test rest resource resolution get with wrong mapping fails`() {
149         runBlocking {
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 {
155                     type = "string"
156                     required = false
157                 }
158             }
159
160             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
161             assertFalse(result, "get Rest resource assignment succeeded while it should fail")
162             assertEquals(
163                 resourceAssignment.status, BluePrintConstants.STATUS_FAILURE,
164                 "get Rest resource assignment succeeded while it should fail"
165             )
166             println("Resolution result: $result, status: ${resourceAssignment.status}")
167         }
168     }
169
170     @Test
171     fun `test rest resource resolution get without output mapping`() {
172         runBlocking {
173             val resourceAssignment = ResourceAssignment().apply {
174                 name = "rr-aai-empty"
175                 dictionaryName = "aai-get-resource-null"
176                 dictionarySource = "aai-data"
177                 property = PropertyDefinition().apply {
178                     type = "string"
179                     required = false
180                 }
181             }
182
183             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
184             assertTrue(result, "get Rest resource assignment failed")
185             assertEquals(
186                 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
187                 "get Rest resource assignment failed"
188             )
189             println("Resolution result: $result, status: ${resourceAssignment.status}")
190         }
191     }
192
193     @Test
194     fun `test rest resource resolution aai get string`() {
195         runBlocking {
196             val resourceAssignment = ResourceAssignment().apply {
197                 name = "vnf-id"
198                 dictionaryName = "aai-get-resource"
199                 dictionarySource = "aai-data"
200                 property = PropertyDefinition().apply {
201                     type = "string"
202                     required = true
203                 }
204             }
205
206             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
207             assertTrue(result, "get AAI string Rest resource assignment failed")
208             assertEquals(
209                 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
210                 "get AAI string Rest resource assignment failed"
211             )
212             val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
213             println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.asText()}")
214             assertEquals(
215                 getExpectedJsonResponse(resourceAssignment.name).asText(),
216                 value.asText(),
217                 "get Rest resource assignment failed - enexpected value"
218             )
219         }
220     }
221
222     @Test
223     fun `test rest resource resolution aai get json`() {
224         runBlocking {
225             val resourceAssignment = ResourceAssignment().apply {
226                 name = "generic-vnf"
227                 dictionaryName = "aai-get-json-resource"
228                 dictionarySource = "aai-data"
229                 property = PropertyDefinition().apply {
230                     type = "json"
231                     required = true
232                 }
233             }
234
235             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
236             assertTrue(result, "get AAI json Rest resource assignment failed")
237             assertEquals(
238                 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
239                 "get AAI json Rest resource assignment failed"
240             )
241             val value = restResourceResolutionProcessor.raRuntimeService.getResolutionStore(resourceAssignment.name)
242             println("Resolution result: $result, status: ${resourceAssignment.status}, value: ${value.toPrettyString()}")
243             assertEquals(
244                 getExpectedJsonResponse().toPrettyString(),
245                 value.toPrettyString(),
246                 "get Rest resource assignment failed - enexpected value"
247             )
248         }
249     }
250
251     @Test
252     fun `test rest resource resolution aai put`() {
253         runBlocking {
254             val resourceAssignment = ResourceAssignment().apply {
255                 name = "rr-aai"
256                 dictionaryName = "aai-put-resource"
257                 dictionarySource = "aai-data"
258                 property = PropertyDefinition().apply {
259                     type = "string"
260                 }
261             }
262
263             val result = restResourceResolutionProcessor.applyNB(resourceAssignment)
264             assertTrue(result, "put AAI Rest resource assignment failed")
265             assertEquals(
266                 resourceAssignment.status, BluePrintConstants.STATUS_SUCCESS,
267                 "put AAI json Rest resource assignment failed"
268             )
269             println("Resolution result: $result, status: ${resourceAssignment.status}")
270         }
271     }
272 }