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 static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.CREATE
26 import org.onap.cps.ncmp.api.impl.event.NcmpEventsService
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
28 import org.onap.cps.ncmp.api.inventory.CmHandleState
29 import org.onap.cps.ncmp.api.inventory.CompositeState
30 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
32 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
33 import spock.lang.Specification
35 class ModuleSyncSpec extends Specification {
37 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockSyncUtils = Mock(SyncUtils)
41 def mockModuleSyncService = Mock(ModuleSyncService)
43 def mockNcmpEventsService = Mock(NcmpEventsService)
45 def cmHandleState = CmHandleState.ADVISED
47 def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, mockNcmpEventsService)
49 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handles'() {
50 given: 'cm handles in an advised state'
51 def compositeState1 = new CompositeState(cmHandleState: cmHandleState)
52 def compositeState2 = new CompositeState(cmHandleState: cmHandleState)
53 def yangModelCmHandle1 = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState1)
54 def yangModelCmHandle2 = new YangModelCmHandle(id: 'some-cm-handle-2', compositeState: compositeState2)
55 and: 'sync utilities return a cm handle twice'
56 mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle1, yangModelCmHandle2, null]
57 when: 'module sync poll is executed'
58 objectUnderTest.executeAdvisedCmHandlePoll()
59 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
60 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
61 and: 'module sync service syncs the first cm handle and creates a schema set'
62 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
63 and: 'the composite state cm handle state is now READY'
64 assert compositeState1.getCmHandleState() == CmHandleState.READY
65 and: 'the first cm handle state is updated'
66 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState1)
67 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
68 mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
69 and: 'module sync service syncs the second cm handle and creates a schema set'
70 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
71 and: 'the composite state cm handle state is now READY'
72 assert compositeState2.getCmHandleState() == CmHandleState.READY
73 and: 'the second cm handle state is updated'
74 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState2)
75 and: 'the ncmp event will be published for both cmHandles'
76 1 * mockNcmpEventsService.publishNcmpEvent('some-cm-handle', CREATE)
77 1 * mockNcmpEventsService.publishNcmpEvent('some-cm-handle-2', CREATE)
80 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
81 given: 'cm handles in an advised state'
82 def compositeState = new CompositeState(cmHandleState: cmHandleState)
83 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
84 and: 'sync utilities return a cm handle'
85 mockSyncUtils.getAnAdvisedCmHandle() >>> [yangModelCmHandle, null]
86 when: 'module sync poll is executed'
87 objectUnderTest.executeAdvisedCmHandlePoll()
88 then: 'the inventory persistence cm handle returns a composite state for the cm handle'
89 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
90 and: 'module sync service attempts to sync the cm handle and throws an exception'
91 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
92 and: 'the composite state cm handle state is now LOCKED'
93 assert compositeState.getCmHandleState() == CmHandleState.LOCKED
94 and: 'update lock reason, details and attempts is invoked'
95 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING ,'some exception')
96 and: 'the cm handle state is updated'
97 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState)
98 and: 'the ncmp event is not published'
99 0 * mockNcmpEventsService.publishNcmpEvent(_, _)
103 def 'Schedule a Cm-Handle Sync for LOCKED with reason LOCKED_MISBEHAVING Cm-Handles '() {
104 given: 'cm handles in an locked state'
105 def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
106 .withLockReason(LockReasonCategory.LOCKED_MISBEHAVING, '').build()
107 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
108 and: 'sync utilities return a cm handle twice'
109 mockSyncUtils.getLockedMisbehavingCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
110 when: 'module sync poll is executed'
111 objectUnderTest.executeLockedMisbehavingCmHandlePoll()
112 then: 'the first cm handle is updated to state "ADVISED" from "READY"'
113 2 * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.id, compositeState)