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