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 static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.ADVISED
24 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.DELETED
25 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.DELETING
26 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.LOCKED
27 import static org.onap.cps.ncmp.api.impl.inventory.CmHandleState.READY
28 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_SYNC_FAILED
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
31 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
32 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
33 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
34 import spock.lang.Specification
36 class LcmEventsCmHandleStateHandlerImplSpec extends Specification {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockLcmEventsCreator = Mock(LcmEventsCreator)
40 def mockLcmEventsService = Mock(LcmEventsService)
42 def lcmEventsCmHandleStateHandlerAsyncHelper = new LcmEventsCmHandleStateHandlerAsyncHelper(mockLcmEventsCreator, mockLcmEventsService)
43 def objectUnderTest = new LcmEventsCmHandleStateHandlerImpl(mockInventoryPersistence, lcmEventsCmHandleStateHandlerAsyncHelper)
45 def cmHandleId = 'cmhandle-id-1'
49 def 'Update and Publish Events on State Change #stateChange'() {
50 given: 'Cm Handle represented as YangModelCmHandle'
51 compositeState = new CompositeState(cmHandleState: fromCmHandleState)
52 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
53 when: 'update state is invoked'
54 objectUnderTest.updateCmHandleState(yangModelCmHandle, toCmHandleState)
55 then: 'state is saved using inventory persistence'
56 expectedCallsToInventoryPersistence * mockInventoryPersistence.saveCmHandleState(cmHandleId, _)
57 and: 'event service is called to publish event'
58 expectedCallsToEventService * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
59 where: 'state change parameters are provided'
60 stateChange | fromCmHandleState | toCmHandleState || expectedCallsToInventoryPersistence | expectedCallsToEventService
61 'ADVISED to READY' | ADVISED | READY || 1 | 1
62 'READY to LOCKED' | READY | LOCKED || 1 | 1
63 'ADVISED to ADVISED' | ADVISED | ADVISED || 0 | 0
64 'READY to READY' | READY | READY || 0 | 0
65 'LOCKED to LOCKED' | LOCKED | LOCKED || 0 | 0
66 'DELETED to ADVISED' | DELETED | ADVISED || 0 | 1
69 def 'Update and Publish Events on State Change from NO_EXISTING state to ADVISED'() {
70 given: 'Cm Handle represented as YangModelCmHandle'
71 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [])
72 when: 'update state is invoked'
73 objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
74 then: 'state is saved using inventory persistence'
75 1 * mockInventoryPersistence.saveCmHandle(yangModelCmHandle)
76 and: 'event service is called to publish event'
77 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
80 def 'Update and Publish Events on State Change from LOCKED to ADVISED'() {
81 given: 'Cm Handle represented as YangModelCmHandle in LOCKED state'
82 compositeState = new CompositeState(cmHandleState: LOCKED,
83 lockReason: CompositeState.LockReason.builder().lockReasonCategory(MODULE_SYNC_FAILED).details('some lock details').build())
84 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
85 when: 'update state is invoked'
86 objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
87 then: 'state is saved using inventory persistence and old lock reason details are retained'
88 1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
90 assert (args[1] as CompositeState).lockReason.details == 'some lock details'
93 and: 'event service is called to publish event'
94 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
97 def 'Update and Publish Events on State Change from DELETING to ADVISED'() {
98 given: 'Cm Handle represented as YangModelCmHandle in DELETING state'
99 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
100 when: 'update state is invoked'
101 objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
102 then: 'the cm handle is saved using inventory persistence'
103 1 * mockInventoryPersistence.saveCmHandle(yangModelCmHandle)
104 and: 'event service is called to publish event'
105 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
108 def 'Update and Publish Events on State Change to READY'() {
109 given: 'Cm Handle represented as YangModelCmHandle'
110 compositeState = new CompositeState(cmHandleState: ADVISED)
111 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
112 and: 'global sync flag is set'
113 compositeState.setDataSyncEnabled(false)
114 when: 'update cmhandle state is invoked'
115 objectUnderTest.updateCmHandleState(yangModelCmHandle, READY)
116 then: 'state is saved using inventory persistence with expected dataSyncState'
117 1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
119 def result = (args[1] as CompositeState)
120 assert result.dataSyncEnabled == false
121 assert result.dataStores.operationalDataStore.dataStoreSyncState == DataStoreSyncState.NONE_REQUESTED
125 and: 'event service is called to publish event'
126 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
129 def 'Update cmHandle state to "DELETING"' (){
130 given: 'cm Handle as Yang model'
131 compositeState = new CompositeState(cmHandleState: READY)
132 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
133 when: 'updating cm handle state to "DELETING"'
134 objectUnderTest.updateCmHandleState(yangModelCmHandle, DELETING)
135 then: 'the cm handle state is as expected'
136 yangModelCmHandle.getCompositeState().getCmHandleState() == DELETING
137 and: 'method to persist cm handle state is called once'
138 1 * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState())
139 and: 'the method to publish Lcm event is called once'
140 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
143 def 'Update cmHandle state to "DELETED"' (){
144 given: 'cm Handle with state "DELETING" as Yang model '
145 compositeState = new CompositeState(cmHandleState: DELETING)
146 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
147 when: 'updating cm handle state to "DELETED"'
148 objectUnderTest.updateCmHandleState(yangModelCmHandle, DELETED)
149 then: 'the cm handle state is as expected'
150 yangModelCmHandle.getCompositeState().getCmHandleState() == DELETED
151 and: 'the method to publish Lcm event is called once'
152 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _, _)
155 def 'No state change and no event to be published'() {
156 given: 'Cm Handle batch with same state transition as before'
157 def cmHandleStateMap = setupBatch('NO_CHANGE')
158 when: 'updating a batch of changes'
159 objectUnderTest.updateCmHandleStateBatch(cmHandleStateMap)
160 then: 'batch is empty and nothing to update'
161 1 * mockInventoryPersistence.saveCmHandleBatch(_) >> {
163 assert (args[0] as Collection<YangModelCmHandle>).size() == 0
166 and: 'no event will be published'
167 0 * mockLcmEventsService.publishLcmEvent(*_)
170 def 'Batch of new cm handles provided'() {
171 given: 'A batch of new cm handles'
172 def cmHandleStateMap = setupBatch('NEW')
173 when: 'updating a batch of changes'
174 objectUnderTest.updateCmHandleStateBatch(cmHandleStateMap)
175 then: 'new cm handles are saved using inventory persistence'
176 1 * mockInventoryPersistence.saveCmHandleBatch(_) >> {
178 assert (args[0] as Collection<YangModelCmHandle>).id.containsAll('cmhandle1', 'cmhandle2')
181 and: 'event service is called to publish events'
182 2 * mockLcmEventsService.publishLcmEvent(_, _, _)
186 def 'Batch of existing cm handles is updated'() {
187 given: 'A batch of updated cm handles'
188 def cmHandleStateMap = setupBatch('UPDATE')
189 when: 'updating a batch of changes'
190 objectUnderTest.updateCmHandleStateBatch(cmHandleStateMap)
191 then : 'existing cm handles composite state is persisted'
192 1 * mockInventoryPersistence.saveCmHandleStateBatch(_) >> {
194 assert (args[0] as Map<String, CompositeState>).keySet().containsAll(['cmhandle1','cmhandle2'])
197 and: 'event service is called to publish events'
198 2 * mockLcmEventsService.publishLcmEvent(_, _, _)
201 def 'Batch of existing cm handles is deleted'() {
202 given: 'A batch of deleted cm handles'
203 def cmHandleStateMap = setupBatch('DELETED')
204 when: 'updating a batch of changes'
205 objectUnderTest.updateCmHandleStateBatch(cmHandleStateMap)
206 then : 'existing cm handles composite state is persisted'
207 1 * mockInventoryPersistence.saveCmHandleStateBatch(_) >> {
209 assert (args[0] as Map<String, CompositeState>).isEmpty()
212 and: 'event service is called to publish events'
213 2 * mockLcmEventsService.publishLcmEvent(_, _, _)
216 def setupBatch(type) {
218 def yangModelCmHandle1 = new YangModelCmHandle(id: 'cmhandle1', dmiProperties: [], publicProperties: [])
219 def yangModelCmHandle2 = new YangModelCmHandle(id: 'cmhandle2', dmiProperties: [], publicProperties: [])
222 return [(yangModelCmHandle1): ADVISED, (yangModelCmHandle2): ADVISED]
225 if ('DELETED' == type) {
226 yangModelCmHandle1.compositeState = new CompositeState(cmHandleState: READY)
227 yangModelCmHandle2.compositeState = new CompositeState(cmHandleState: READY)
228 return [(yangModelCmHandle1): DELETED, (yangModelCmHandle2): DELETED]
231 if ('UPDATE' == type) {
232 yangModelCmHandle1.compositeState = new CompositeState(cmHandleState: ADVISED)
233 yangModelCmHandle2.compositeState = new CompositeState(cmHandleState: READY)
234 return [(yangModelCmHandle1): READY, (yangModelCmHandle2): DELETING]
237 if ('NO_CHANGE' == type) {
238 yangModelCmHandle1.compositeState = new CompositeState(cmHandleState: ADVISED)
239 yangModelCmHandle2.compositeState = new CompositeState(cmHandleState: READY)
240 return [(yangModelCmHandle1): ADVISED, (yangModelCmHandle2): READY]