20e7b9596aa11d9b210a59147fc5de599c4c2930
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2026 OpenInfra Foundation Europe. All rights reserved.
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.ncmp.impl.inventory.sync.lcm
22
23 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
24 import org.onap.cps.ncmp.api.inventory.models.CompositeState
25 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
26 import org.onap.cps.ncmp.events.lcm.Values
27 import spock.lang.Specification
28
29 class CmHandlePropertyChangeDetectorSpec extends Specification {
30
31     def 'Determine updates for create operation.'() {
32         given: 'a new cm handle'
33             def ncmpServiceCmHandle = new NcmpServiceCmHandle(
34                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
35                 publicProperties: ['prop1': 'value1']
36             )
37         when: 'updates are determined for create'
38             def result = CmHandlePropertyChangeDetector.determineUpdatesForCreate(ncmpServiceCmHandle)
39         then: 'new values are populated'
40             assert result.newValues.dataSyncEnabled == true
41             assert result.newValues.cmHandleState == Values.CmHandleState.READY
42             assert result.newValues.cmHandleProperties == [['prop1': 'value1']]
43         and: 'old values are not set'
44             assert result.oldValues == null
45     }
46
47     def 'Determine updates when no changes detected.'() {
48         given: 'current and target cm handles with same properties'
49             def currentCmHandle = new NcmpServiceCmHandle(
50                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
51                 publicProperties: ['prop1': 'value1']
52             )
53             def targetCmHandle = new NcmpServiceCmHandle(
54                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
55                 publicProperties: ['prop1': 'value1']
56             )
57         when: 'updates are determined'
58             def result = CmHandlePropertyChangeDetector.determineUpdates(currentCmHandle, targetCmHandle)
59         then: 'no updates are detected'
60             assert result.oldValues == null
61             assert result.newValues == null
62     }
63
64     def 'Determine updates when data sync flag changes.'() {
65         given: 'current and target cm handles with different data sync flags'
66             def currentCmHandle = new NcmpServiceCmHandle(
67                 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.READY),
68                 publicProperties: [:]
69             )
70             def targetCmHandle = new NcmpServiceCmHandle(
71                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
72                 publicProperties: [:]
73             )
74         when: 'updates are determined'
75             def result = CmHandlePropertyChangeDetector.determineUpdates(currentCmHandle, targetCmHandle)
76         then: 'data sync flag change is detected'
77             assert result.oldValues.dataSyncEnabled == false
78             assert result.newValues.dataSyncEnabled == true
79     }
80
81     def 'Determine updates when cm handle state changes.'() {
82         given: 'current and target cm handles with different states'
83             def currentCmHandle = new NcmpServiceCmHandle(
84                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.ADVISED),
85                 publicProperties: [:]
86             )
87             def targetCmHandle = new NcmpServiceCmHandle(
88                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
89                 publicProperties: [:]
90             )
91         when: 'updates are determined'
92             def result = CmHandlePropertyChangeDetector.determineUpdates(currentCmHandle, targetCmHandle)
93         then: 'state change is detected'
94             assert result.oldValues.cmHandleState == Values.CmHandleState.ADVISED
95             assert result.newValues.cmHandleState == Values.CmHandleState.READY
96     }
97
98     def 'Determine updates when public properties change.'() {
99         given: 'current and target cm handles with different properties'
100             def currentCmHandle = new NcmpServiceCmHandle(
101                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
102                 publicProperties: ['prop1': 'value1', 'prop2': 'value2', 'unchanged': 'sameValue']
103             )
104             def targetCmHandle = new NcmpServiceCmHandle(
105                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
106                 publicProperties: ['prop1': 'newValue1', 'prop3': 'value3', 'unchanged': 'sameValue']
107             )
108         when: 'updates are determined'
109             def result = CmHandlePropertyChangeDetector.determineUpdates(currentCmHandle, targetCmHandle)
110         then: 'property changes are detected'
111             assert result.oldValues.cmHandleProperties[0]['prop1'] == 'value1'
112             assert result.oldValues.cmHandleProperties[0]['prop2'] == 'value2'
113             assert result.newValues.cmHandleProperties[0]['prop1'] == 'newValue1'
114             assert result.newValues.cmHandleProperties[0]['prop3'] == 'value3'
115         and: 'unchanged property is not included in the result'
116             assert !result.oldValues.cmHandleProperties[0].containsKey('unchanged')
117             assert !result.newValues.cmHandleProperties[0].containsKey('unchanged')
118     }
119
120     def 'Determine updates when multiple changes occur.'() {
121         given: 'current and target cm handles with multiple differences'
122             def currentCmHandle = new NcmpServiceCmHandle(
123                 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.ADVISED),
124                 publicProperties: ['prop1': 'value1']
125             )
126             def targetCmHandle = new NcmpServiceCmHandle(
127                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
128                 publicProperties: ['prop1': 'newValue1']
129             )
130         when: 'updates are determined'
131             def result = CmHandlePropertyChangeDetector.determineUpdates(currentCmHandle, targetCmHandle)
132         then: 'all changes are detected'
133             assert result.oldValues.dataSyncEnabled == false
134             assert result.newValues.dataSyncEnabled == true
135             assert result.oldValues.cmHandleState == Values.CmHandleState.ADVISED
136             assert result.newValues.cmHandleState == Values.CmHandleState.READY
137             assert result.oldValues.cmHandleProperties[0]['prop1'] == 'value1'
138             assert result.newValues.cmHandleProperties[0]['prop1'] == 'newValue1'
139     }
140 }