97bea096a31132e9c2d0b28d1e99c955ad9a7c7b
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncSpec.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.inventory.sync
22
23 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
24 import org.onap.cps.ncmp.api.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.inventory.CompositeState
26 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
27 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
28 import spock.lang.Specification
29
30 class ModuleSyncSpec extends Specification {
31
32     def mockInventoryPersistence = Mock(InventoryPersistence)
33
34     def mockSyncUtils = Mock(SyncUtils)
35
36     def mockModuleSyncService = Mock(ModuleSyncService)
37
38     def cmHandleState = CmHandleState.ADVISED
39
40     def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService)
41
42     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handles'() {
43         given: 'cm handles in an advised state'
44             def compositeState1 = new CompositeState(cmHandleState: cmHandleState)
45             def compositeState2 = new CompositeState(cmHandleState: cmHandleState)
46             def yangModelCmHandle1 = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState1)
47             def yangModelCmHandle2 = new YangModelCmHandle(id: 'some-cm-handle-2', compositeState: compositeState2)
48         and: 'sync utilities return a cm handle twice'
49             mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle1, yangModelCmHandle2, null]
50         when: 'module sync poll is executed'
51             objectUnderTest.executeAdvisedCmHandlePoll()
52         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
53             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
54         and: 'module sync service syncs the first cm handle and creates a schema set'
55             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
56         and: 'the composite state cm handle state is now READY'
57             assert compositeState1.getCmHandleState() == CmHandleState.READY
58         and: 'the first cm handle state is updated'
59             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState1)
60         then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
61             mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
62         and: 'module sync service syncs the second cm handle and creates a schema set'
63             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
64         and: 'the composite state cm handle state is now READY'
65             assert compositeState2.getCmHandleState() == CmHandleState.READY
66         and: 'the second cm handle state is updated'
67             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState2)
68     }
69
70     def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
71         given: 'cm handles in an advised state'
72             def compositeState = new CompositeState(cmHandleState: cmHandleState)
73             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
74         and: 'sync utilities return a cm handle'
75             mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle, null]
76         when: 'module sync poll is executed'
77             objectUnderTest.executeAdvisedCmHandlePoll()
78         then: 'the inventory persistence cm handle returns a composite state for the cm handle'
79             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
80         and: 'module sync service attempts to sync the cm handle and throws an exception'
81             1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
82         and: 'the composite state cm handle state is now LOCKED'
83             assert compositeState.getCmHandleState() == CmHandleState.LOCKED
84         and: 'update lock reason, details and attempts is invoked'
85             1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING ,'some exception')
86         and: 'the cm handle state is updated'
87             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState)
88
89     }
90
91 }