Merge "Modify INFO.yaml for committer rights"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / config / embeddedcache / SynchronizationCacheConfigSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  *  Copyright (C) 2022-2023 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.api.impl.config.embeddedcache
22
23 import com.hazelcast.config.Config
24 import com.hazelcast.core.Hazelcast
25 import com.hazelcast.map.IMap
26 import org.onap.cps.spi.model.DataNode
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.test.context.ContextConfiguration
30 import spock.lang.Specification
31 import java.util.concurrent.BlockingQueue
32 import java.util.concurrent.TimeUnit
33
34 @SpringBootTest
35 @ContextConfiguration(classes = [SynchronizationCacheConfig])
36 class SynchronizationCacheConfigSpec extends Specification {
37
38     @Autowired
39     private BlockingQueue<DataNode> moduleSyncWorkQueue
40
41     @Autowired
42     private IMap<String, Object> moduleSyncStartedOnCmHandles
43
44     @Autowired
45     private IMap<String, Boolean> dataSyncSemaphores
46
47     @Autowired
48     private IMap<String, Set<String>> moduleSetTagCache
49
50     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
51         expect: 'system is able to create an instance of the Module Sync Work Queue'
52             assert null != moduleSyncWorkQueue
53         and: 'system is able to create an instance of a map to hold cm handles which have started (and maybe finished) module sync'
54             assert null != moduleSyncStartedOnCmHandles
55         and: 'system is able to create an instance of a map to hold data sync semaphores'
56             assert null != dataSyncSemaphores
57         and: 'system is able to create an instance of a map to hold module set tags'
58             assert null != moduleSetTagCache
59             and: 'there are at least 4 instances'
60         assert Hazelcast.allHazelcastInstances.size() > 3
61         and: 'they have the correct names (in any order)'
62             assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores', 'moduleSetTags')
63     }
64
65     def 'Verify configs for Distributed objects'(){
66         given: 'the Module Sync Work Queue config'
67             def moduleSyncWorkQueueConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config
68             def moduleSyncDefaultWorkQueueConfig =  moduleSyncWorkQueueConfig.queueConfigs.get('defaultQueueConfig')
69         and: 'the Module Sync Started Cm Handle Map config'
70             def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config
71             def moduleSyncStartedOnCmHandlesMapConfig =  moduleSyncStartedOnCmHandlesConfig.mapConfigs.get('moduleSyncStartedConfig')
72         and: 'the Data Sync Semaphores Map config'
73             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
74             def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
75         and: 'the Module Set Tag Map config'
76             def moduleSetTagCacheConfig =  Hazelcast.getHazelcastInstanceByName('moduleSetTags').config
77             def moduleSetTagMapConfig =  moduleSetTagCacheConfig.mapConfigs.get('moduleSetTagCacheMapConfig')
78         expect: 'system created instance with correct config of Module Sync Work Queue'
79             assert moduleSyncDefaultWorkQueueConfig.backupCount == 3
80             assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 3
81         and: 'Module Sync Started Cm Handle Map has the correct settings'
82             assert moduleSyncStartedOnCmHandlesMapConfig.backupCount == 3
83             assert moduleSyncStartedOnCmHandlesMapConfig.asyncBackupCount == 3
84         and: 'Data Sync Semaphore Map has the correct settings'
85             assert dataSyncSemaphoresMapConfig.backupCount == 3
86             assert dataSyncSemaphoresMapConfig.asyncBackupCount == 3
87         and: 'Module Set Tag Map has the correct settings'
88             assert moduleSetTagMapConfig.backupCount == 3
89             assert moduleSetTagMapConfig.asyncBackupCount == 3
90         and: 'all instances are part of same cluster'
91             def testClusterName = 'cps-and-ncmp-test-caches'
92             assert moduleSyncWorkQueueConfig.clusterName == testClusterName
93             assert moduleSyncStartedOnCmHandlesConfig.clusterName == testClusterName
94             assert dataSyncSemaphoresConfig.clusterName == testClusterName
95             assert moduleSetTagCacheConfig.clusterName == testClusterName
96     }
97
98     def 'Verify deployment network configs for Distributed objects'() {
99         given: 'the Module Sync Work Queue config'
100             def queueNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.networkConfig
101         and: 'the Module Sync Started Cm Handle Map config'
102             def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
103         and: 'the Data Sync Semaphores Map config'
104             def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
105         and: 'the Module Set Tag Map config'
106             def moduleSetTagNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSetTags').config.networkConfig
107         expect: 'system created instance with correct config of Module Sync Work Queue'
108             assert queueNetworkConfig.join.autoDetectionConfig.enabled
109             assert !queueNetworkConfig.join.kubernetesConfig.enabled
110         and: 'Module Sync Started Cm Handle Map has the correct settings'
111             assert moduleSyncStartedOnCmHandlesNetworkConfig.join.autoDetectionConfig.enabled
112             assert !moduleSyncStartedOnCmHandlesNetworkConfig.join.kubernetesConfig.enabled
113         and: 'Data Sync Semaphore Map has the correct settings'
114             assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
115             assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
116         and: 'Module Set Tag Map has the correct settings'
117             assert moduleSetTagNetworkConfig.join.autoDetectionConfig.enabled
118             assert !moduleSetTagNetworkConfig.join.kubernetesConfig.enabled
119     }
120
121     def 'Verify network config'() {
122         given: 'Synchronization config object and test configuration'
123             def objectUnderTest = new SynchronizationCacheConfig()
124             def testConfig = new Config()
125         when: 'kubernetes properties are enabled'
126             objectUnderTest.cacheKubernetesEnabled = true
127             objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
128         and: 'method called to update the discovery mode'
129             objectUnderTest.updateDiscoveryMode(testConfig)
130         then: 'applied properties are reflected'
131             assert testConfig.networkConfig.join.kubernetesConfig.enabled
132             assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
133
134     }
135
136     def 'Time to Live Verify for Module Sync Semaphore'() {
137         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
138             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
139         then: 'the entry is present in the map'
140             assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
141         and: 'the entry expires in less then 2 seconds'
142             waitMax2SecondsForKeyExpiration(moduleSyncStartedOnCmHandles, 'testKeyModuleSync')
143     }
144
145     def 'Time to Live Verify for Data Sync Semaphore'() {
146         when: 'the key is inserted with a TTL of 1 second'
147             dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1, TimeUnit.SECONDS)
148         then: 'the entry is present in the map'
149             assert dataSyncSemaphores.get('testKeyDataSync') != null
150         and: 'the entry expires in less then 2 seconds'
151             waitMax2SecondsForKeyExpiration(dataSyncSemaphores, 'testKeyDataSync')
152     }
153
154     def 'Time to Live Verify for Module Set Tag'() {
155         when: 'the key is inserted with a TTL of 1 second'
156             moduleSetTagCache.put('testKeyModuleSetTag', ['module-set-tag'] as Set, 1, TimeUnit.SECONDS)
157         then: 'the entry is present in the map'
158             assert moduleSetTagCache.get('testKeyModuleSetTag') != null
159         and: 'the entry expires in less then 2 seconds'
160             waitMax2SecondsForKeyExpiration(moduleSetTagCache, 'testKeyModuleSetTag')
161     }
162
163     def waitMax2SecondsForKeyExpiration(map, key) {
164         def count = 0
165         while ( map.get(key)!=null && ++count <= 20 ) {
166             sleep(100)
167         }
168         return count < 20 // Should have expired in less the 20 x 100ms = 2 seconds!
169     }
170
171 }