2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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 org.onap.cps.ncmp.api.inventory.CmHandleState
24 import org.onap.cps.ncmp.api.inventory.CompositeState
25 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
26 import org.onap.ncmp.cmhandle.event.lcm.Values
27 import spock.lang.Specification
29 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED
30 import static org.onap.cps.ncmp.api.inventory.CmHandleState.DELETING
31 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY
33 class LcmEventsCreatorSpec extends Specification {
35 def objectUnderTest = new LcmEventsCreator()
36 def cmHandleId = 'test-cm-handle'
38 def 'Map the LcmEvent for #operation'() {
39 given: 'NCMP cm handle details with current and old properties'
40 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: existingCmHandleState),
41 publicProperties: existingPublicProperties)
42 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: targetCmHandleState),
43 publicProperties: targetPublicProperties)
44 when: 'the event is populated'
45 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
46 then: 'event header is mapped correctly'
47 assert result.eventSource == 'org.onap.ncmp'
48 assert result.eventCorrelationId == cmHandleId
49 assert result.eventType == LcmEventType.UPDATE.eventType
50 and: 'event payload is mapped correctly with correct cmhandle id'
51 assert result.event.cmHandleId == cmHandleId
52 and: 'it should have correct old state and properties'
53 assert result.event.oldValues.cmHandleState == expectedExistingCmHandleState
54 assert result.event.oldValues.cmHandleProperties == [expectedExistingPublicProperties]
55 and: 'the correct new state and properties'
56 assert result.event.newValues.cmHandleProperties == [expectedTargetPublicProperties]
57 assert result.event.newValues.cmHandleState == expectedTargetCmHandleState
58 where: 'following parameters are provided'
59 operation | existingCmHandleState | targetCmHandleState | existingPublicProperties | targetPublicProperties || expectedExistingPublicProperties | expectedTargetPublicProperties | expectedExistingCmHandleState | expectedTargetCmHandleState
60 'UPDATE' | ADVISED | READY | ['publicProperty1': 'value1', 'publicProperty2': 'value2'] | ['publicProperty1': 'value11'] || ['publicProperty1': 'value1', 'publicProperty2': 'value2'] | ['publicProperty1': 'value11'] | Values.CmHandleState.ADVISED | Values.CmHandleState.READY
61 'DELETING' | READY | DELETING | ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] || ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] | Values.CmHandleState.READY | Values.CmHandleState.DELETING
62 'CHANGE' | READY | READY | ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] || ['publicProperty1': 'value3', 'publicProperty2': 'value4'] | ['publicProperty1': 'value33'] | null | null
65 def 'Map the LcmEvent for all properties NO CHANGE'() {
66 given: 'NCMP cm handle details without any changes'
67 def publicProperties = ['publicProperty1': 'value3', 'publicProperty2': 'value4']
68 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: READY),
69 publicProperties: publicProperties)
70 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: READY),
71 publicProperties: publicProperties)
72 when: 'the event is populated'
73 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
74 then: 'Properties are just the one which are same'
75 assert result.event.oldValues == null
76 assert result.event.newValues == null
79 def 'Map the LcmEvent for operation CREATE'() {
80 given: 'NCMP cm handle details'
81 def targetNcmpServiceCmhandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: READY),
82 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
83 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProperty1': 'value1', 'publicProperty2': 'value2'])
84 when: 'the event is populated'
85 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmhandle, existingNcmpServiceCmHandle)
86 then: 'event header is mapped correctly'
87 assert result.eventSource == 'org.onap.ncmp'
88 assert result.eventCorrelationId == cmHandleId
89 assert result.eventType == LcmEventType.CREATE.eventType
90 and: 'event payload is mapped correctly'
91 assert result.event.cmHandleId == cmHandleId
92 assert result.event.newValues.cmHandleState == Values.CmHandleState.READY
93 assert result.event.newValues.dataSyncEnabled == false
94 assert result.event.newValues.cmHandleProperties == [['publicProperty1': 'value11', 'publicProperty2': 'value22']]
95 and: 'it should not have any old values'
96 assert result.event.oldValues == null
99 def 'Map the LcmEvent for DELETE operation'() {
100 given: 'NCMP cm handle details'
101 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: false, cmHandleState: CmHandleState.DELETED),
102 publicProperties: ['publicProperty1': 'value11', 'publicProperty2': 'value22'])
103 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: true, cmHandleState: DELETING),
104 publicProperties: ['publicProperty1': 'value1'])
105 when: 'the event is populated'
106 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
107 then: 'event header is mapped correctly'
108 assert result.eventSource == 'org.onap.ncmp'
109 assert result.eventCorrelationId == cmHandleId
110 assert result.eventType == LcmEventType.DELETE.eventType
111 and: 'event payload is mapped correctly '
112 assert result.event.cmHandleId == cmHandleId
113 assert result.event.oldValues == null
114 assert result.event.newValues == null
117 def 'Map the LcmEvent for datasync flag transition from #operation'() {
118 given: 'NCMP cm handle details with current and old details'
119 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: existingDataSyncEnableFlag, cmHandleState: ADVISED))
120 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
121 when: 'the event is populated'
122 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
123 then: 'event header is mapped correctly'
124 assert result.eventSource == 'org.onap.ncmp'
125 assert result.eventCorrelationId == cmHandleId
126 assert result.eventType == LcmEventType.UPDATE.eventType
127 and: 'event payload is mapped correctly with correct cmhandle id'
128 assert result.event.cmHandleId == cmHandleId
129 and: 'it should have correct old values'
130 assert result.event.oldValues.cmHandleState == Values.CmHandleState.ADVISED
131 assert result.event.oldValues.dataSyncEnabled == existingDataSyncEnableFlag
132 and: 'the correct new values'
133 assert result.event.newValues.cmHandleState == Values.CmHandleState.READY
134 assert result.event.newValues.dataSyncEnabled == targetDataSyncEnableFlag
135 where: 'following parameters are provided'
136 operation | existingDataSyncEnableFlag | targetDataSyncEnableFlag
137 'false to true' | false | true
138 'false to null' | false | null
139 'true to false' | true | false
140 'true to null' | true | null
141 'null to true' | null | true
142 'null to false' | null | false
146 def 'Map the LcmEvent for datasync flag for same transition from #operation'() {
147 given: 'NCMP cm handle details with current and old details'
148 def existingNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: existingDataSyncEnableFlag, cmHandleState: ADVISED))
149 def targetNcmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, compositeState: new CompositeState(dataSyncEnabled: targetDataSyncEnableFlag, cmHandleState: READY))
150 when: 'the event is populated'
151 def result = objectUnderTest.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle)
152 then: 'the data sync flag is not present in the event'
153 assert result.event.oldValues.dataSyncEnabled == null
154 assert result.event.newValues.dataSyncEnabled == null
155 where: 'following parameters are provided'
156 operation | existingDataSyncEnableFlag | targetDataSyncEnableFlag
157 'false to false' | false | false
158 'true to true' | true | true
159 'null to null' | null | null