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