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
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.ncmp.impl.inventory.sync.lcm
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
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']
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
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']
53 def targetCmHandle = new NcmpServiceCmHandle(cmHandleId: 'ch',
54 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
55 publicProperties: ['prop1': 'value1']
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
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),
71 def targetCmHandle = new NcmpServiceCmHandle(
72 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
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
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),
88 def targetCmHandle = new NcmpServiceCmHandle(
89 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
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
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']
105 def targetCmHandle = new NcmpServiceCmHandle(
106 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
107 publicProperties: ['prop1': 'new value', 'prop3': 'new', 'prop4': 'unchanged']
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')
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']
127 def targetCmHandle = new NcmpServiceCmHandle(
128 compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: CmHandleState.READY),
129 publicProperties: ['prop1': 'newValue1']
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'
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']
148 def targetCmHandle = new NcmpServiceCmHandle(cmHandleId: 'ch',
149 compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.DELETED),
150 publicProperties: ['prop1': 'value1']
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