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