Merge "use separated get methods for every cmHandle instead of one "get all" query"
[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 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.ncmp.api.inventory.CmHandleState
26 import org.onap.cps.ncmp.api.inventory.CompositeState
27 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
28 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
29 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
30 import spock.lang.Specification
31
32 class ModuleSyncWatchdogSpec extends Specification {
33
34     def mockInventoryPersistence = Mock(InventoryPersistence)
35
36     def mockSyncUtils = Mock(SyncUtils)
37
38     def mockModuleSyncService = Mock(ModuleSyncService)
39
40     def cmHandleState = CmHandleState.ADVISED
41
42     def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService)
43
44     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handles'() {
45         given: 'cm handles in an advised state'
46             def compositeState1 = new CompositeState(cmHandleState: cmHandleState)
47             def compositeState2 = new CompositeState(cmHandleState: cmHandleState)
48             def yangModelCmHandle1 = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState1)
49             def yangModelCmHandle2 = new YangModelCmHandle(id: 'some-cm-handle-2', compositeState: compositeState2)
50         and: 'sync utilities return a cm handle twice'
51             mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle1, yangModelCmHandle2, null]
52         when: 'module sync poll is executed'
53             objectUnderTest.executeAdvisedCmHandlePoll()
54         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
55             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
56         and: 'module sync service deletes schema set of cm handle if it exists'
57             1 * mockModuleSyncService.deleteSchemaSetIfExists(yangModelCmHandle1)
58         and: 'module sync service syncs the first cm handle and creates a schema set'
59             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
60         and: 'the composite state cm handle state is now READY'
61             assert compositeState1.getCmHandleState() == CmHandleState.READY
62         and: 'the first cm handle state is updated'
63             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState1)
64         then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
65             mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
66         and: 'module sync service syncs the second cm handle and creates a schema set'
67             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
68         and: 'the composite state cm handle state is now READY'
69             assert compositeState2.getCmHandleState() == CmHandleState.READY
70         and: 'the second cm handle state is updated'
71             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState2)
72     }
73
74     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
75         given: 'cm handles in an advised state'
76             def compositeState = new CompositeState(cmHandleState: cmHandleState)
77             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
78         and: 'sync utilities return a cm handle'
79             mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle, null]
80         when: 'module sync poll is executed'
81             objectUnderTest.executeAdvisedCmHandlePoll()
82         then: 'the inventory persistence cm handle returns a composite state for the cm handle'
83             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
84         and: 'module sync service attempts to sync the cm handle and throws an exception'
85             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
86         and: 'the composite state cm handle state is now LOCKED'
87             assert compositeState.getCmHandleState() == CmHandleState.LOCKED
88         and: 'update lock reason, details and attempts is invoked'
89             1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING ,'some exception')
90         and: 'the cm handle state is updated'
91             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState)
92
93     }
94
95     def 'Schedule a Cm-Handle Sync for LOCKED with reason LOCKED_MISBEHAVING Cm-Handles with #scenario'() {
96         given: 'cm handles in an locked state'
97             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
98                     .withLockReason(LockReasonCategory.LOCKED_MISBEHAVING, '').withLastUpdatedTimeNow().build()
99             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
100         and: 'sync utilities return a cm handle twice'
101             mockSyncUtils.getLockedMisbehavingYangModelCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
102         and: 'inventory persistence returns the composite state of the cm handle'
103             mockInventoryPersistence.getCmHandleState(yangModelCmHandle.getId()) >> compositeState
104         and: 'sync utils retry locked cm handle returns #isReadyForRetry'
105             mockSyncUtils.isReadyForRetry(compositeState) >>> isReadyForRetry
106         when: 'module sync poll is executed'
107             objectUnderTest.executeLockedCmHandlePoll()
108         then: 'the first cm handle is updated to state "ADVISED" from "READY"'
109             expectedNumberOfInvocationsToSaveCmHandleState * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.id, compositeState)
110         where:
111             scenario                        | isReadyForRetry         || expectedNumberOfInvocationsToSaveCmHandleState
112             'retry locked cm handle once'   | [true, false]           || 1
113             'retry locked cm handle twice'  | [true, true]            || 2
114             'do not retry locked cm handle' | [false, false]          || 0
115     }
116 }