81268cbc0b2b9364ff03307e28dcc36623ae52bb
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdogSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory.sync
23
24
25 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
31 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
32 import spock.lang.Specification
33
34 import java.util.concurrent.ConcurrentHashMap
35 import java.util.concurrent.ConcurrentMap
36
37 class ModuleSyncWatchdogSpec extends Specification {
38
39     def mockInventoryPersistence = Mock(InventoryPersistence)
40
41     def mockSyncUtils = Mock(SyncUtils)
42
43     def mockModuleSyncService = Mock(ModuleSyncService)
44
45     def stubbedMap = Stub(ConcurrentMap)
46
47     def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
48
49     def cmHandleState = CmHandleState.ADVISED
50
51     def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, stubbedMap as ConcurrentHashMap, mockLcmEventsCmHandleStateHandler)
52
53     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handles'() {
54         given: 'cm handles in an advised state and a data sync state'
55             def compositeState1 = new CompositeState(cmHandleState: cmHandleState)
56             def compositeState2 = new CompositeState(cmHandleState: cmHandleState)
57             def yangModelCmHandle1 = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState1)
58             def yangModelCmHandle2 = new YangModelCmHandle(id: 'some-cm-handle-2', compositeState: compositeState2)
59         and: 'sync utilities return a cm handle twice'
60             mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
61         when: 'module sync poll is executed'
62             objectUnderTest.executeAdvisedCmHandlePoll()
63         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
64             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
65         and: 'module sync service deletes schema set of cm handle if it exists'
66             1 * mockModuleSyncService.deleteSchemaSetIfExists(yangModelCmHandle1)
67         and: 'module sync service syncs the first cm handle and creates a schema set'
68             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
69         then: 'the state handler is called for the first cm handle'
70             1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle1, CmHandleState.READY)
71         and: 'the inventory persistence cm handle returns a composite state for the second cm handle'
72             mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
73         and: 'module sync service syncs the second cm handle and creates a schema set'
74             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
75         then: 'the state handler is called for the second cm handle'
76             1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle2, CmHandleState.READY)
77     }
78
79     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
80         given: 'cm handles in an advised state'
81             def compositeState = new CompositeState(cmHandleState: cmHandleState)
82             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
83         and: 'sync utilities return a cm handle'
84             mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle]
85         when: 'module sync poll is executed'
86             objectUnderTest.executeAdvisedCmHandlePoll()
87         then: 'the inventory persistence cm handle returns a composite state for the cm handle'
88             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
89         and: 'module sync service attempts to sync the cm handle and throws an exception'
90             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
91         and: 'update lock reason, details and attempts is invoked'
92             1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED ,'some exception')
93         and: 'the state handler is called to update the state to LOCKED'
94             1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.LOCKED)
95     }
96
97     def 'Schedule a Cm-Handle Sync with condition #scenario '() {
98         given: 'cm handles in an locked state'
99             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
100                     .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
101             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
102         and: 'sync utilities return a cm handle twice'
103             mockSyncUtils.getModuleSyncFailedCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
104         and: 'inventory persistence returns the composite state of the cm handle'
105             mockInventoryPersistence.getCmHandleState(yangModelCmHandle.getId()) >> compositeState
106         and: 'sync utils retry locked cm handle returns #isReadyForRetry'
107             mockSyncUtils.isReadyForRetry(compositeState) >>> isReadyForRetry
108         when: 'module sync poll is executed'
109             objectUnderTest.executeLockedCmHandlePoll()
110         then: 'the first cm handle is updated to state "ADVISED" from "READY"'
111             expectedNumberOfInvocationsToSaveCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.ADVISED)
112         where:
113             scenario                        | isReadyForRetry         || expectedNumberOfInvocationsToSaveCmHandleState
114             'retry locked cm handle once'   | [true, false]           || 1
115             'retry locked cm handle twice'  | [true, true]            || 2
116             'do not retry locked cm handle' | [false, false]          || 0
117     }
118 }