Use PollingConditions to improve intermittent test failure
[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 org.onap.cps.spi.model.DataNodeBuilder
25 import spock.lang.Shared
26 import spock.lang.Specification
27
28 class CpsDeltaServiceImplSpec extends Specification{
29
30     def objectUnderTest = new CpsDeltaServiceImpl()
31
32     @Shared
33     def dataNodeWithLeafAndChildDataNode = [new DataNodeBuilder().withXpath('/parent').withLeaves(['parent-leaf': 'parent-payload'])
34                             .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").withLeaves('child-leaf': 'child-payload').build()]).build()]
35     @Shared
36     def dataNodeWithChildDataNode = [new DataNodeBuilder().withXpath('/parent').withLeaves(['parent-leaf': 'parent-payload'])
37                                              .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()]
38     @Shared
39     def emptyDataNode = [new DataNodeBuilder().withXpath('/parent').build()]
40
41     def 'Get delta between data nodes for removed data where source data node has #scenario'() {
42         when: 'attempt to get delta between 2 data nodes'
43             def result = objectUnderTest.getDeltaReports(sourceDataNode as Collection<DataNode>, emptyDataNode)
44         then: 'the delta report contains "remove" action with right data'
45             assert result.first().action.equals("remove")
46             assert result.first().xpath == "/parent/child"
47             assert result.first().sourceData == expectedSourceData
48         where: 'following data was used'
49             scenario       | sourceDataNode                   || expectedSourceData
50             'leaf data'    | dataNodeWithLeafAndChildDataNode || ['child-leaf': 'child-payload']
51             'no leaf data' | dataNodeWithChildDataNode        || null
52     }
53
54     def 'Get delta between data nodes with new data where target data node has #scenario'() {
55         when: 'attempt to get delta between 2 data nodes'
56             def result = objectUnderTest.getDeltaReports(emptyDataNode, targetDataNode)
57         then: 'the delta report contains "add" action with right data'
58             assert result.first().action.equals("add")
59             assert result.first().xpath == "/parent/child"
60             assert result.first().targetData == expectedTargetData
61         where: 'following data was used'
62             scenario       | targetDataNode                   || expectedTargetData
63             'leaf data'    | dataNodeWithLeafAndChildDataNode || ['child-leaf': 'child-payload']
64             'no leaf data' | dataNodeWithChildDataNode        || null
65     }
66 }