Merge "Code Refactoring Ncmp* to Lcm* as per new scope"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / event / lcm / LcmEventsCmHandleStateHandlerImplSpec.groovy
1 /*
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
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.event.lcm
22
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
28
29 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED
30 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED
31 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY
32 import static org.onap.cps.ncmp.api.inventory.LockReasonCategory.LOCKED_MODULE_SYNC_FAILED
33
34 class LcmEventsCmHandleStateHandlerImplSpec extends Specification {
35
36     def mockInventoryPersistence = Mock(InventoryPersistence)
37     def mockLcmEventsCreator = Mock(LcmEventsCreator)
38     def mockLcmEventsService = Mock(LcmEventsService)
39
40     def objectUnderTest = new LcmEventsCmHandleStateHandlerImpl(mockInventoryPersistence, mockLcmEventsCreator, mockLcmEventsService)
41
42     def 'Update and Publish Events on State Change #stateChange'() {
43         given: 'Cm Handle represented as YangModelCmHandle'
44             def cmHandleId = 'cmhandle-id-1'
45             def compositeState = new CompositeState(cmHandleState: fromCmHandleState)
46             def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
47         when: 'update state is invoked'
48             objectUnderTest.updateCmHandleState(yangModelCmHandle, toCmHandleState)
49         then: 'state is saved using inventory persistence'
50             expectedCallsToInventoryPersistence * mockInventoryPersistence.saveCmHandleState(cmHandleId, _)
51         and: 'event service is called to publish event'
52             expectedCallsToEventService * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
53         where: 'state change parameters are provided'
54             stateChange          | fromCmHandleState | toCmHandleState || expectedCallsToInventoryPersistence | expectedCallsToEventService
55             'ADVISED to READY'   | ADVISED           | READY           || 1                                   | 1
56             'READY to LOCKED'    | READY             | LOCKED          || 1                                   | 1
57             'ADVISED to ADVISED' | ADVISED           | ADVISED         || 0                                   | 0
58             'READY to READY'     | READY             | READY           || 0                                   | 0
59             'LOCKED to LOCKED'   | LOCKED            | LOCKED          || 0                                   | 0
60
61     }
62
63     def 'Update and Publish Events on State Change from NO_EXISTING state to ADVISED'() {
64         given: 'Cm Handle represented as YangModelCmHandle in READY state'
65             def cmHandleId = 'cmhandle-id-1'
66             def compositeState = new CompositeState()
67             def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
68         when: 'update state is invoked'
69             objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
70         then: 'state is saved using inventory persistence'
71             1 * mockInventoryPersistence.saveCmHandle(yangModelCmHandle)
72         and: 'event service is called to publish event'
73             1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
74     }
75
76     def 'Update and Publish Events on State Change from LOCKED to ADVISED'() {
77         given: 'Cm Handle represented as YangModelCmHandle in LOCKED state'
78             def cmHandleId = 'cmhandle-id-1'
79             def compositeState = new CompositeState(cmHandleState: LOCKED,
80                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LOCKED_MODULE_SYNC_FAILED).details('some lock details').build())
81             def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
82         when: 'update state is invoked'
83             objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
84         then: 'state is saved using inventory persistence and old lock reason details are retained'
85             1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
86                 args -> {
87                         assert (args[1] as CompositeState).lockReason.details == 'some lock details'
88                     }
89             }
90         and: 'event service is called to publish event'
91             1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
92     }
93
94     def 'Update and Publish Events on State Change to READY with #scenario'() {
95         given: 'Cm Handle represented as YangModelCmHandle'
96             def cmHandleId = 'cmhandle-id-1'
97             def compositeState = new CompositeState(cmHandleState: ADVISED)
98             def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
99         and: 'global sync flag is set'
100             objectUnderTest.isGlobalDataSyncCacheEnabled = dataSyncCacheEnabled
101         when: 'update cmhandle state is invoked'
102             objectUnderTest.updateCmHandleState(yangModelCmHandle, READY)
103         then: 'state is saved using inventory persistence with expected dataSyncState'
104             1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
105                 args-> {
106                     def result = (args[1] as CompositeState)
107                     assert result.dataSyncEnabled == dataSyncCacheEnabled
108                     assert result.dataStores.operationalDataStore.dataStoreSyncState == expectedDataStoreSyncState
109
110                 }
111             }
112         and: 'event service is called to publish event'
113             1 * mockLcmEventsService.publishLcmEvent(cmHandleId, _)
114         where:
115             scenario                         | dataSyncCacheEnabled || expectedDataStoreSyncState
116             'data sync cache enabled'        | true                 || DataStoreSyncState.UNSYNCHRONIZED
117             'data sync cache is not enabled' | false                || DataStoreSyncState.NONE_REQUESTED
118
119     }
120 }