2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.event.lcm
23 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
24 import org.onap.cps.ncmp.api.inventory.CompositeState
25 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
26 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
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.DELETED
31 import static org.onap.cps.ncmp.api.inventory.CmHandleState.DELETING
32 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED
33 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY
34 import static org.onap.cps.ncmp.api.inventory.LockReasonCategory.LOCKED_MODULE_SYNC_FAILED
36 class LcmEventsCmHandleStateHandlerImplSpec extends Specification {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockLcmEventsCreator = Mock(LcmEventsCreator)
40 def mockLcmEventsService = Mock(LcmEventsService)
42 def objectUnderTest = new LcmEventsCmHandleStateHandlerImpl(mockInventoryPersistence, mockLcmEventsCreator, mockLcmEventsService)
44 def cmHandleId = 'cmhandle-id-1'
48 def 'Update and Publish Events on State Change #stateChange'() {
49 given: 'Cm Handle represented as YangModelCmHandle'
50 compositeState = new CompositeState(cmHandleState: fromCmHandleState)
51 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
52 when: 'update state is invoked'
53 objectUnderTest.updateCmHandleState(yangModelCmHandle, toCmHandleState)
54 then: 'state is saved using inventory persistence'
55 expectedCallsToInventoryPersistence * mockInventoryPersistence.saveCmHandleState(cmHandleId, _)
56 and: 'event service is called to publish event'
57 expectedCallsToEventService * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
58 where: 'state change parameters are provided'
59 stateChange | fromCmHandleState | toCmHandleState || expectedCallsToInventoryPersistence | expectedCallsToEventService
60 'ADVISED to READY' | ADVISED | READY || 1 | 1
61 'READY to LOCKED' | READY | LOCKED || 1 | 1
62 'ADVISED to ADVISED' | ADVISED | ADVISED || 0 | 0
63 'READY to READY' | READY | READY || 0 | 0
64 'LOCKED to LOCKED' | LOCKED | LOCKED || 0 | 0
68 def 'Update and Publish Events on State Change from NO_EXISTING state to ADVISED'() {
69 given: 'Cm Handle represented as YangModelCmHandle in READY state'
70 compositeState = new CompositeState()
71 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
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(LOCKED_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 to READY with #scenario'() {
98 given: 'Cm Handle represented as YangModelCmHandle'
99 compositeState = new CompositeState(cmHandleState: ADVISED)
100 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
101 and: 'global sync flag is set'
102 objectUnderTest.isGlobalDataSyncCacheEnabled = dataSyncCacheEnabled
103 when: 'update cmhandle state is invoked'
104 objectUnderTest.updateCmHandleState(yangModelCmHandle, READY)
105 then: 'state is saved using inventory persistence with expected dataSyncState'
106 1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
108 def result = (args[1] as CompositeState)
109 assert result.dataSyncEnabled == dataSyncCacheEnabled
110 assert result.dataStores.operationalDataStore.dataStoreSyncState == expectedDataStoreSyncState
114 and: 'event service is called to publish event'
115 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
117 scenario | dataSyncCacheEnabled || expectedDataStoreSyncState
118 'data sync cache enabled' | true || DataStoreSyncState.UNSYNCHRONIZED
119 'data sync cache is not enabled' | false || DataStoreSyncState.NONE_REQUESTED
122 def 'Update cmHandle state to "DELETING"' (){
123 given: 'cm Handle as Yang model'
124 compositeState = new CompositeState(cmHandleState: READY)
125 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
126 when: 'updating cm handle state to "DELETING"'
127 objectUnderTest.updateCmHandleState(yangModelCmHandle, DELETING)
128 then: 'the cm handle state is as expected'
129 yangModelCmHandle.getCompositeState().getCmHandleState() == DELETING
130 and: 'method to persist cm handle state is called once'
131 1 * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState())
132 and: 'the method to publish Lcm event is called once'
133 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
136 def 'Update cmHandle state to "DELETED"' (){
137 given: 'cm Handle with state "DELETING" as Yang model '
138 compositeState = new CompositeState(cmHandleState: DELETING)
139 yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
140 when: 'updating cm handle state to "DELETED"'
141 objectUnderTest.updateCmHandleState(yangModelCmHandle, DELETED)
142 then: 'the cm handle state is as expected'
143 yangModelCmHandle.getCompositeState().getCmHandleState() == DELETED
144 and: 'the method to publish Lcm event is called once'
145 1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)