2833ca838f1495153daf9d004b601aa51cdc664f
[cps.git] /
1 /*
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
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.impl.inventory.sync.lcm
22
23 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.ADVISED
24 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleState.READY
25
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.models.YangModelCmHandle
30 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.DecreasingEntryProcessor
31 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.IncreasingEntryProcessor
32 import spock.lang.Shared
33 import spock.lang.Specification;
34
35 class CmHandleStateMonitorSpec extends Specification {
36
37     def cmHandlesByState = Mock(IMap)
38     def objectUnderTest = new CmHandleStateMonitor(cmHandlesByState)
39
40     @Shared
41     def entryProcessingMap =  Hazelcast.newHazelcastInstance().getMap('entryProcessingMap')
42
43     def setup() {
44         entryProcessingMap.put('zeroCmHandlesCount', 0)
45         entryProcessingMap.put('tenCmHandlesCount', 10)
46     }
47
48     def cleanupSpec() {
49         Hazelcast.shutdownAll()
50     }
51
52     def 'Update cm handle state metric'() {
53         given: 'a collection of cm handle state pair'
54             def cmHandleTransitionPair = new LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair()
55             cmHandleTransitionPair.currentYangModelCmHandle = new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED))
56             cmHandleTransitionPair.targetYangModelCmHandle =  new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: READY))
57         when: 'method to update cm handle state metrics is called'
58             objectUnderTest.updateCmHandleStateMetrics([cmHandleTransitionPair])
59         then: 'cm handle by state cache map is called once for current and target state for entry processing'
60             1 * cmHandlesByState.executeOnKey('advisedCmHandlesCount', _)
61             1 * cmHandlesByState.executeOnKey('readyCmHandlesCount', _)
62     }
63
64     def 'Updating cm handle state metric with no previous state'() {
65         given: 'a collection of cm handle state pair wherein current state is null'
66             def cmHandleTransitionPair = new LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair()
67             cmHandleTransitionPair.currentYangModelCmHandle = new YangModelCmHandle(compositeState: null)
68             cmHandleTransitionPair.targetYangModelCmHandle =  new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED))
69         when: 'method to update cm handle state metrics is called'
70             objectUnderTest.updateCmHandleStateMetrics([cmHandleTransitionPair])
71         then: 'cm handle by state cache map is called only once'
72             1 * cmHandlesByState.executeOnKey(_, _)
73     }
74
75     def 'Applying decreasing entry processor to a key on map where #scenario'() {
76         when: 'decreasing entry processor is applied to subtract 1 to the value'
77             entryProcessingMap.executeOnKey(key, new DecreasingEntryProcessor())
78         then: 'the new value is as expected'
79             assert entryProcessingMap.get(key) == expectedValue
80         where: 'the following data is used'
81             scenario                        | key                 || expectedValue
82             'current value of count is zero'| 'zeroCmHandlesCount'|| 0
83             'current value of count is >0'  | 'tenCmHandlesCount' || 9
84     }
85
86     def 'Applying increasing entry processor to a key on map'() {
87         when: 'increasing entry processor is applied to add 1 to the value'
88             entryProcessingMap.executeOnKey('tenCmHandlesCount', new IncreasingEntryProcessor())
89         then: 'the new value is as expected'
90             assert entryProcessingMap.get('tenCmHandlesCount') == 11
91     }
92 }