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