Merge "Orphaned modules are removed from the DB before cmhandle removal"
[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     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
48         expect: 'system is able to create an instance of the Module Sync Work Queue'
49             assert null != moduleSyncWorkQueue
50         and: 'system is able to create an instance of a map to hold cm handles which have started (and maybe finished) module sync'
51             assert null != moduleSyncStartedOnCmHandles
52         and: 'system is able to create an instance of a map to hold data sync semaphores'
53             assert null != dataSyncSemaphores
54         and: 'there are at least 3 instances'
55             assert Hazelcast.allHazelcastInstances.size() > 2
56         and: 'they have the correct names (in any order)'
57             assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores')
58     }
59
60     def 'Verify configs for Distributed objects'(){
61         given: 'the Module Sync Work Queue config'
62             def moduleSyncWorkQueueConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config
63             def moduleSyncDefaultWorkQueueConfig =  moduleSyncWorkQueueConfig.queueConfigs.get('defaultQueueConfig')
64         and: 'the Module Sync Started Cm Handle Map config'
65             def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config
66             def moduleSyncStartedOnCmHandlesMapConfig =  moduleSyncStartedOnCmHandlesConfig.mapConfigs.get('moduleSyncStartedConfig')
67         and: 'the Data Sync Semaphores Map config'
68             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
69             def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
70         expect: 'system created instance with correct config of Module Sync Work Queue'
71             assert moduleSyncDefaultWorkQueueConfig.backupCount == 3
72             assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 3
73         and: 'Module Sync Started Cm Handle Map has the correct settings'
74             assert moduleSyncStartedOnCmHandlesMapConfig.backupCount == 3
75             assert moduleSyncStartedOnCmHandlesMapConfig.asyncBackupCount == 3
76         and: 'Data Sync Semaphore Map has the correct settings'
77             assert dataSyncSemaphoresMapConfig.backupCount == 3
78             assert dataSyncSemaphoresMapConfig.asyncBackupCount == 3
79         and: 'all instances are part of same cluster'
80             def testClusterName = 'cps-and-ncmp-test-caches'
81             assert moduleSyncWorkQueueConfig.clusterName == testClusterName
82             assert moduleSyncStartedOnCmHandlesConfig.clusterName == testClusterName
83             assert dataSyncSemaphoresConfig.clusterName == testClusterName
84     }
85
86     def 'Verify deployment network configs for Distributed objects'() {
87         given: 'the Module Sync Work Queue config'
88             def queueNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.networkConfig
89         and: 'the Module Sync Started Cm Handle Map config'
90             def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
91         and: 'the Data Sync Semaphores Map config'
92             def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
93         expect: 'system created instance with correct config of Module Sync Work Queue'
94             assert queueNetworkConfig.join.autoDetectionConfig.enabled
95             assert !queueNetworkConfig.join.kubernetesConfig.enabled
96         and: 'Module Sync Started Cm Handle Map has the correct settings'
97             assert moduleSyncStartedOnCmHandlesNetworkConfig.join.autoDetectionConfig.enabled
98             assert !moduleSyncStartedOnCmHandlesNetworkConfig.join.kubernetesConfig.enabled
99         and: 'Data Sync Semaphore Map has the correct settings'
100             assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
101             assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
102     }
103
104     def 'Verify network config'() {
105         given: 'Synchronization config object and test configuration'
106             def objectUnderTest = new SynchronizationCacheConfig()
107             def testConfig = new Config()
108         when: 'kubernetes properties are enabled'
109             objectUnderTest.cacheKubernetesEnabled = true
110             objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
111         and: 'method called to update the discovery mode'
112             objectUnderTest.updateDiscoveryMode(testConfig)
113         then: 'applied properties are reflected'
114             assert testConfig.networkConfig.join.kubernetesConfig.enabled
115             assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
116
117     }
118
119     def 'Time to Live Verify for Module Sync Semaphore'() {
120         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
121             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
122         then: 'the entry is present in the map'
123             assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
124         and: 'the entry expires in less then 2 seconds'
125             waitMax2SecondsForKeyExpiration(moduleSyncStartedOnCmHandles, 'testKeyModuleSync')
126     }
127
128     def 'Time to Live Verify for Data Sync Semaphore'() {
129         when: 'the key is inserted with a TTL of 1 second'
130             dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1, TimeUnit.SECONDS)
131         then: 'the entry is present in the map'
132             assert dataSyncSemaphores.get('testKeyDataSync') != null
133         and: 'the entry expires in less then 2 seconds'
134             waitMax2SecondsForKeyExpiration(dataSyncSemaphores, 'testKeyDataSync')
135     }
136
137     def waitMax2SecondsForKeyExpiration(map, key) {
138         def count = 0
139         while ( map.get(key)!=null && ++count <= 20 ) {
140             sleep(100)
141         }
142         return count < 20 // Should have expired in less the 20 x 100ms = 2 seconds!
143     }
144
145 }