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