2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 TechMahindra Ltd.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
23 import org.onap.cps.spi.model.DataNode
24 import spock.lang.Specification
26 class CpsDeltaServiceImplSpec extends Specification{
28 def objectUnderTest = new CpsDeltaServiceImpl()
31 static def sourceDataNodeWithLeafData = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-in-source'])]
32 static def sourceDataNodeWithoutLeafData = [new DataNode(xpath: '/parent')]
33 static def targetDataNodeWithLeafData = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-in-target'])]
34 static def targetDataNodeWithoutLeafData = [new DataNode(xpath: '/parent')]
35 static def sourceDataNodeWithMultipleLeaves = [new DataNode(xpath: '/parent', leaves: ['leaf-1': 'leaf-1-in-source', 'leaf-2': 'leaf-2-in-source'])]
36 static def targetDataNodeWithMultipleLeaves = [new DataNode(xpath: '/parent', leaves: ['leaf-1': 'leaf-1-in-target', 'leaf-2': 'leaf-2-in-target'])]
38 def 'Get delta between data nodes for REMOVED data'() {
39 when: 'attempt to get delta between 2 data nodes'
40 def result = objectUnderTest.getDeltaReports(sourceDataNodeWithLeafData, [])
41 then: 'the delta report contains expected "remove" action'
42 assert result[0].action.equals('remove')
43 and : 'the delta report contains the expected xpath'
44 assert result[0].xpath == '/parent'
45 and: 'the delta report contains expected source data'
46 assert result[0].sourceData == ['parent-leaf': 'parent-payload-in-source']
47 and: 'the delta report contains no target data'
48 assert result[0].targetData == null
51 def 'Get delta between data nodes for ADDED data'() {
52 when: 'attempt to get delta between 2 data nodes'
53 def result = objectUnderTest.getDeltaReports([], targetDataNodeWithLeafData)
54 then: 'the delta report contains expected "add" action'
55 assert result[0].action.equals('add')
56 and: 'the delta report contains expected xpath'
57 assert result[0].xpath == '/parent'
58 and: 'the delta report contains no source data'
59 assert result[0].sourceData == null
60 and: 'the delta report contains expected target data'
61 assert result[0].targetData == ['parent-leaf': 'parent-payload-in-target']
64 def 'Delta Report between leaves for parent and child nodes'() {
65 given: 'Two data nodes'
66 def sourceDataNode = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload'], childDataNodes: [new DataNode(xpath: '/parent/child', leaves: ['child-leaf': 'child-payload'])])]
67 def targetDataNode = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-updated'], childDataNodes: [new DataNode(xpath: '/parent/child', leaves: ['child-leaf': 'child-payload-updated'])])]
68 when: 'attempt to get delta between 2 data nodes'
69 def result = objectUnderTest.getDeltaReports(sourceDataNode, targetDataNode)
70 then: 'the delta report contains expected details for parent node'
71 assert result[0].action.equals('update')
72 assert result[0].xpath == '/parent'
73 assert result[0].sourceData == ['parent-leaf': 'parent-payload']
74 assert result[0].targetData == ['parent-leaf': 'parent-payload-updated']
75 and: 'the delta report contains expected details for child node'
76 assert result[1].action.equals('update')
77 assert result[1].xpath == '/parent/child'
78 assert result[1].sourceData == ['child-leaf': 'child-payload']
79 assert result[1].targetData == ['child-leaf': 'child-payload-updated']
82 def 'Delta report between leaves, #scenario'() {
83 when: 'attempt to get delta between 2 data nodes'
84 def result = objectUnderTest.getDeltaReports(sourceDataNode, targetDataNode)
85 then: 'the delta report contains expected "update" action'
86 assert result[0].action.equals('update')
87 and: 'the delta report contains expected xpath'
88 assert result[0].xpath == '/parent'
89 and: 'the delta report contains expected source and target data'
90 assert result[0].sourceData == expectedSourceData
91 assert result[0].targetData == expectedTargetData
92 where: 'the following data was used'
93 scenario | sourceDataNode | targetDataNode || expectedSourceData | expectedTargetData
94 'source and target data nodes have leaves' | sourceDataNodeWithLeafData | targetDataNodeWithLeafData || ['parent-leaf': 'parent-payload-in-source'] | ['parent-leaf': 'parent-payload-in-target']
95 'only source data node has leaves' | sourceDataNodeWithLeafData | targetDataNodeWithoutLeafData || ['parent-leaf': 'parent-payload-in-source'] | null
96 'only target data node has leaves' | sourceDataNodeWithoutLeafData | targetDataNodeWithLeafData || null | ['parent-leaf': 'parent-payload-in-target']
97 'source and target dsta node with multiple leaves' | sourceDataNodeWithMultipleLeaves | targetDataNodeWithMultipleLeaves || ['leaf-1': 'leaf-1-in-source', 'leaf-2': 'leaf-2-in-source'] | ['leaf-1': 'leaf-1-in-target', 'leaf-2': 'leaf-2-in-target']
100 def 'Get delta between data nodes for updated data, where source and target data nodes have no leaves '() {
101 when: 'attempt to get delta between 2 data nodes'
102 def result = objectUnderTest.getDeltaReports(sourceDataNodeWithoutLeafData, targetDataNodeWithoutLeafData)
103 then: 'the delta report contains "update" action with right data'
104 assert result.isEmpty()