Extend capability of distributed cache
[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 3 instances'
55             assert Hazelcast.allHazelcastInstances.size() == 3
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 queueConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.queueConfigs.get('defaultQueueConfig')
63         and: 'the Module Sync Started Cm Handle Map config'
64             def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.mapConfigs.get('moduleSyncStartedConfig')
65         and: 'the Data Sync Semaphores Map config'
66             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.mapConfigs.get('dataSyncSemaphoresConfig')
67         expect: 'system created instance with correct config of Module Sync Work Queue'
68             assert queueConfig.backupCount == 3
69             assert queueConfig.asyncBackupCount == 3
70         and: 'Module Sync Started Cm Handle Map has the correct settings'
71             assert moduleSyncStartedOnCmHandlesConfig.backupCount == 3
72             assert moduleSyncStartedOnCmHandlesConfig.asyncBackupCount == 3
73         and: 'Data Sync Semaphore Map has the correct settings'
74             assert dataSyncSemaphoresConfig.backupCount == 3
75             assert dataSyncSemaphoresConfig.asyncBackupCount == 3
76     }
77
78     def 'Verify deployment network configs for Distributed objects'() {
79         given: 'the Module Sync Work Queue config'
80             def queueNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.networkConfig
81         and: 'the Module Sync Started Cm Handle Map config'
82             def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
83         and: 'the Data Sync Semaphores Map config'
84             def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
85         expect: 'system created instance with correct config of Module Sync Work Queue'
86             assert queueNetworkConfig.join.autoDetectionConfig.enabled
87             assert !queueNetworkConfig.join.kubernetesConfig.enabled
88         and: 'Module Sync Started Cm Handle Map has the correct settings'
89             assert moduleSyncStartedOnCmHandlesNetworkConfig.join.autoDetectionConfig.enabled
90             assert !moduleSyncStartedOnCmHandlesNetworkConfig.join.kubernetesConfig.enabled
91         and: 'Data Sync Semaphore Map has the correct settings'
92             assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
93             assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
94
95     }
96
97     def 'Verify network config'() {
98         given: 'Synchronization config object and test configuration'
99             def objectUnderTest = new SynchronizationCacheConfig()
100             def testConfig = new Config()
101         when: 'kubernetes properties are enabled'
102             objectUnderTest.cacheKubernetesEnabled = true
103             objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
104         and: 'method called to update the discovery mode'
105             objectUnderTest.updateDiscoveryMode(testConfig)
106         then: 'applied properties are reflected'
107             assert testConfig.networkConfig.join.kubernetesConfig.enabled
108             assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
109
110     }
111
112     def 'Time to Live Verify for Module Sync Semaphore'() {
113         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
114             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
115         then: 'the entry is present in the map'
116             assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
117         and: 'the entry expires in less then 2 seconds'
118             waitMax2SecondsForKeyExpiration(moduleSyncStartedOnCmHandles, 'testKeyModuleSync')
119     }
120
121     def 'Time to Live Verify for Data Sync Semaphore'() {
122         when: 'the key is inserted with a TTL of 1 second'
123             dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1, TimeUnit.SECONDS)
124         then: 'the entry is present in the map'
125             assert dataSyncSemaphores.get('testKeyDataSync') != null
126         and: 'the entry expires in less then 2 seconds'
127             waitMax2SecondsForKeyExpiration(dataSyncSemaphores, 'testKeyDataSync')
128     }
129
130     def waitMax2SecondsForKeyExpiration(map, key) {
131         def count = 0
132         while ( map.get(key)!=null && ++count <= 20 ) {
133             sleep(100)
134         }
135         return count < 20 // Should have expired in less the 20 x 100ms = 2 seconds!
136     }
137
138 }