Fix the compilation issue.
[so.git] / adapters / mso-adapter-utils / src / test / java / org / openecomp / mso / adapter_utils / tests / CloudConfigTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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  * This class implements test methods of the CloudConfig features.
40  */
41 public class CloudConfigTest {
42
43         private static CloudConfig con;
44         private static CloudConfigFactory cloudConfigFactory= new CloudConfigFactory();
45
46         public CloudConfigTest () {
47
48         }
49
50         /**
51     * This method is called before any test occurs.
52     * It creates a fake tree from scratch
53          * @throws MsoCloudIdentityNotFound 
54     */
55    @Before
56    public final void prepare () throws MsoCloudIdentityNotFound {
57            ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
58            String config = classLoader.getResource("cloud_config.json").toString().substring(5);
59
60            cloudConfigFactory.initializeCloudConfig(config,1);
61            con = cloudConfigFactory.getCloudConfig();
62    }
63
64    /**
65     * This method implements a test for the getCloudConfig method.
66     */
67    @Test
68    public final void testGetCloudConfig () {
69            assertNotNull(con);
70    }
71
72    /**
73     * This method implements a test for the getCloudSites method.
74     */
75    @Test
76    public final void testGetCloudSites () {
77            Map<String,CloudSite> siteMap = con.getCloudSites();
78            assertNotNull(siteMap);
79
80            CloudSite site1 = siteMap.get("MT");
81            CloudSite site2 = siteMap.get("DAN");
82            CloudSite site3 = siteMap.get("MTINJVCC101");
83            CloudSite site4 = siteMap.get("MTSNJA4LCP1");
84
85            assertEquals (site1.getRegionId(), "regionOne");
86            assertEquals (site1.getIdentityServiceId(), "MT_KEYSTONE");
87            assertEquals (site2.getRegionId(), "RegionOne");
88            assertEquals (site2.getIdentityServiceId(), "DAN_KEYSTONE");
89            assertEquals (site3.getRegionId(), "regionTwo");
90            assertEquals (site3.getIdentityServiceId(), "MTINJVCC101_DCP");
91            assertEquals (site4.getRegionId(), "mtsnjlcp1");
92            assertEquals (site4.getIdentityServiceId(), "MTSNJA3DCP1");
93    }
94
95
96    /**
97     * This method implements a test for the getIdentityServices method.
98     */
99    @Test
100    public final void testGetIdentityServices () {
101            Map<String,CloudIdentity> identityMap = con.getIdentityServices ();
102            assertNotNull(identityMap);
103
104            CloudIdentity identity1 = identityMap.get("MT_KEYSTONE");
105            CloudIdentity identity2 = identityMap.get("DAN_KEYSTONE");
106            CloudIdentity identity3 = identityMap.get("MTINJVCC101_DCP");
107            CloudIdentity identity4 = identityMap.get("MTSNJA3DCP1");
108
109            assertEquals("john", identity1.getMsoId());
110            assertEquals("changeme", identity1.getMsoPass());
111            assertEquals("admin", identity1.getAdminTenant());
112            assertEquals("_member_", identity1.getMemberRole());
113            assertEquals(false, identity1.hasTenantMetadata());
114
115            assertEquals("mockId", identity2.getMsoId());
116            assertEquals("stack123", identity2.getMsoPass());
117            assertEquals("service", identity2.getAdminTenant());
118            assertEquals("_member_", identity2.getMemberRole());
119            assertEquals(false, identity2.hasTenantMetadata());
120
121            assertEquals("mockIdToo", identity3.getMsoId());
122            assertEquals("AICG@mm@@2015", identity3.getMsoPass());
123            assertEquals("service", identity3.getAdminTenant());
124            assertEquals("admin", identity3.getMemberRole());
125            assertEquals(true, identity3.hasTenantMetadata());
126
127            assertEquals("mockIdToo", identity4.getMsoId());
128            assertEquals("2315QRS2015srq", identity4.getMsoPass());
129            assertEquals("service", identity4.getAdminTenant());
130            assertEquals("admin", identity4.getMemberRole());
131            assertEquals(true, identity4.hasTenantMetadata());
132
133    }
134
135    /**
136     * This method implements a test for the getCloudSite method.
137     */
138    @Test
139    public final void testGetCloudSite () {
140            CloudSite site1  = con.getCloudSite("MT");
141            assertNotNull(site1);
142            assertEquals (site1.getRegionId(), "regionOne");
143            assertEquals (site1.getIdentityServiceId(), "MT_KEYSTONE");
144    }
145
146    /**
147     * This method implements a test for the getIdentityService method.
148     */
149    @Test
150    public final void testGetIdentityService () {
151            CloudIdentity identity1  = con.getIdentityService("MT_KEYSTONE");
152            assertNotNull(identity1);
153            assertEquals (identity1.getMsoId(), "john");
154            assertEquals (identity1.getMsoPass(), "changeme");
155            assertEquals (identity1.getAdminTenant(), "admin");
156            assertEquals (identity1.getMemberRole(), "_member_");
157            assertEquals (identity1.hasTenantMetadata(), false);
158
159            CloudIdentity identity2  = con.getIdentityService("Test");
160            assertNull(identity2);
161    }
162    
163    @Test (expected = MsoCloudIdentityNotFound.class)
164    public final void testLoadWithWrongFile () throws MsoCloudIdentityNotFound {
165        ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
166        String config = classLoader.getResource("cloud_config_bad.json").toString().substring(5);
167
168        cloudConfigFactory.initializeCloudConfig(config,1);
169    }
170    
171    @Test
172    public final void testReloadWithWrongFile () {
173        ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
174        String config = classLoader.getResource("cloud_config_bad.json").toString().substring(5);
175
176        try {
177            cloudConfigFactory.initializeCloudConfig(config,1);
178            Assert.fail("MsoCloudIdentityNotFound was expected");
179        } catch (MsoCloudIdentityNotFound e) {
180            
181        }
182        Assert.assertTrue("Should be an empty CloudConfig", cloudConfigFactory.getCloudConfig().getCloudSites().isEmpty());
183        Assert.assertTrue("Should be an empty CloudConfig", cloudConfigFactory.getCloudConfig().getIdentityServices().isEmpty());
184        
185        // Now reload the right config
186        config = classLoader.getResource("cloud_config.json").toString().substring(5);
187        cloudConfigFactory.changeMsoPropertiesFilePath(config);
188        cloudConfigFactory.reloadCloudConfig();
189        Assert.assertTrue("Flag valid Config should be true now that the cloud_config is correct", cloudConfigFactory.getCloudConfig().isValidCloudConfig());
190
191    }
192
193 }