291ba968ff29c9c3e61cd2ad6f4f36f0b99dfeb1
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncTasksSpec.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 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.CmHandleState
27 import org.onap.cps.ncmp.api.inventory.CompositeState
28 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
33
34 class ModuleSyncTasksSpec extends Specification {
35
36     def mockInventoryPersistence = Mock(InventoryPersistence)
37
38     def mockSyncUtils = Mock(SyncUtils)
39
40     def mockModuleSyncService = Mock(ModuleSyncService)
41
42     def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
43
44     def objectUnderTest = new ModuleSyncTasks(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, mockLcmEventsCmHandleStateHandler)
45
46     def 'Module Sync ADVISED cm handles.'() {
47         given: 'cm handles in an ADVISED state'
48             def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
49             def cmHandle2 = advisedCmHandleAsDataNode('cm-handle-2')
50         and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
51             mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
52         when: 'module sync poll is executed'
53             objectUnderTest.performModuleSync([cmHandle1, cmHandle2])
54         then: 'module sync service deletes schemas set of each cm handle if it already exists'
55             1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-1')
56             1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-2')
57         and: 'module sync service is invoked for each cm handle'
58             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args,'cm-handle-1') }
59             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args,'cm-handle-2') }
60         and: 'the state handler is called for the both cm handles'
61             2 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.READY)
62     }
63
64     def 'Module Sync ADVISED cm handle with failure during sync.'() {
65         given: 'a cm handle in an ADVISED state'
66             def cmHandle = advisedCmHandleAsDataNode('cm-handle')
67         and: 'the inventory persistence cm handle returns a ADVISED state for the cm handle'
68             def cmHandleState = new CompositeState(cmHandleState: CmHandleState.ADVISED)
69             1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> cmHandleState
70         and: 'module sync service attempts to sync the cm handle and throws an exception'
71             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
72         when: 'module sync is executed'
73             objectUnderTest.performModuleSync([cmHandle])
74         then: 'update lock reason, details and attempts is invoked'
75             1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED ,'some exception')
76         and: 'the state handler is called to update the state to LOCKED'
77             1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.LOCKED)
78     }
79
80     def 'Reset failed CM Handles #scenario.'() {
81         given: 'cm handles in an locked state'
82             def lockedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
83                     .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
84             def yangModelCmHandle1 = new YangModelCmHandle(id: 'cm-handle-1', compositeState: lockedState)
85             def yangModelCmHandle2 = new YangModelCmHandle(id: 'cm-handle-2', compositeState: lockedState)
86         and: 'sync utils retry locked cm handle returns #isReadyForRetry'
87             mockSyncUtils.isReadyForRetry(lockedState) >>> isReadyForRetry
88         when: 'resetting failed cm handles'
89             objectUnderTest.resetFailedCmHandles([yangModelCmHandle1, yangModelCmHandle2])
90         then: 'updated to state "ADVISED" from "READY" is called as often as there are cm handles ready for retry'
91             expectedNumberOfInvocationsToSaveCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.ADVISED)
92         where:
93             scenario                        | isReadyForRetry         || expectedNumberOfInvocationsToSaveCmHandleState
94             'retry locked cm handle once'   | [true, false]           || 1
95             'retry locked cm handle twice'  | [true, true]            || 2
96             'do not retry locked cm handle' | [false, false]          || 0
97     }
98
99     def advisedCmHandleAsDataNode(cmHandleId) {
100         return new DataNode(anchorName:cmHandleId, leaves:['id':cmHandleId, 'cm-handle-state':'ADVISED'])
101     }
102
103     def assertYamgModelCmHandleArgument(args, expectedCmHandleId) {
104         {
105             def yangModelCmHandle = args[0]
106             assert yangModelCmHandle.id == expectedCmHandleId
107         }
108         return true
109     }
110 }