Remove 32K limit from queries with collection parameters
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / config / embeddedcache / SynchronizationCacheConfigSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  *  Copyright (C) 2022 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.api.impl.config.embeddedcache
22
23 import com.hazelcast.core.Hazelcast
24 import com.hazelcast.map.IMap
25 import org.onap.cps.spi.model.DataNode
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.boot.test.context.SpringBootTest
28 import org.springframework.test.context.ContextConfiguration
29 import spock.lang.Specification
30 import java.util.concurrent.BlockingQueue
31 import java.util.concurrent.TimeUnit
32
33 @SpringBootTest
34 @ContextConfiguration(classes = [SynchronizationCacheConfig])
35 class SynchronizationCacheConfigSpec extends Specification {
36
37     @Autowired
38     private BlockingQueue<DataNode> moduleSyncWorkQueue
39
40     @Autowired
41     private IMap<String, Object> moduleSyncStartedOnCmHandles
42
43     @Autowired
44     private IMap<String, Boolean> dataSyncSemaphores
45
46     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
47         expect: 'system is able to create an instance of the Module Sync Work Queue'
48             assert null != moduleSyncWorkQueue
49         and: 'system is able to create an instance of a map to hold cm handles which have started (and maybe finished) module sync'
50             assert null != moduleSyncStartedOnCmHandles
51         and: 'system is able to create an instance of a map to hold data sync semaphores'
52             assert null != dataSyncSemaphores
53         and: 'there 3 instances'
54             assert Hazelcast.allHazelcastInstances.size() == 3
55         and: 'they have the correct names (in any order)'
56             assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores' )
57     }
58
59     def 'Verify configs for Distributed objects'(){
60         given: 'the Module Sync Work Queue config'
61             def queueConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncWorkQueue').config.queueConfigs.get('defaultQueueConfig')
62         and: 'the Module Sync Started Cm Handle Map config'
63             def moduleSyncStartedOnCmHandlesConfig =  Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.mapConfigs.get('moduleSyncStartedConfig')
64         and: 'the Data Sync Semaphores Map config'
65             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.mapConfigs.get('dataSyncSemaphoresConfig')
66         expect: 'system created instance with correct config of Module Sync Work Queue'
67             assert queueConfig.backupCount == 3
68             assert queueConfig.asyncBackupCount == 3
69         and: 'Module Sync Started Cm Handle Map has the correct settings'
70             assert moduleSyncStartedOnCmHandlesConfig.backupCount == 3
71             assert moduleSyncStartedOnCmHandlesConfig.asyncBackupCount == 3
72         and: 'Data Sync Semaphore Map has the correct settings'
73             assert dataSyncSemaphoresConfig.backupCount == 3
74             assert dataSyncSemaphoresConfig.asyncBackupCount == 3
75     }
76
77     def 'Time to Live Verify for Module Sync Semaphore'() {
78         when: 'the key is inserted with a TTL of 1 second (Hazelcast TTL resolution is seconds!)'
79             moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1, TimeUnit.SECONDS)
80         then: 'the entry is present in the map'
81             assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
82         and: 'the entry expires in less then 2 seconds'
83             waitMax2SecondsForKeyExpiration(moduleSyncStartedOnCmHandles, 'testKeyModuleSync')
84     }
85
86     def 'Time to Live Verify for Data Sync Semaphore'() {
87         when: 'the key is inserted with a TTL of 1 second'
88             dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1, TimeUnit.SECONDS)
89         then: 'the entry is present in the map'
90             assert dataSyncSemaphores.get('testKeyDataSync') != null
91         and: 'the entry expires in less then 2 seconds'
92             waitMax2SecondsForKeyExpiration(dataSyncSemaphores, 'testKeyDataSync')
93     }
94
95     def waitMax2SecondsForKeyExpiration(map, key) {
96         def count = 0
97         while ( map.get(key)!=null && ++count <= 20 ) {
98             sleep(100)
99         }
100         return count < 20 // Should have expired in less the 20 x 100ms = 2 seconds!
101     }
102
103 }