b409a1a13995e2be869d5e2b37d58700cdd8f7f3
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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 com.hazelcast.core.Hazelcast
24 import com.hazelcast.map.IMap
25 import org.onap.cps.ncmp.api.inventory.models.CompositeState
26 import org.onap.cps.ncmp.impl.inventory.CmHandleQueryService
27 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
28 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.DecreasingEntryProcessor
29 import org.onap.cps.ncmp.impl.inventory.sync.lcm.CmHandleStateMonitor.IncreasingEntryProcessor
30 import org.onap.cps.ncmp.utils.events.NcmpInventoryModelOnboardingFinishedEvent
31 import spock.lang.Shared
32 import spock.lang.Specification
33
34 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.ADVISED
35 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.READY
36
37 class CmHandleStateMonitorSpec extends Specification {
38
39     def mockCmHandlesByState = Mock(IMap)
40     def mockCmHandleQueryService = Mock(CmHandleQueryService)
41     def objectUnderTest = new CmHandleStateMonitor(mockCmHandleQueryService, mockCmHandlesByState)
42
43     @Shared
44     def entryProcessingMap =  Hazelcast.newHazelcastInstance().getMap('entryProcessingMap')
45
46     def setup() {
47         entryProcessingMap.put('zeroCmHandlesCount', 0)
48         entryProcessingMap.put('tenCmHandlesCount', 10)
49     }
50
51     def cleanupSpec() {
52         Hazelcast.shutdownAll()
53     }
54
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)
67         where:
68             scenario                                 | queryResult                 || expectedValue
69             'query service returns zero cm handle id'| []                          || 0
70             'query service returns 1 cm handle id'   | ['someId']                  || 1
71     }
72
73
74     def 'Update cm handle state metric'() {
75         given: 'a collection of cm handle state pair'
76             def cmHandleTransitionPair = new CmHandleTransitionPair(new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED)),
77                                                                     new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: READY))
78             )
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', _)
84     }
85
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 CmHandleTransitionPair(new YangModelCmHandle(compositeState: null),
89                                                                     new YangModelCmHandle(compositeState: new CompositeState(cmHandleState: ADVISED)))
90         when: 'updating cm handle state metrics'
91             objectUnderTest.updateCmHandleStateMetrics([cmHandleTransitionPair])
92         then: 'cm handle by state cache map is called only once'
93             1 * mockCmHandlesByState.executeOnKey(_, _)
94     }
95
96     def 'Applying decreasing entry processor to a key on map where #scenario'() {
97         when: 'decreasing entry processor is applied to subtract 1 to the value'
98             entryProcessingMap.executeOnKey(key, new DecreasingEntryProcessor())
99         then: 'the new value is as expected'
100             assert entryProcessingMap.get(key) == expectedValue
101         where: 'the following data is used'
102             scenario                        | key                 || expectedValue
103             'current value of count is zero'| 'zeroCmHandlesCount'|| 0
104             'current value of count is >0'  | 'tenCmHandlesCount' || 9
105     }
106
107     def 'Applying increasing entry processor to a key on map'() {
108         when: 'increasing entry processor is applied to add 1 to the value'
109             entryProcessingMap.executeOnKey('tenCmHandlesCount', new IncreasingEntryProcessor())
110         then: 'the new value is as expected'
111             assert entryProcessingMap.get('tenCmHandlesCount') == 11
112     }
113 }