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