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
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.api.impl.config.embeddedcache
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
34 @ContextConfiguration(classes = [SynchronizationCacheConfig])
35 class SynchronizationCacheConfigSpec extends Specification {
38 private BlockingQueue<DataNode> moduleSyncWorkQueue
41 private IMap<String, Object> moduleSyncStartedOnCmHandles
44 private IMap<String, Boolean> dataSyncSemaphores
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' )
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
77 def 'Time to Live Verify for Module Sync and Data Sync Semaphore'() {
78 when: 'the keys are inserted with a TTL'
79 moduleSyncStartedOnCmHandles.put('testKeyModuleSync', 'toBeExpired' as Object, 1000, TimeUnit.MILLISECONDS)
80 dataSyncSemaphores.put('testKeyDataSync', Boolean.TRUE, 1000, TimeUnit.MILLISECONDS)
81 then: 'the entries are present in the map'
82 assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') != null
83 assert dataSyncSemaphores.get('testKeyDataSync') != null
84 and: 'we wait for the key expiration'
86 and: 'the keys should be expired as TTL elapsed'
87 assert moduleSyncStartedOnCmHandles.get('testKeyModuleSync') == null
88 assert dataSyncSemaphores.get('testKeyDataSync') == null