Configure Hazelcast to have 1 backup to reduce memory
[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-2024 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 spock.util.concurrent.PollingConditions
32 import java.util.concurrent.BlockingQueue
33 import java.util.concurrent.TimeUnit
34
35 @SpringBootTest
36 @ContextConfiguration(classes = [SynchronizationCacheConfig])
37 class SynchronizationCacheConfigSpec extends Specification {
38
39     @Autowired
40     private BlockingQueue<DataNode> moduleSyncWorkQueue
41
42     @Autowired
43     private IMap<String, Object> moduleSyncStartedOnCmHandles
44
45     @Autowired
46     private IMap<String, Boolean> dataSyncSemaphores
47
48     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
49         expect: 'system is able to create an instance of the Module Sync Work Queue'
50             assert null != moduleSyncWorkQueue
51         and: 'system is able to create an instance of a map to hold cm handles which have started (and maybe finished) module sync'
52             assert null != moduleSyncStartedOnCmHandles
53         and: 'system is able to create an instance of a map to hold data sync semaphores'
54             assert null != dataSyncSemaphores
55         and: 'there are at least 3 instances'
56             assert Hazelcast.allHazelcastInstances.size() > 2
57         and: 'they have the correct names (in any order)'
58             assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores')
59     }
60
61     def 'Verify configs for Distributed objects'(){
62         given: 'the Module Sync Work Queue config'
63             def moduleSyncWorkQueueConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config
64             def moduleSyncDefaultWorkQueueConfig =  moduleSyncWorkQueueConfig.queueConfigs.get('defaultQueueConfig')
65         and: 'the Module Sync Started Cm Handle Map config'
66             def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config
67             def moduleSyncStartedOnCmHandlesMapConfig =  moduleSyncStartedOnCmHandlesConfig.mapConfigs.get('moduleSyncStartedConfig')
68         and: 'the Data Sync Semaphores Map config'
69             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
70             def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
71         expect: 'system created instance with correct config of Module Sync Work Queue'
72             assert moduleSyncDefaultWorkQueueConfig.backupCount == 1
73             assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 0
74         and: 'Module Sync Started Cm Handle Map has the correct settings'
75             assert moduleSyncStartedOnCmHandlesMapConfig.backupCount == 1
76             assert moduleSyncStartedOnCmHandlesMapConfig.asyncBackupCount == 0
77         and: 'Data Sync Semaphore Map has the correct settings'
78             assert dataSyncSemaphoresMapConfig.backupCount == 1
79             assert dataSyncSemaphoresMapConfig.asyncBackupCount == 0
80         and: 'all instances are part of same cluster'
81             def testClusterName = 'cps-and-ncmp-test-caches'
82             assert moduleSyncWorkQueueConfig.clusterName == testClusterName
83             assert moduleSyncStartedOnCmHandlesConfig.clusterName == testClusterName
84             assert dataSyncSemaphoresConfig.clusterName == testClusterName
85     }
86
87     def 'Verify deployment network configs for Distributed objects'() {
88         given: 'the Module Sync Work Queue config'
89             def queueNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.networkConfig
90         and: 'the Module Sync Started Cm Handle Map config'
91             def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
92         and: 'the Data Sync Semaphores Map config'
93             def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
94         expect: 'system created instance with correct config of Module Sync Work Queue'
95             assert queueNetworkConfig.join.autoDetectionConfig.enabled
96             assert !queueNetworkConfig.join.kubernetesConfig.enabled
97         and: 'Module Sync Started Cm Handle Map has the correct settings'
98             assert moduleSyncStartedOnCmHandlesNetworkConfig.join.autoDetectionConfig.enabled
99             assert !moduleSyncStartedOnCmHandlesNetworkConfig.join.kubernetesConfig.enabled
100         and: 'Data Sync Semaphore Map has the correct settings'
101             assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
102             assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
103     }
104
105     def 'Verify network config'() {
106         given: 'Synchronization config object and test configuration'
107             def objectUnderTest = new SynchronizationCacheConfig()
108             def testConfig = new Config()
109         when: 'kubernetes properties are enabled'
110             objectUnderTest.cacheKubernetesEnabled = true
111             objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
112         and: 'method called to update the discovery mode'
113             objectUnderTest.updateDiscoveryMode(testConfig)
114         then: 'applied properties are reflected'
115             assert testConfig.networkConfig.join.kubernetesConfig.enabled
116             assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
117
118     }
119
120     def 'Time to Live Verify for Module Sync Semaphore'() {
121         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
122             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
123         then: 'the entry is present in the map'
124             assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
125         and: 'the entry expires'
126             new PollingConditions().within(10) {
127                 assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') == null
128             }
129     }
130
131     def 'Time to Live Verify for Data Sync Semaphore'() {
132         when: 'the key is inserted with a TTL of 1 second'
133             dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1, TimeUnit.SECONDS)
134         then: 'the entry is present in the map'
135             assert dataSyncSemaphores.get('testKeyDataSync') != null
136         and: 'the entry expires'
137             new PollingConditions().within(10) {
138                 assert dataSyncSemaphores.get('testKeyDataSync') == null
139             }
140     }
141
142 }