2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.inventory.sync.lcm
23 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.ADVISED
24 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.READY
26 import com.hazelcast.core.Hazelcast
27 import com.hazelcast.map.IMap
28 import org.onap.cps.ncmp.api.inventory.models.CompositeState
29 import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService
30 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
31 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.DecreasingEntryProcessor
32 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.IncreasingEntryProcessor
33 import org.onap.cps.ncmp.utils.events.NcmpInventoryModelOnboardingFinishedEvent
34 import spock.lang.Shared
35 import spock.lang.Specification;
37 class CmHandleStateMonitorSpec extends Specification {
39 def mockCmHandlesByState = Mock(IMap)
40 def mockCmHandleQueryService = Mock(CmHandleQueryService)
41 def objectUnderTest = new CmHandleStateMonitor(mockCmHandleQueryService, mockCmHandlesByState)
44 def entryProcessingMap = Hazelcast.newHazelcastInstance().getMap('entryProcessingMap')
47 entryProcessingMap.put('zeroCmHandlesCount', 0)
48 entryProcessingMap.put('tenCmHandlesCount', 10)
52 Hazelcast.shutdownAll()
55 def 'Initialise cm handle state monitor: #scenario'() {
56 given: 'the query service returns a list of cm-handle ids for the given state'
57 mockCmHandleQueryService.queryCmHandleIdsByState(_) >> queryResult
58 and: 'a mocked NcmpModelOnboardingFinishedEvent is triggered'
59 def mockNcmpModelOnboardingFinishedEvent = Mock(NcmpInventoryModelOnboardingFinishedEvent)
60 when: 'the method to initialise cm handle state monitor is triggered by onboarding event'
61 objectUnderTest.initialiseCmHandleStateMonitor(mockNcmpModelOnboardingFinishedEvent)
62 then: 'metrics map is called correct number of times for each state except DELETED, with expected value'
63 1 * mockCmHandlesByState.putIfAbsent("advisedCmHandlesCount", expectedValue)
64 1 * mockCmHandlesByState.putIfAbsent("readyCmHandlesCount", expectedValue)
65 1 * mockCmHandlesByState.putIfAbsent("lockedCmHandlesCount", expectedValue)
66 1 * mockCmHandlesByState.putIfAbsent("deletingCmHandlesCount", expectedValue)
68 scenario | queryResult || expectedValue
69 'query service returns zero cm handle id'| [] || 0
70 'query service returns 1 cm handle id' | ['someId'] || 1
74 def 'Update cm handle state metric'() {
75 given: 'a collection of cm handle state pair'
76 def cmHandleTransitionPair = new LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair()
77 cmHandleTransitionPair.currentYangModelCmHandle = new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED))
78 cmHandleTransitionPair.targetYangModelCmHandle = new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: READY))
79 when: 'method to update cm handle state metrics is called'
80 objectUnderTest.updateCmHandleStateMetrics([cmHandleTransitionPair])
81 then: 'cm handle by state cache map is called once for current and target state for entry processing'
82 1 * mockCmHandlesByState.executeOnKey('advisedCmHandlesCount', _)
83 1 * mockCmHandlesByState.executeOnKey('readyCmHandlesCount', _)
86 def 'Update cm handle state metric with no previous state'() {
87 given: 'a collection of cm handle state pair wherein current state is null'
88 def cmHandleTransitionPair = new LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair()
89 cmHandleTransitionPair.currentYangModelCmHandle = new YangModelCmHandle(compositeState: null)
90 cmHandleTransitionPair.targetYangModelCmHandle = new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED))
91 when: 'updating cm handle state metrics'
92 objectUnderTest.updateCmHandleStateMetrics([cmHandleTransitionPair])
93 then: 'cm handle by state cache map is called only once'
94 1 * mockCmHandlesByState.executeOnKey(_, _)
97 def 'Applying decreasing entry processor to a key on map where #scenario'() {
98 when: 'decreasing entry processor is applied to subtract 1 to the value'
99 entryProcessingMap.executeOnKey(key, new DecreasingEntryProcessor())
100 then: 'the new value is as expected'
101 assert entryProcessingMap.get(key) == expectedValue
102 where: 'the following data is used'
103 scenario | key || expectedValue
104 'current value of count is zero'| 'zeroCmHandlesCount'|| 0
105 'current value of count is >0' | 'tenCmHandlesCount' || 9
108 def 'Applying increasing entry processor to a key on map'() {
109 when: 'increasing entry processor is applied to add 1 to the value'
110 entryProcessingMap.executeOnKey('tenCmHandlesCount', new IncreasingEntryProcessor())
111 then: 'the new value is as expected'
112 assert entryProcessingMap.get('tenCmHandlesCount') == 11