2 * ============LICENSE_START========================================================
3 * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
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 java.util.concurrent.BlockingQueue
27 import java.util.concurrent.TimeUnit
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.context.SpringBootTest
30 import org.springframework.test.context.ContextConfiguration
31 import spock.lang.Specification
32 import spock.util.concurrent.PollingConditions
35 @ContextConfiguration(classes = [SynchronizationCacheConfig])
36 class SynchronizationCacheConfigSpec extends Specification {
39 BlockingQueue<String> moduleSyncWorkQueue
42 IMap<String, Object> moduleSyncStartedOnCmHandles
45 IMap<String, Boolean> dataSyncSemaphores
48 Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').shutdown()
51 def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
52 expect: 'system is able to create an instance of the Module Sync Work Queue'
53 assert null != moduleSyncWorkQueue
54 and: 'system is able to create an instance of a map to hold cm handles which have started (and maybe finished) module sync'
55 assert null != moduleSyncStartedOnCmHandles
56 and: 'system is able to create an instance of a map to hold data sync semaphores'
57 assert null != dataSyncSemaphores
58 and: 'there is only one instance with the correct name'
59 assert Hazelcast.allHazelcastInstances.size() == 1
60 assert Hazelcast.allHazelcastInstances.name[0] == 'cps-and-ncmp-hazelcast-instance-test-config'
63 def 'Verify configs for Distributed objects'(){
64 given: 'hazelcast common config'
65 def hzConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config
66 and: 'the Module Sync Work Queue config'
67 def moduleSyncDefaultWorkQueueConfig = hzConfig.queueConfigs.get('defaultQueueConfig')
68 and: 'the Module Sync Started Cm Handle Map config'
69 def moduleSyncStartedOnCmHandlesMapConfig = hzConfig.mapConfigs.get('moduleSyncStartedConfig')
70 and: 'the Data Sync Semaphores Map config'
71 def dataSyncSemaphoresMapConfig = hzConfig.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 assert hzConfig.clusterName == 'cps-and-ncmp-test-caches'
85 def 'Verify deployment network configs for Distributed objects'() {
86 given: 'common hazelcast network config'
87 def hzConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
88 and: 'all configs has the correct settings'
89 assert hzConfig.join.autoDetectionConfig.enabled
90 assert !hzConfig.join.kubernetesConfig.enabled
93 def 'Verify network config'() {
94 given: 'Synchronization config object and test configuration'
95 def objectUnderTest = new SynchronizationCacheConfig()
96 def testConfig = new Config()
97 when: 'kubernetes properties are enabled'
98 objectUnderTest.cacheKubernetesEnabled = true
99 objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
100 and: 'method called to update the discovery mode'
101 objectUnderTest.updateDiscoveryMode(testConfig)
102 then: 'applied properties are reflected'
103 assert testConfig.networkConfig.join.kubernetesConfig.enabled
104 assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
107 def 'Time to Live Verify for Module Sync Semaphore'() {
108 when: 'the key is inserted with a TTL'
109 moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 100, TimeUnit.MILLISECONDS)
110 then: 'the entry expires within a second'
111 new PollingConditions().within(1) {
112 assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') == null
116 def 'Time to Live Verify for Data Sync Semaphore'() {
117 when: 'the key is inserted with a TTL'
118 dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 100, TimeUnit.MILLISECONDS)
119 then: 'the entry expires within a second'
120 new PollingConditions().within(1) {
121 assert dataSyncSemaphores.get('testKeyDataSync') == null