2 * Copyright © 2019 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.config.snapshots
19 import com.fasterxml.jackson.databind.JsonNode
20 import kotlinx.coroutines.runBlocking
21 import org.junit.Assert.assertEquals
22 import org.junit.Assert.assertTrue
23 import org.junit.Before
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_JSON
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_XML
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_DIFF
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_FETCH
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_STORE
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshot
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshotService
34 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
35 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.context.annotation.ComponentScan
40 import org.springframework.test.context.ContextConfiguration
41 import org.springframework.test.context.TestPropertySource
42 import org.springframework.test.context.junit4.SpringRunner
44 @RunWith(SpringRunner::class)
45 @ContextConfiguration(
46 classes = [ResourceConfigSnapshotService::class, TestDatabaseConfiguration::class]
48 @TestPropertySource(locations = ["classpath:application-test.properties"])
50 basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots"]
52 @Suppress("SameParameterValue")
53 class ComponentConfigSnapshotsExecutorTest {
56 lateinit var cfgSnapshotService: ResourceConfigSnapshotService
57 lateinit var cfgSnapshotComponent: ComponentConfigSnapshotsExecutor
58 private var bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
60 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
63 private val resourceId = "1"
64 private val resourceType = "ServiceInstance"
65 private val props = mutableMapOf<String, JsonNode>()
66 private val nodeTemplateName = "nodeTemplateName"
68 private val executionRequest = ExecutionServiceInput()
72 cfgSnapshotComponent = ComponentConfigSnapshotsExecutor(cfgSnapshotService)
73 props[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
74 props[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
76 cfgSnapshotComponent.operationInputs = props
77 cfgSnapshotComponent.bluePrintRuntimeService = bluePrintRuntimeService
78 cfgSnapshotComponent.nodeTemplateName = nodeTemplateName
80 cfgSnapshotComponent.executionServiceInput = executionRequest
81 cfgSnapshotComponent.processId = "12"
82 cfgSnapshotComponent.workflowName = "workflow"
83 cfgSnapshotComponent.stepName = "step"
84 cfgSnapshotComponent.interfaceName = "interfaceName"
85 cfgSnapshotComponent.operationName = "operationName"
89 fun processNBFetchWithResourceIdAndResourceTypeSingleFind() {
90 val snapshot = ResourceConfigSnapshot()
91 val snapshotConfig = "TEST1"
92 snapshot.config_snapshot = snapshotConfig
98 cfgSnapshotService.write(snapshotConfig, resId, resType)
99 prepareRequestProperties(OPERATION_FETCH, resId, resType, ResourceConfigSnapshot.Status.RUNNING.name)
101 cfgSnapshotComponent.processNB(executionRequest)
102 } catch (e: BluePrintProcessorException) {
103 kotlin.test.assertEquals(
104 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
109 // then; we should get success and the TEST1 payload in our output properties
111 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
112 bluePrintRuntimeService.getNodeTemplateAttributeValue(
114 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
118 snapshotConfig.asJsonPrimitive(),
119 bluePrintRuntimeService.getNodeTemplateAttributeValue(
121 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
128 fun processNBFetchCandidateWithResourceIdAndResourceTypeSingleFind() {
129 val snapshot = ResourceConfigSnapshot()
130 val snapshotConfig = "TEST"
131 snapshot.config_snapshot = snapshotConfig
137 cfgSnapshotService.write(snapshotConfig, resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
138 prepareRequestProperties(OPERATION_FETCH, resId, resType, ResourceConfigSnapshot.Status.CANDIDATE.name)
140 cfgSnapshotComponent.processNB(executionRequest)
141 } catch (e: BluePrintProcessorException) {
142 kotlin.test.assertEquals(
143 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
148 // then; we should get success and the TEST payload in our output properties
150 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
151 bluePrintRuntimeService.getNodeTemplateAttributeValue(
153 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
157 snapshotConfig.asJsonPrimitive(),
158 bluePrintRuntimeService.getNodeTemplateAttributeValue(
160 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
167 fun processNBStoreWithResourceIdAndResourceType() {
168 val snapshot = ResourceConfigSnapshot()
169 val snapshotConfig = "PAYLOAD"
170 snapshot.config_snapshot = snapshotConfig
176 prepareRequestProperties(OPERATION_STORE, resId, resType, snapshotConfig)
178 cfgSnapshotComponent.processNB(executionRequest)
179 } catch (e: BluePrintProcessorException) {
180 kotlin.test.assertEquals(
181 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
187 // then; we should get success and the PAYLOAD payload in our output properties
189 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
190 bluePrintRuntimeService.getNodeTemplateAttributeValue(
192 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
196 snapshotConfig.asJsonPrimitive(),
197 bluePrintRuntimeService.getNodeTemplateAttributeValue(
199 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
206 fun processNBFetchNoneFound() {
209 // when; asking for unknown resource Id/ resource Type combo; should get an error response
211 prepareRequestProperties(OPERATION_FETCH, "asdasd", "PNF", ResourceConfigSnapshot.Status.RUNNING.name)
213 cfgSnapshotComponent.processNB(executionRequest)
214 } catch (e: BluePrintProcessorException) {
215 kotlin.test.assertEquals(
216 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
223 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
224 bluePrintRuntimeService.getNodeTemplateAttributeValue(
226 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
233 fun processNBErrorOperationUnknown() {
236 // when; asking for unknown operation update; should get an error response
238 prepareRequestProperties("update", "asdasd", "PNF", ResourceConfigSnapshot.Status.RUNNING.name)
240 cfgSnapshotComponent.processNB(executionRequest)
241 } catch (e: BluePrintProcessorException) {
242 kotlin.test.assertEquals(
243 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
249 // then; we should get error in our output properties
250 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 1)
252 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_ERROR.asJsonPrimitive(),
253 bluePrintRuntimeService.getNodeTemplateAttributeValue(
255 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
258 val msg = "Operation parameter must be fetch, store or diff"
260 msg.asJsonPrimitive(),
261 bluePrintRuntimeService.getNodeTemplateAttributeValue(
263 ComponentConfigSnapshotsExecutor.OUTPUT_MESSAGE
270 fun processNBErrorDiffContentTypeUnknown() {
273 // when; asking for unknown content type diff operation; should get an error response
277 cfgSnapshotService.write("snapshotConfig", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
278 prepareRequestProperties(OPERATION_DIFF, resId, resType, "YANG")
280 cfgSnapshotComponent.processNB(executionRequest)
281 } catch (e: BluePrintProcessorException) {
282 kotlin.test.assertEquals(
283 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
289 // then; we should get error in our output properties
291 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_ERROR.asJsonPrimitive(),
292 bluePrintRuntimeService.getNodeTemplateAttributeValue(
294 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
297 val message = "Could not compare config snapshots for type YANG"
299 message.asJsonPrimitive(),
300 bluePrintRuntimeService.getNodeTemplateAttributeValue(
302 ComponentConfigSnapshotsExecutor.OUTPUT_MESSAGE
309 fun processNBCompareTwoJsonConfigSnapshots() {
313 // when; comparing RUNNING vs CANDIDATE json configs; should get an success response; with differences
317 preparePayload("config-payload-running.json", resId, resType, ResourceConfigSnapshot.Status.RUNNING)
318 preparePayload("config-payload-candidate.json", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
320 prepareRequestProperties(OPERATION_DIFF, resId, resType, DIFF_JSON)
321 cfgSnapshotComponent.processNB(executionRequest)
322 } catch (e: BluePrintProcessorException) {
323 kotlin.test.assertEquals(
324 "Can't proceed with the cfg snapshot diff: provide resource-id and resource-type.",
330 // then; we should get success
331 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 0)
333 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
334 bluePrintRuntimeService.getNodeTemplateAttributeValue(
336 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
340 // then; we should get JSON-patches differences in our response property
342 "[{\"op\":\"add\",\"path\":\"/system-uptime-information/last-configured-time/new-child-object\",\"value\":{\"property\":\"value\"}}," +
343 "{\"op\":\"replace\",\"path\":\"/system-uptime-information/system-booted-time/time-length\",\"value\":\"14:52:54\"}," +
344 "{\"op\":\"replace\",\"path\":\"/system-uptime-information/time-source\",\"value\":\" DNS CLOCK \"}," +
345 "{\"op\":\"add\",\"path\":\"/system-uptime-information/uptime-information/load-average-10\",\"value\":\"0.05\"}]"
347 diffJson.asJsonPrimitive(),
348 bluePrintRuntimeService.getNodeTemplateAttributeValue(
350 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
357 fun processNBCompareTwoXmlConfigSnapshots() {
361 // when; comparing RUNNING vs CANDIDATE xml configs; should get an success response; with differences
365 preparePayload("interface-running.xml", resId, resType, ResourceConfigSnapshot.Status.RUNNING)
366 preparePayload("interface-candidate.xml", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
368 prepareRequestProperties(OPERATION_DIFF, resId, resType, DIFF_XML)
370 cfgSnapshotComponent.processNB(executionRequest)
371 } catch (e: BluePrintProcessorException) {
372 kotlin.test.assertEquals(
373 "Can't proceed with the cfg snapshot diff: provide resource-id and resource-type.",
379 // then; we should get success
380 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 0)
382 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
383 bluePrintRuntimeService.getNodeTemplateAttributeValue(
385 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
389 // then; we should get XML-patches differences in our response property
390 val diffXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
392 "<replace sel=\"/output[1]/interface-information[1]/interface-flapped[1]/@seconds\">2343</replace>" +
393 "<replace sel=\"/output[1]/interface-information[1]/interface-flapped[1]/text()[1]\">34</replace>" +
394 "<replace sel=\"/output[1]/interface-information[1]/traffic-statistics[1]/input-packets[1]/text()[1]\">09098789</replace>" +
395 "<replace sel=\"/output[1]/interface-information[1]/traffic-statistics[1]/output-packets[1]/text()[1]\">2828828</replace>" +
396 "<add sel=\"/output[1]/interface-information[1]/physical-interface[1]\"><interface-name>TEGig400-int01</interface-name></add>" +
399 diffXml.asJsonPrimitive(),
400 bluePrintRuntimeService.getNodeTemplateAttributeValue(
402 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
408 private fun preparePayload(
412 status: ResourceConfigSnapshot.Status
415 cfgSnapshotService.write(
416 JacksonUtils.getClassPathFileContent("payload/requests/$filename"),
424 private fun prepareRequestProperties(oper: String, resId: String, resType: String, optional: String = "") {
425 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_OPERATION] = oper.asJsonPrimitive()
426 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] =
427 resId.asJsonPrimitive()
428 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] =
429 resType.asJsonPrimitive()
432 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] =
434 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] =
436 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] =
437 ResourceConfigSnapshot.Status.RUNNING.name.asJsonPrimitive()
441 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] =
442 optional.asJsonPrimitive()
444 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] =
445 optional.asJsonPrimitive()
447 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] =
448 optional.asJsonPrimitive()