d711305d8fe62db425d4b13b7083fffe401067c5
[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 PayloadFactorySpec extends Specification {
30     def 'Create payload for create operation.'() {
31         given: 'a new cm handle'
32             def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'ch',
33                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
34                 publicProperties: ['prop1': 'value1']
35             )
36         when: 'payload is created for create'
37             def result = PayloadFactory.createPayloadV1(LcmEventType.CREATE, null, ncmpServiceCmHandle)
38         then: 'new values are populated'
39             assert result.cmHandleId == 'ch'
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 'Create payload 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(cmHandleId: 'ch',
54                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
55                 publicProperties: ['prop1': 'value1']
56             )
57         when: 'payload is created'
58             def result = PayloadFactory.createPayloadV1(LcmEventType.UPDATE, currentCmHandle, targetCmHandle)
59         then: 'no updates are detected'
60             assert result.cmHandleId == 'ch'
61             assert result.oldValues == null
62             assert result.newValues == null
63     }
64
65     def 'Create payload when data sync flag changes.'() {
66         given: 'current and target cm handles with different data sync flags'
67             def currentCmHandle = new NcmpServiceCmHandle(
68                 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.READY),
69                 publicProperties: [:]
70             )
71             def targetCmHandle = new NcmpServiceCmHandle(
72                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
73                 publicProperties: [:]
74             )
75         when: 'payload is created'
76             def result = PayloadFactory.createPayloadV1(LcmEventType.UPDATE,currentCmHandle, targetCmHandle)
77         then: 'data sync flag change is detected'
78             assert result.oldValues.dataSyncEnabled == false
79             assert result.newValues.dataSyncEnabled == true
80     }
81
82     def 'Create payload when cm handle state changes.'() {
83         given: 'current and target cm handles with different states'
84             def currentCmHandle = new NcmpServiceCmHandle(
85                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.ADVISED),
86                 publicProperties: [:]
87             )
88             def targetCmHandle = new NcmpServiceCmHandle(
89                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
90                 publicProperties: [:]
91             )
92         when: 'payload is created'
93             def result = PayloadFactory.createPayloadV1(LcmEventType.UPDATE,currentCmHandle, targetCmHandle)
94         then: 'state change is detected'
95             assert result.oldValues.cmHandleState == Values.CmHandleState.ADVISED
96             assert result.newValues.cmHandleState == Values.CmHandleState.READY
97     }
98
99     def 'Create payload when public properties change.'() {
100         given: 'current and target cm handles with different properties'
101             def currentCmHandle = new NcmpServiceCmHandle(
102                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
103                 publicProperties: ['prop1': 'old value', 'prop2': 'to be deleted', 'prop4': 'unchanged']
104             )
105             def targetCmHandle = new NcmpServiceCmHandle(
106                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
107                 publicProperties: ['prop1': 'new value', 'prop3': 'new', 'prop4': 'unchanged']
108             )
109         when: 'payload is created'
110             def result = PayloadFactory.createPayloadV1(LcmEventType.UPDATE,currentCmHandle, targetCmHandle)
111         then: 'property changes are detected'
112             assert result.oldValues.cmHandleProperties[0]['prop1'] == 'old value'
113             assert result.oldValues.cmHandleProperties[0]['prop2'] == 'to be deleted'
114             assert result.newValues.cmHandleProperties[0]['prop1'] == 'new value'
115             assert result.newValues.cmHandleProperties[0]['prop3'] == 'new'
116         and: 'unchanged property is not included in the result'
117             assert !result.oldValues.cmHandleProperties[0].containsKey('prop4')
118             assert !result.newValues.cmHandleProperties[0].containsKey('prop4')
119     }
120
121     def 'Create payload when multiple changes occur.'() {
122         given: 'current and target cm handles with multiple differences'
123             def currentCmHandle = new NcmpServiceCmHandle(
124                 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.ADVISED),
125                 publicProperties: ['prop1': 'value1']
126             )
127             def targetCmHandle = new NcmpServiceCmHandle(
128                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
129                 publicProperties: ['prop1': 'newValue1']
130             )
131         when: 'payload is created'
132             def result = PayloadFactory.createPayloadV1(LcmEventType.UPDATE,currentCmHandle, targetCmHandle)
133         then: 'all changes are detected'
134             assert result.oldValues.dataSyncEnabled == false
135             assert result.newValues.dataSyncEnabled == true
136             assert result.oldValues.cmHandleState == Values.CmHandleState.ADVISED
137             assert result.newValues.cmHandleState == Values.CmHandleState.READY
138             assert result.oldValues.cmHandleProperties[0]['prop1'] == 'value1'
139             assert result.newValues.cmHandleProperties[0]['prop1'] == 'newValue1'
140     }
141
142     def 'Create payload for delete operation.'() {
143         given: 'a cm handle being deleted'
144             def currentCmHandle = new NcmpServiceCmHandle(
145                 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
146                 publicProperties: ['prop1': 'value1']
147             )
148             def targetCmHandle = new NcmpServiceCmHandle(cmHandleId: 'ch',
149                 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.DELETED),
150                 publicProperties: ['prop1': 'value1']
151             )
152         when: 'payload is created for delete'
153             def result = PayloadFactory.createPayloadV1(LcmEventType.DELETE, currentCmHandle, targetCmHandle)
154         then: 'cmHandleId is populated'
155             assert result.cmHandleId == 'ch'
156         and: 'no values are populated'
157             assert result.oldValues == null
158             assert result.newValues == null
159     }
160 }