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