Modify lcm events to include dataProducerIdentifier and moduleSetTag (CPS-1964 3)
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / lcm / LcmEventsCreatorSpec.groovy
1 /*
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
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.api.impl.events.lcm
22
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
26
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
33
34 class LcmEventsCreatorSpec extends Specification {
35
36     LcmEventHeaderMapper lcmEventsHeaderMapper = Mappers.getMapper(LcmEventHeaderMapper)
37
38     def objectUnderTest = new LcmEventsCreator(lcmEventsHeaderMapper)
39     def cmHandleId = 'test-cm-handle'
40
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
66     }
67
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
80     }
81
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
100     }
101
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
118     }
119
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
146
147     }
148
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
163
164     }
165
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
175     }
176
177     def 'Map the LcmEvent for alternate ID, data producer identifier, and module set tag when they contain #scenario'() {
178         given: 'NCMP cm handle details with current and old values for alternate ID, module set tag, and data producer identifier'
179             def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: existingAlternateId, moduleSetTag: existingModuleSetTag, dataProducerIdentifier: existingDataProducerIdentifier, compositeState: new CompositeState(dataSyncEnabled: false))
180             def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, alternateId: targetAlternateId, moduleSetTag: targetModuleSetTag, dataProducerIdentifier: targetDataProducerIdentifier, compositeState: new CompositeState(dataSyncEnabled: false))
181         when: 'the event is populated'
182             def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
183         then: 'the alternate ID, module set tag, and data producer identifier are present or are an empty string in the payload'
184             assert result.event.alternateId == targetAlternateId
185             assert result.event.moduleSetTag == targetModuleSetTag
186             assert result.event.dataProducerIdentifier == targetDataProducerIdentifier
187         where: 'the following values are provided for the alternate ID, module set tag, and data producer identifier'
188             scenario                                     | existingAlternateId | targetAlternateId | existingModuleSetTag | targetModuleSetTag | existingDataProducerIdentifier | targetDataProducerIdentifier
189             'same target and existing values'            | 'someAlternateId'   | 'someAlternateId' | 'someModuleSetTag'   | 'someModuleSetTag' | 'someDataProducerIdentifier'   | 'someDataProducerIdentifier'
190             'blank target and existing values'           | ''                  | ''                | ''                   | ''                 | ''                             | ''
191             'new target value and blank existing values' | ''                  | 'someAlternateId' | ''                   | 'someAlternateId'  | ''                             | 'someDataProducerIdentifier'
192     }
193 }