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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync
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
32 class ModuleSyncSpec extends Specification {
34 def mockInventoryPersistence = Mock(InventoryPersistence)
36 def mockSyncUtils = Mock(SyncUtils)
38 def mockModuleSyncService = Mock(ModuleSyncService)
40 def cmHandleState = CmHandleState.ADVISED
42 def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService)
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 syncs the first cm handle and creates a schema set'
57 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
58 and: 'the composite state cm handle state is now READY'
59 assert compositeState1.getCmHandleState() == CmHandleState.READY
60 and: 'the first cm handle state is updated'
61 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState1)
62 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
63 mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
64 and: 'module sync service syncs the second cm handle and creates a schema set'
65 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
66 and: 'the composite state cm handle state is now READY'
67 assert compositeState2.getCmHandleState() == CmHandleState.READY
68 and: 'the second cm handle state is updated'
69 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState2)
72 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
73 given: 'cm handles in an advised state'
74 def compositeState = new CompositeState(cmHandleState: cmHandleState)
75 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
76 and: 'sync utilities return a cm handle'
77 mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle, null]
78 when: 'module sync poll is executed'
79 objectUnderTest.executeAdvisedCmHandlePoll()
80 then: 'the inventory persistence cm handle returns a composite state for the cm handle'
81 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
82 and: 'module sync service attempts to sync the cm handle and throws an exception'
83 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
84 and: 'the composite state cm handle state is now LOCKED'
85 assert compositeState.getCmHandleState() == CmHandleState.LOCKED
86 and: 'update lock reason, details and attempts is invoked'
87 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING ,'some exception')
88 and: 'the cm handle state is updated'
89 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState)
93 def 'Schedule a Cm-Handle Sync for LOCKED with reason LOCKED_MISBEHAVING Cm-Handles '() {
94 given: 'cm handles in an locked state'
95 def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
96 .withLockReason(LockReasonCategory.LOCKED_MISBEHAVING, '').build()
97 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
98 and: 'sync utilities return a cm handle twice'
99 mockSyncUtils.getLockedMisbehavingCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
100 when: 'module sync poll is executed'
101 objectUnderTest.executeLockedMisbehavingCmHandlePoll()
102 then: 'the first cm handle is updated to state "ADVISED" from "READY"'
103 2 * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.id, compositeState)