2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation
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.mapstruct.factory.Mappers
24 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
25 import org.onap.cps.ncmp.api.inventory.models.CompositeState
26 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
27 import org.onap.cps.ncmp.events.lcm.v1.Values
28 import spock.lang.Specification
30 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.ADVISED
31 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.DELETING
32 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.READY
34 class LcmEventsProducerHelperSpec extends Specification {
36 LcmEventHeaderMapper lcmEventsHeaderMapper = Mappers.getMapper(LcmEventHeaderMapper)
38 def objectUnderTest = new LcmEventsProducerHelper(lcmEventsHeaderMapper)
39 def cmHandleId = 'test-cm-handle'
41 def 'Map the LcmEvent for #operation'() {
42 given: 'NCMP cm handle details with current and old properties'
43 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: currentCmHandleState),
44 publicProperties: currentPublicProperties)
45 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: targetCmHandleState),
46 publicProperties: targetPublicProperties)
47 when: 'the lcm event is created'
48 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
49 then: 'event header is mapped correctly'
50 assert result.eventSource == 'org.onap.ncmp'
51 assert result.eventCorrelationId == cmHandleId
52 assert result.eventType == LcmEventType.UPDATE.eventType
53 and: 'event payload is mapped correctly with correct cmhandle id'
54 assert result.event.cmHandleId == cmHandleId
55 and: 'it should have correct old state and properties'
56 assert result.event.oldValues.cmHandleState == expectedCurrentCmHandleState
57 assert result.event.oldValues.cmHandleProperties == [expectedCurrentPublicProperties]
58 and: 'the correct new state and properties'
59 assert result.event.newValues.cmHandleProperties == [expectedTargetPublicProperties]
60 assert result.event.newValues.cmHandleState == expectedTargetCmHandleState
61 where: 'following parameters are provided'
62 operation | currentCmHandleState | targetCmHandleState | currentPublicProperties | targetPublicProperties || expectedCurrentPublicProperties | expectedTargetPublicProperties | expectedCurrentCmHandleState | expectedTargetCmHandleState
63 'UPDATE' | ADVISED | READY | ['publicProperty1': 'value1', 'publicProperty2': 'value2'] | ['publicProperty1': 'value11'] || ['publicProperty1': 'value1', 'publicProperty2': 'value2'] | ['publicProperty1': 'value11'] | Values.CmHandleState.ADVISED | Values.CmHandleState.READY
64 'DELETING' | READY | DELETING | ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] || ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] | Values.CmHandleState.READY | Values.CmHandleState.DELETING
65 'CHANGE' | READY | READY | ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] || ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] | null | null
68 def 'Map the LcmEvent for all properties NO CHANGE'() {
69 given: 'NCMP cm handle details without any changes'
70 def publicProperties = ['publicProperty1': 'value3', 'publicProperty2': 'value4']
71 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: READY),
72 publicProperties: publicProperties)
73 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: READY),
74 publicProperties: publicProperties)
75 when: 'the lcm event is created'
76 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
77 then: 'Properties are just the one which are same'
78 assert result.event.oldValues == null
79 assert result.event.newValues == null
82 def 'Map the LcmEvent for operation CREATE'() {
83 given: 'NCMP cm handle details'
84 def targetNcmpServiceCmhandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: READY),
85 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
86 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value2'])
87 when: 'the lcm event is created'
88 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmhandle)
89 then: 'event header is mapped correctly'
90 assert result.eventSource == 'org.onap.ncmp'
91 assert result.eventCorrelationId == cmHandleId
92 assert result.eventType == LcmEventType.CREATE.eventType
93 and: 'event payload is mapped correctly'
94 assert result.event.cmHandleId == cmHandleId
95 assert result.event.newValues.cmHandleState == Values.CmHandleState.READY
96 assert result.event.newValues.dataSyncEnabled == false
97 assert result.event.newValues.cmHandleProperties == [['publicProperty1': 'value11', 'publicProperty2': 'value22']]
98 and: 'it should not have any old values'
99 assert result.event.oldValues == null
102 def 'Map the LcmEvent for DELETE operation'() {
103 given: 'NCMP cm handle details'
104 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.DELETED),
105 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
106 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: DELETING),
107 publicProperties: ['publicProperty1': 'value1'])
108 when: 'the lcm event is created'
109 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
110 then: 'event header is mapped correctly'
111 assert result.eventSource == 'org.onap.ncmp'
112 assert result.eventCorrelationId == cmHandleId
113 assert result.eventType == LcmEventType.DELETE.eventType
114 and: 'event payload is mapped correctly '
115 assert result.event.cmHandleId == cmHandleId
116 assert result.event.oldValues == null
117 assert result.event.newValues == null
120 def 'Map the LcmEvent for datasync flag transition from #operation'() {
121 given: 'NCMP cm handle details with current and old details'
122 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: currentDataSyncEnableFlag, cmHandleState: ADVISED))
123 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
124 when: 'the lcm event is created'
125 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
126 then: 'event header is mapped correctly'
127 assert result.eventSource == 'org.onap.ncmp'
128 assert result.eventCorrelationId == cmHandleId
129 assert result.eventType == LcmEventType.UPDATE.eventType
130 and: 'event payload is mapped correctly with correct cmhandle id'
131 assert result.event.cmHandleId == cmHandleId
132 and: 'it should have correct old values'
133 assert result.event.oldValues.cmHandleState == Values.CmHandleState.ADVISED
134 assert result.event.oldValues.dataSyncEnabled == currentDataSyncEnableFlag
135 and: 'the correct new values'
136 assert result.event.newValues.cmHandleState == Values.CmHandleState.READY
137 assert result.event.newValues.dataSyncEnabled == targetDataSyncEnableFlag
138 where: 'following parameters are provided'
139 operation | currentDataSyncEnableFlag | targetDataSyncEnableFlag
140 'false to true' | false | true
141 'false to null' | false | null
142 'true to false' | true | false
143 'true to null' | true | null
144 'null to true' | null | true
145 'null to false' | null | false
148 def 'Map the LcmEvent for datasync flag for same transition from #operation'() {
149 given: 'NCMP cm handle details with current and old details'
150 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: currentDataSyncEnableFlag, cmHandleState: ADVISED))
151 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
152 when: 'the lcm event is created'
153 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
154 then: 'the data sync flag is not present in the event'
155 assert result.event.oldValues.dataSyncEnabled == null
156 assert result.event.newValues.dataSyncEnabled == null
157 where: 'following parameters are provided'
158 operation | currentDataSyncEnableFlag | targetDataSyncEnableFlag
159 'false to false' | false | false
160 'true to true' | true | true
161 'null to null' | null | null
164 def 'Map the LcmEventHeader'() {
165 given: 'NCMP cm handle details with current and old details'
166 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(cmHandleState: ADVISED))
167 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(cmHandleState: READY))
168 when: 'the lcm event header is created'
169 def result = objectUnderTest.createLcmEventHeader(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
170 then: 'the header field are populated'
171 assert result.eventCorrelationId == cmHandleId
172 assert result.eventId != null
175 def 'Map the LcmEvent for alternate ID, data producer identifier, and module set tag when they contain #scenario'() {
176 given: 'NCMP cm handle details with current and old values for alternate ID, module set tag, and data producer identifier'
177 def currentNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: currentAlternateId, moduleSetTag: currentModuleSetTag, dataProducerIdentifier: currentDataProducerIdentifier, compositeState: new CompositeState(dataSyncEnabled: false))
178 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: targetAlternateId, moduleSetTag: targetModuleSetTag, dataProducerIdentifier: targetDataProducerIdentifier, compositeState: new CompositeState(dataSyncEnabled: false))
179 when: 'the lcm event is created'
180 def result = objectUnderTest.createLcmEvent(cmHandleId, currentNcmpServiceCmHandle, targetNcmpServiceCmHandle)
181 then: 'the alternate ID, module set tag, and data producer identifier are present or are an empty string in the payload'
182 assert result.event.alternateId == targetAlternateId
183 assert result.event.moduleSetTag == targetModuleSetTag
184 assert result.event.dataProducerIdentifier == targetDataProducerIdentifier
185 where: 'the following values are provided for the alternate ID, module set tag, and data producer identifier'
186 scenario | currentAlternateId | targetAlternateId | currentModuleSetTag | targetModuleSetTag | currentDataProducerIdentifier | targetDataProducerIdentifier
187 'same target and current values' | 'myAlternateId' | 'myAlternateId' | 'myModuleSetTag' | 'myModuleSetTag' | 'myDataProducerIdentifier' | 'myDataProducerIdentifier'
188 'blank target and current values' | '' | '' | '' | '' | '' | ''
189 'new target value and blank current values' | '' | 'myAlternateId' | '' | 'myAlternateId' | '' | 'myDataProducerIdentifier'