20b1c1c33587898f82d253ba1472d55a215591cf
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.trustlevel
22
23 import com.hazelcast.config.Config
24 import com.hazelcast.core.Hazelcast
25 import com.hazelcast.map.IMap
26 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.beans.factory.annotation.Qualifier
29 import org.springframework.boot.test.context.SpringBootTest
30 import spock.lang.Specification
31
32 @SpringBootTest(classes = [TrustLevelCacheConfig])
33 class TrustLevelCacheConfigSpec extends Specification {
34
35     @Autowired
36     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN)
37     private IMap<String, TrustLevel> trustLevelPerDmiPlugin
38
39     @Autowired
40     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE)
41     private IMap<String, TrustLevel> trustLevelPerCmHandleId
42
43     def cleanupSpec() {
44         Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').shutdown()
45     }
46
47     def 'Hazelcast cache for trust level per dmi plugin'() {
48         expect: 'system is able to create an instance of the trust level per dmi plugin cache'
49             assert null != trustLevelPerDmiPlugin
50         and: 'there is at least 1 instance'
51             assert Hazelcast.allHazelcastInstances.size() > 0
52         and: 'Dmi Plugin Trust Level Cache is present'
53             assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
54     }
55
56     def 'Hazelcast cache for trust level per cm handle'() {
57         expect: 'system is able to create an instance of the trust level per cm handle cache'
58             assert null != trustLevelPerCmHandleId
59         and: 'there is at least 1 instance'
60             assert Hazelcast.allHazelcastInstances.size() > 0
61         and: 'Hazelcast cache instance for trust level is present'
62             assert Hazelcast.allHazelcastInstances.name.contains('cps-and-ncmp-hazelcast-instance-test-config')
63     }
64
65     def 'Trust level cache configurations: #scenario'() {
66         given: 'retrieving the common cache config'
67             def cacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config
68         and: 'the cache config has the right cluster'
69             assert cacheConfig.clusterName == 'cps-and-ncmp-test-caches'
70         when: 'retrieving the map config for trustLevel'
71             def mapConfig = cacheConfig.mapConfigs.get(hazelcastMapConfigName)
72         then: 'the map config has the correct backup counts'
73             assert mapConfig.backupCount == 1
74             assert mapConfig.asyncBackupCount == 0
75         where: 'the following caches are used'
76             scenario         | hazelcastMapConfigName
77             'cmhandle map'   | 'trustLevelPerCmHandleCacheConfig'
78             'dmi plugin map' | 'trustLevelPerDmiPluginCacheConfig'
79     }
80
81     def 'Verify deployment network configs for Distributed Caches'() {
82         given: 'the Trust Level Per Dmi Plugin Cache config'
83             def trustLevelDmiPerPluginCacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
84         expect: 'system created instance with correct config'
85             assert trustLevelDmiPerPluginCacheConfig.join.autoDetectionConfig.enabled
86             assert !trustLevelDmiPerPluginCacheConfig.join.kubernetesConfig.enabled
87     }
88
89     def 'Verify deployment network configs for Cm Handle Distributed Caches'() {
90         given: 'the Trust Level Per Cm Handle Cache config'
91             def trustLevelPerCmHandlePluginCacheConfig = Hazelcast.getHazelcastInstanceByName('cps-and-ncmp-hazelcast-instance-test-config').config.networkConfig
92         expect: 'system created instance with correct config'
93             assert trustLevelPerCmHandlePluginCacheConfig.join.autoDetectionConfig.enabled
94             assert !trustLevelPerCmHandlePluginCacheConfig.join.kubernetesConfig.enabled
95     }
96
97     def 'Verify network config'() {
98         given: 'Synchronization config object and test configuration'
99             def objectUnderTest = new TrustLevelCacheConfig()
100             def testConfig = new Config()
101         when: 'kubernetes properties are enabled'
102             objectUnderTest.cacheKubernetesEnabled = true
103             objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
104         and: 'method called to update the discovery mode'
105             objectUnderTest.updateDiscoveryMode(testConfig)
106         then: 'applied properties are reflected'
107             assert testConfig.networkConfig.join.kubernetesConfig.enabled
108             assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
109     }
110
111 }