aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/test/java/org/openecomp/mso/cloud/CloudConfigTest.java
blob: b213bbd90b8c8e4e6477674f2ea3264bda702460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.cloud;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Optional;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Map;

import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;

public class CloudConfigTest {

    private static String cloudConfigJsonFilePath;
    private static String cloudDefaultConfigJsonFilePath;
    private static String cloudConfigInvalidJsonFilePath;

    @BeforeClass
    public static void preparePaths() {
        ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
        cloudConfigJsonFilePath = classLoader.getResource("cloud_config.json").getPath();
        cloudDefaultConfigJsonFilePath = classLoader.getResource("cloud_default_config.json").getPath();
        cloudConfigInvalidJsonFilePath = classLoader.getResource("cloud_config_bad.json").getPath();
    }

    private CloudConfig createTestObject(String jsonFilePath) throws MsoCloudIdentityNotFound {
        CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
        cloudConfigFactory.initializeCloudConfig(jsonFilePath, 1);
        return cloudConfigFactory.getCloudConfig();
    }

    @Test
    public void testGetCloudSites() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudConfigJsonFilePath);
        Map<String, CloudSite> siteMap = con.getCloudSites();
        assertNotNull(siteMap);

        CloudSite site1 = siteMap.get("MT");
        CloudSite site2 = siteMap.get("DAN");
        CloudSite site3 = siteMap.get("MTINJVCC101");
        CloudSite site4 = siteMap.get("MTSNJA4LCP1");

        assertEquals("regionOne", site1.getRegionId());
        assertEquals("MT_KEYSTONE", site1.getIdentityServiceId());
        assertEquals("RegionOne", site2.getRegionId());
        assertEquals("DAN_KEYSTONE", site2.getIdentityServiceId());
        assertEquals("regionTwo", site3.getRegionId());
        assertEquals("MTINJVCC101_DCP", site3.getIdentityServiceId());
        assertEquals("mtsnjlcp1", site4.getRegionId());
        assertEquals("MTSNJA3DCP1", site4.getIdentityServiceId());
    }

    @Test
    public void testGetIdentityServices() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudConfigJsonFilePath);
        Map<String, CloudIdentity> identityMap = con.getIdentityServices();
        assertNotNull(identityMap);

        CloudIdentity identity1 = identityMap.get("MT_KEYSTONE");
        CloudIdentity identity2 = identityMap.get("DAN_KEYSTONE");
        CloudIdentity identity3 = identityMap.get("MTINJVCC101_DCP");
        CloudIdentity identity4 = identityMap.get("MTSNJA3DCP1");

        assertEquals("john", identity1.getMsoId());
        assertEquals("changeme", identity1.getMsoPass());
        assertEquals("admin", identity1.getAdminTenant());
        assertEquals("_member_", identity1.getMemberRole());
        assertFalse(identity1.hasTenantMetadata());

        assertEquals("mockId", identity2.getMsoId());
        assertEquals("stack123", identity2.getMsoPass());
        assertEquals("service", identity2.getAdminTenant());
        assertEquals("_member_", identity2.getMemberRole());
        assertFalse(identity2.hasTenantMetadata());

        assertEquals("mockIdToo", identity3.getMsoId());
        assertEquals("AICG@mm@@2015", identity3.getMsoPass());
        assertEquals("service", identity3.getAdminTenant());
        assertEquals("admin", identity3.getMemberRole());
        assertTrue(identity3.hasTenantMetadata());

        assertEquals("mockIdToo", identity4.getMsoId());
        assertEquals("2315QRS2015srq", identity4.getMsoPass());
        assertEquals("service", identity4.getAdminTenant());
        assertEquals("admin", identity4.getMemberRole());
        assertTrue(identity4.hasTenantMetadata());
    }

    @Test
    public void cloudSiteIsGotById_when_IdFound() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudConfigJsonFilePath);
        Optional<CloudSite> cloudSite = con.getCloudSite("MT");
        assertTrue(cloudSite.isPresent());
        assertEquals("regionOne", cloudSite.get().getRegionId());
        assertEquals("MT_KEYSTONE", cloudSite.get().getIdentityServiceId());
    }

    @Test
    public void cloudSiteIsGotByClli_when_IdNotFound() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudConfigJsonFilePath);
        Optional<CloudSite> cloudSite = con.getCloudSite("CS_clli");
        assertTrue(cloudSite.isPresent());
        assertEquals("clliRegion", cloudSite.get().getRegionId());
        assertEquals("CS_clli", cloudSite.get().getClli());
        assertEquals("CS_service", cloudSite.get().getIdentityServiceId());
    }

    @Test
    public void cloudSiteIsGotByDefault_when_IdAndClliNotFound() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudDefaultConfigJsonFilePath);
        Optional<CloudSite> cloudSite = con.getCloudSite("not_existing_id");
        assertTrue(cloudSite.isPresent());
        assertEquals("not_existing_id", cloudSite.get().getId());
        assertEquals("not_existing_id", cloudSite.get().getRegionId());
    }

    @Test
    public void testGetIdentityService() throws MsoCloudIdentityNotFound {
        CloudConfig con = createTestObject(cloudConfigJsonFilePath);
        CloudIdentity identity1 = con.getIdentityService("MT_KEYSTONE");
        assertNotNull(identity1);
        assertEquals("john", identity1.getMsoId());
        assertEquals("changeme", identity1.getMsoPass());
        assertEquals("admin", identity1.getAdminTenant());
        assertEquals("_member_", identity1.getMemberRole());
        assertFalse(identity1.hasTenantMetadata());

        CloudIdentity identity2 = con.getIdentityService("Test");
        assertNull(identity2);
    }

    @Test(expected = MsoCloudIdentityNotFound.class)
    public void testLoadWithWrongFile() throws MsoCloudIdentityNotFound {
        createTestObject(cloudConfigInvalidJsonFilePath);
    }

    @Test
    public void testReloadWithWrongFile() {
        CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
        try {
            cloudConfigFactory.initializeCloudConfig(cloudConfigInvalidJsonFilePath, 1);
            Assert.fail("MsoCloudIdentityNotFound was expected");
        } catch (MsoCloudIdentityNotFound e) {

        }
        assertTrue("Should be an empty CloudConfig", cloudConfigFactory.getCloudConfig().getCloudSites().isEmpty());
        assertTrue("Should be an empty CloudConfig",
                cloudConfigFactory.getCloudConfig().getIdentityServices().isEmpty());
        // Now reload the right config
        cloudConfigFactory.changeMsoPropertiesFilePath(cloudConfigJsonFilePath);
        cloudConfigFactory.reloadCloudConfig();
        assertTrue("Flag valid Config should be true now that the cloud_config is correct",
                cloudConfigFactory.getCloudConfig().isValidCloudConfig());
    }

}