Merge "CPS Delta API: Update action for delta service"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDeltaServiceImplSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl
22
23 import org.onap.cps.spi.model.DataNode
24 import spock.lang.Shared
25 import spock.lang.Specification
26
27 class CpsDeltaServiceImplSpec extends Specification{
28
29     def objectUnderTest = new CpsDeltaServiceImpl()
30
31
32     static def sourceDataNodeWithLeafData = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-in-source'])]
33     static def sourceDataNodeWithoutLeafData = [new DataNode(xpath: '/parent')]
34     static def targetDataNodeWithLeafData = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-in-target'])]
35     static def targetDataNodeWithoutLeafData = [new DataNode(xpath: '/parent')]
36     static def sourceDataNodeWithMultipleLeaves = [new DataNode(xpath: '/parent', leaves: ['leaf-1': 'leaf-1-in-source', 'leaf-2': 'leaf-2-in-source'])]
37     static def targetDataNodeWithMultipleLeaves = [new DataNode(xpath: '/parent', leaves: ['leaf-1': 'leaf-1-in-target', 'leaf-2': 'leaf-2-in-target'])]
38
39     def 'Get delta between data nodes for REMOVED data where source data node has #scenario'() {
40         when: 'attempt to get delta between 2 data nodes'
41             def result = objectUnderTest.getDeltaReports(sourceDataNodeWithLeafData, [])
42         then: 'the delta report contains expected "remove" action'
43             assert result[0].action.equals('remove')
44         and : 'the delta report contains the expected xpath'
45             assert result[0].xpath == '/parent'
46         and: 'the delta report contains expected source data'
47             assert result[0].sourceData == ['parent-leaf': 'parent-payload-in-source']
48         and: 'the delta report contains no target data'
49             assert  result[0].targetData == null
50     }
51
52     def 'Get delta between data nodes with ADDED data where target data node has #scenario'() {
53         when: 'attempt to get delta between 2 data nodes'
54             def result = objectUnderTest.getDeltaReports([], targetDataNodeWithLeafData)
55         then: 'the delta report contains expected "add" action'
56             assert result[0].action.equals('add')
57         and: 'the delta report contains expected xpath'
58             assert result[0].xpath == '/parent'
59         and: 'the delta report contains no source data'
60             assert result[0].sourceData == null
61         and: 'the delta report contains expected target data'
62             assert result[0].targetData == ['parent-leaf': 'parent-payload-in-target']
63     }
64
65     def 'Delta Report between leaves for parent and child nodes, #scenario'() {
66         given: 'Two data nodes'
67             def sourceDataNode  = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload'], childDataNodes: [new DataNode(xpath: '/parent/child', leaves: ['child-leaf': 'child-payload'])])]
68             def targetDataNode  = [new DataNode(xpath: '/parent', leaves: ['parent-leaf': 'parent-payload-updated'], childDataNodes: [new DataNode(xpath: '/parent/child', leaves: ['child-leaf': 'child-payload-updated'])])]
69         when: 'attempt to get delta between 2 data nodes'
70             def result = objectUnderTest.getDeltaReports(sourceDataNode, targetDataNode)
71         then: 'the delta report contains expected "update" action'
72             assert result[index].action.equals('update')
73         and: 'the delta report contains expected xpath'
74             assert result[index].xpath == expectedXpath
75         and: 'the delta report contains expected source and target data'
76             assert result[index].sourceData == expectedSourceData
77             assert result[index].targetData == expectedTargetData
78         where: 'the following data was used'
79             scenario           | index || expectedXpath   | expectedSourceData                | expectedTargetData
80             'parent data node' | 0     || '/parent'       | ['parent-leaf': 'parent-payload'] | ['parent-leaf': 'parent-payload-updated']
81             'child data node'  | 1     || '/parent/child' | ['child-leaf': 'child-payload']   | ['child-leaf': 'child-payload-updated']
82     }
83
84     def 'Delta report between leaves, #scenario'() {
85         when: 'attempt to get delta between 2 data nodes'
86             def result = objectUnderTest.getDeltaReports(sourceDataNode, targetDataNode)
87         then: 'the delta report contains expected "update" action'
88             assert result[0].action.equals('update')
89         and: 'the delta report contains expected xpath'
90             assert result[0].xpath == '/parent'
91         and: 'the delta report contains expected source and target data'
92             assert result[0].sourceData == expectedSourceData
93             assert result[0].targetData == expectedTargetData
94         where: 'the following data was used'
95             scenario                                           | sourceDataNode                   | targetDataNode                   || expectedSourceData                                           | expectedTargetData
96             'source and target data nodes have leaves'         | sourceDataNodeWithLeafData       | targetDataNodeWithLeafData       || ['parent-leaf': 'parent-payload-in-source']                  | ['parent-leaf': 'parent-payload-in-target']
97             'only source data node has leaves'                 | sourceDataNodeWithLeafData       | targetDataNodeWithoutLeafData    || ['parent-leaf': 'parent-payload-in-source']                  | null
98             'only target data node has leaves'                 | sourceDataNodeWithoutLeafData    | targetDataNodeWithLeafData       || null                                                         | ['parent-leaf': 'parent-payload-in-target']
99             '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     }
101
102     def 'Get delta between data nodes for updated data, where source and target data nodes have no leaves '() {
103         when: 'attempt to get delta between 2 data nodes'
104             def result = objectUnderTest.getDeltaReports(sourceDataNodeWithoutLeafData, targetDataNodeWithoutLeafData)
105         then: 'the delta report contains "update" action with right data'
106             assert result.isEmpty()
107     }
108 }