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.api.impl.events.lcm
23 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.ADVISED
24 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.DELETING
25 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.READY
27 import org.mapstruct.factory.Mappers
28 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
29 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
30 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
31 import org.onap.cps.ncmp.events.lcm.v1.Values
32 import spock.lang.Specification
34 class LcmEventsCreatorSpec extends Specification {
36 LcmEventHeaderMapper lcmEventsHeaderMapper = Mappers.getMapper(LcmEventHeaderMapper)
38 def objectUnderTest = new LcmEventsCreator(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 existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: existingCmHandleState),
44 publicProperties: existingPublicProperties)
45 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: targetCmHandleState),
46 publicProperties: targetPublicProperties)
47 when: 'the event is populated'
48 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
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 == expectedExistingCmHandleState
57 assert result.event.oldValues.cmHandleProperties == [expectedExistingPublicProperties]
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 | existingCmHandleState | targetCmHandleState | existingPublicProperties | targetPublicProperties || expectedExistingPublicProperties | expectedTargetPublicProperties | expectedExistingCmHandleState | 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 existingNcmpServiceCmHandle = 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 event is populated'
76 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
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 existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value2'])
87 when: 'the event is populated'
88 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmhandle, existingNcmpServiceCmHandle)
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 existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: DELETING),
107 publicProperties: ['publicProperty1': 'value1'])
108 when: 'the event is populated'
109 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
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 existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: existingDataSyncEnableFlag, cmHandleState: ADVISED))
123 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
124 when: 'the event is populated'
125 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
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 == existingDataSyncEnableFlag
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 | existingDataSyncEnableFlag | 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
149 def 'Map the LcmEvent for datasync flag for same transition from #operation'() {
150 given: 'NCMP cm handle details with current and old details'
151 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: existingDataSyncEnableFlag, cmHandleState: ADVISED))
152 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
153 when: 'the event is populated'
154 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
155 then: 'the data sync flag is not present in the event'
156 assert result.event.oldValues.dataSyncEnabled == null
157 assert result.event.newValues.dataSyncEnabled == null
158 where: 'following parameters are provided'
159 operation | existingDataSyncEnableFlag | targetDataSyncEnableFlag
160 'false to false' | false | false
161 'true to true' | true | true
162 'null to null' | null | null
166 def 'Map the LcmEventHeader'() {
167 given: 'NCMP cm handle details with current and old details'
168 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(cmHandleState: ADVISED))
169 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(cmHandleState: READY))
170 when: 'the event header is populated'
171 def result = objectUnderTest.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
172 then: 'the header has fields populated'
173 assert result.eventCorrelationId == cmHandleId
174 assert result.eventId != null
177 def 'Map the LcmEvent for alternate IDs when #scenario'() {
178 given: 'NCMP cm handle details with current and old alternate IDs'
179 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: existingAlternateId, compositeState: new CompositeState(dataSyncEnabled: false))
180 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: targetAlternateId, compositeState: new CompositeState(dataSyncEnabled: false))
181 when: 'the event is populated'
182 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
183 then: 'the alternate ID is present or is an empty string in the payload'
184 assert result.event.alternateId == expectedEventAlternateId
185 where: 'the following alternate IDs are provided'
186 scenario | existingAlternateId | targetAlternateId || expectedEventAlternateId
187 'same new and old alternate ID' | 'someAlternateId' | 'someAlternateId' || 'someAlternateId'
188 'blank new and old alternate ID' | '' | '' || ''
189 'new alternate id and blank old alternate ID' | '' | 'someAlternateId' || 'someAlternateId'