03771e76677ecafa4c8f0f4a9bf3cd2685281002
[so.git] / adapters / mso-adapter-utils / src / test / java / org / openecomp / mso / adapter_utils / tests / CloudConfigTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.adapter_utils.tests;
22
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import java.util.Map;
32 import org.openecomp.mso.cloud.CloudConfig;
33 import org.openecomp.mso.cloud.CloudConfigFactory;
34 import org.openecomp.mso.cloud.CloudIdentity;
35 import org.openecomp.mso.cloud.CloudSite;
36 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
37
38
39
40 /**
41  * This class implements test methods of the CloudConfig features.
42  *
43  *
44  */
45 public class CloudConfigTest {
46
47         private static CloudConfig con;
48         private static CloudConfigFactory cloudConfigFactory= new CloudConfigFactory();
49
50         public CloudConfigTest () {
51
52         }
53
54         /**
55     * This method is called before any test occurs.
56     * It creates a fake tree from scratch
57          * @throws MsoCloudIdentityNotFound 
58     */
59    @Before
60    public final void prepare () throws MsoCloudIdentityNotFound {
61            ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
62            String config = classLoader.getResource("cloud_config.json").toString().substring(5);
63
64            cloudConfigFactory.initializeCloudConfig(config,1);
65            con = cloudConfigFactory.getCloudConfig();
66    }
67
68    /**
69     * This method implements a test for the getCloudConfig method.
70     */
71    @Test
72    public final void testGetCloudConfig () {
73            assertNotNull(con);
74    }
75
76    /**
77     * This method implements a test for the getCloudSites method.
78     */
79    @Test
80    public final void testGetCloudSites () {
81            Map<String,CloudSite> siteMap = con.getCloudSites();
82            assertNotNull(siteMap);
83
84            CloudSite site1 = siteMap.get("MT");
85            CloudSite site2 = siteMap.get("DAN");
86            CloudSite site3 = siteMap.get("MTINJVCC101");
87            CloudSite site4 = siteMap.get("MTSNJA4LCP1");
88
89            assertEquals (site1.getRegionId(), "regionOne");
90            assertEquals (site1.getIdentityServiceId(), "MT_KEYSTONE");
91            assertEquals (site2.getRegionId(), "RegionOne");
92            assertEquals (site2.getIdentityServiceId(), "DAN_KEYSTONE");
93            assertEquals (site3.getRegionId(), "regionTwo");
94            assertEquals (site3.getIdentityServiceId(), "MTINJVCC101_DCP");
95            assertEquals (site4.getRegionId(), "mtsnjlcp1");
96            assertEquals (site4.getIdentityServiceId(), "MTSNJA3DCP1");
97    }
98
99
100    /**
101     * This method implements a test for the getIdentityServices method.
102     */
103    @Test
104    public final void testGetIdentityServices () {
105            Map<String,CloudIdentity> identityMap = con.getIdentityServices ();
106            assertNotNull(identityMap);
107
108            CloudIdentity identity1 = identityMap.get("MT_KEYSTONE");
109            CloudIdentity identity2 = identityMap.get("DAN_KEYSTONE");
110            CloudIdentity identity3 = identityMap.get("MTINJVCC101_DCP");
111            CloudIdentity identity4 = identityMap.get("MTSNJA3DCP1");
112
113 //         assertEquals (identity1.getKeystoneUrl(), "http://localhost:5000/v2.0");
114 //         assertEquals (identity1.getIdentityUrl(), "http://localhost:5000/v2.0");
115            assertEquals (identity1.getMsoId(), "john");
116            assertEquals (identity1.getMsoPass(), "changeme");
117            assertEquals (identity1.getAdminTenant(), "admin");
118            assertEquals (identity1.getMemberRole(), "_member_");
119            assertEquals (identity1.hasTenantMetadata(), false);
120
121 //         assertEquals (identity2.getKeystoneUrl(), "http://localhost:5000/v2.0");
122 //         assertEquals (identity2.getIdentityUrl(), "http://localhost:5000/v2.0");
123            assertEquals (identity2.getMsoId(), "mockId");
124            assertEquals (identity2.getMsoPass(), "stack123");
125            assertEquals (identity2.getAdminTenant(), "service");
126            assertEquals (identity2.getMemberRole(), "_member_");
127            assertEquals (identity2.hasTenantMetadata(), false);
128
129 //         assertEquals (identity3.getKeystoneUrl(), "http://localhost:5000/v2.0");
130 //         assertEquals (identity3.getIdentityUrl(), "http://localhost:5000/v2.0");
131            assertEquals (identity3.getMsoId(), "mockIdToo");
132            assertEquals (identity3.getMsoPass(), "AICG@mm@@2015");
133            assertEquals (identity3.getAdminTenant(), "service");
134            assertEquals (identity3.getMemberRole(), "admin");
135            assertEquals (identity3.hasTenantMetadata(), true);
136
137 //         assertEquals (identity4.getKeystoneUrl(), "https://localhost:5000/v2.0");
138 //         assertEquals (identity4.getIdentityUrl(), "https://localhost:5000/v2.0");
139            assertEquals (identity4.getMsoId(), "mockIdToo");
140            assertEquals (identity4.getMsoPass(), "2315QRS2015srq");
141            assertEquals (identity4.getAdminTenant(), "service");
142            assertEquals (identity4.getMemberRole(), "admin");
143            assertEquals (identity4.hasTenantMetadata(), true);
144
145    }
146
147    /**
148     * This method implements a test for the getCloudSite method.
149     */
150    @Test
151    public final void testGetCloudSite () {
152            CloudSite site1  = con.getCloudSite("MT");
153            assertNotNull(site1);
154            assertEquals (site1.getRegionId(), "regionOne");
155            assertEquals (site1.getIdentityServiceId(), "MT_KEYSTONE");
156    }
157
158    /**
159     * This method implements a test for the getIdentityService method.
160     */
161    @Test
162    public final void testGetIdentityService () {
163            CloudIdentity identity1  = con.getIdentityService("MT_KEYSTONE");
164            assertNotNull(identity1);
165 //         assertEquals (identity1.getKeystoneUrl(), "http://localhost:5000/v2.0");
166 //         assertEquals (identity1.getIdentityUrl(), "http://localhost:5000/v2.0");
167            assertEquals (identity1.getMsoId(), "john");
168            assertEquals (identity1.getMsoPass(), "changeme");
169            assertEquals (identity1.getAdminTenant(), "admin");
170            assertEquals (identity1.getMemberRole(), "_member_");
171            assertEquals (identity1.hasTenantMetadata(), false);
172
173            CloudIdentity identity2  = con.getIdentityService("Test");
174            assertNull(identity2);
175    }
176    
177    @Test (expected = MsoCloudIdentityNotFound.class)
178    public final void testLoadWithWrongFile () throws MsoCloudIdentityNotFound {
179        ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
180        String config = classLoader.getResource("cloud_config_bad.json").toString().substring(5);
181
182        cloudConfigFactory.initializeCloudConfig(config,1);
183    }
184    
185    @Test
186    public final void testReloadWithWrongFile () {
187        ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
188        String config = classLoader.getResource("cloud_config_bad.json").toString().substring(5);
189
190        try {
191            cloudConfigFactory.initializeCloudConfig(config,1);
192            Assert.fail("MsoCloudIdentityNotFound was expected");
193        } catch (MsoCloudIdentityNotFound e) {
194            
195        }
196        Assert.assertTrue("Should be an empty CloudConfig", cloudConfigFactory.getCloudConfig().getCloudSites().isEmpty());
197        Assert.assertTrue("Should be an empty CloudConfig", cloudConfigFactory.getCloudConfig().getIdentityServices().isEmpty());
198        
199        // Now reload the right config
200        config = classLoader.getResource("cloud_config.json").toString().substring(5);
201        cloudConfigFactory.changeMsoPropertiesFilePath(config);
202        cloudConfigFactory.reloadCloudConfig();
203        Assert.assertTrue("Flag valid Config should be true now that the cloud_config is correct", cloudConfigFactory.getCloudConfig().isValidCloudConfig());
204
205    }
206
207 }