5b6be03a22b1e9df11b1f50307b52c019117a09d
[so.git] / adapters / mso-adapter-utils / src / test / java / org / openecomp / mso / cloud / 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.cloud;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Optional;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
35
36 public class CloudConfigTest {
37
38     private static final int NUMBER_OF_CLOUD_SITES_IN_JSON_FILE = 4;
39     private static final int NUMBER_OF_IDENTITY_SERVICES_IN_JSON_FILE = 4;
40     private static final String CLOUD_SITES_FIELD_NAME = "cloudSites";
41     private static final String IDENTITY_SERVICE_FIELD_NAME = "identityServices";
42     private static final String CLOUD_SITE_DEFAULT = "default";
43     private static final String CLOUD_CONFIG_JSON_FILE_NAME = "cloud_config.json";
44     private static final String CLOUD_CONFIG_INVALID_JSON_FILE_NAME = "cloud_config_bad.json";
45
46     private CloudConfig testedObject;
47     private CloudSite cloudSite;
48     private CloudSite cloudSiteDefault;
49
50     @Before
51     public void init() {
52         testedObject = new CloudConfig();
53     }
54
55     @Test
56     public void cloudSite_returnEmptyOptionalIfIdIsNull() {
57         Optional<CloudSite> cloudConfigOpt = new CloudConfig().getCloudSite(null);
58         assertThat(cloudConfigOpt).isEmpty();
59     }
60
61     @Test
62     public void cloudSiteIsGotById_when_IdFound() throws NoSuchFieldException, IllegalAccessException {
63         setCloudSitesMap();
64         Optional<CloudSite> cloudSiteOpt = testedObject.getCloudSite(cloudSite.getId());
65         assertThat(cloudSiteOpt).isPresent();
66         assertThat(cloudSiteOpt.get().getId()).isEqualTo(cloudSite.getId());
67         assertThat(cloudSiteOpt.get().getClli()).isEqualTo(cloudSite.getClli());
68     }
69
70     @Test
71     public void cloudSiteIsGotByClli_when_IdNotFound() throws NoSuchFieldException, IllegalAccessException {
72         setCloudSitesMap();
73         Optional<CloudSite> cloudSiteOpt = testedObject.getCloudSite(cloudSite.getClli());
74         assertTrue(cloudSiteOpt.isPresent());
75         assertThat(cloudSiteOpt.get().getId()).isEqualTo(cloudSite.getId());
76         assertThat(cloudSiteOpt.get().getClli()).isEqualTo(cloudSite.getClli());
77     }
78
79     @Test
80     public void cloudSiteIsGotByDefault_when_IdAndClliNotFound() throws NoSuchFieldException, IllegalAccessException {
81         setCloudSitesMap();
82         Optional<CloudSite> cloudSiteOpt = testedObject.getCloudSite("not_existing_id");
83         assertTrue(cloudSiteOpt.isPresent());
84         assertThat(cloudSiteOpt.get().getId()).isEqualTo("not_existing_id");
85         assertThat(cloudSiteOpt.get().getClli()).isEqualTo(cloudSiteDefault.getClli());
86     }
87
88     @Test
89     public void cloudSiteNotFound_returnNull() {
90         assertThat(testedObject.getCloudSite("not_existing_id")).isEmpty();
91     }
92
93     @Test
94     public void identityServiceFoundById() throws NoSuchFieldException, IllegalAccessException {
95         CloudIdentity cloudIdentity = createCloudIdentity();
96         setIdentityServiceMap();
97         CloudIdentity cloudIdentityResult = testedObject.getIdentityService(cloudIdentity.getId());
98
99         assertThat(cloudIdentityResult).isNotNull();
100         assertThat(cloudIdentityResult.getId()).isEqualTo(cloudIdentity.getId());
101         assertThat(cloudIdentityResult.getMsoId()).isEqualTo(cloudIdentity.getMsoId());
102     }
103
104     @Test
105     public void defaultClodeSiteNotFound_returnNull() {
106         assertThat(testedObject.getIdentityService("not_existing_id")).isNull();
107     }
108
109     @Test
110     public void loadCloudConfigSuccessful() throws IOException, MsoCloudIdentityNotFound {
111         ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
112         String cloudConfigJsonFilePath = classLoader.getResource(CLOUD_CONFIG_JSON_FILE_NAME).getPath();
113         testedObject.loadCloudConfig(cloudConfigJsonFilePath, 1);
114         assertThat(testedObject.isValidCloudConfig()).isTrue();
115         checkCloudSites();
116         checkIdentityServices();
117     }
118
119     @Test
120     public void loadCloudConfig_cloudIdentityNotFound() {
121         ClassLoader classLoader = CloudConfigTest.class.getClassLoader();
122         String cloudConfigInvalidJsonFilePath = classLoader.getResource(CLOUD_CONFIG_INVALID_JSON_FILE_NAME).getPath();
123         assertThatThrownBy(() -> testedObject.loadCloudConfig(cloudConfigInvalidJsonFilePath, 1))
124                 .isInstanceOf(MsoCloudIdentityNotFound.class)
125                 .hasMessage("Cloud Identity [MT Cloud site refers to a non-existing identity service: "
126                         + "MT_KEYSTONE_NOT_EXISTING] not found");
127         assertThat(testedObject.isValidCloudConfig()).isFalse();
128     }
129
130     private void checkCloudSites() {
131         Map<String, CloudSite> siteMap = testedObject.getCloudSites();
132         assertThat(siteMap).isNotEmpty().hasSize(NUMBER_OF_CLOUD_SITES_IN_JSON_FILE);
133         CloudSite site1 = siteMap.get("MT");
134         CloudSite site2 = siteMap.get("DAN");
135         CloudSite site3 = siteMap.get("MTINJVCC101");
136         CloudSite site4 = siteMap.get("MTSNJA4LCP1");
137
138         assertThat(site1.getId()).isEqualTo("MT");
139         assertThat(site1.getRegionId()).isEqualTo("regionOne");
140         assertThat(site1.getIdentityServiceId()).isEqualTo("MT_KEYSTONE");
141         assertThat(site1.getIdentityService()).isNotNull();
142         assertThat(site1.getIdentityService().getId()).isEqualTo(site1.getIdentityServiceId());
143
144         assertThat(site2.getId()).isEqualTo("DAN");
145         assertThat(site2.getRegionId()).isEqualTo("RegionOne");
146         assertThat(site2.getIdentityServiceId()).isEqualTo("DAN_KEYSTONE");
147         assertThat(site2.getIdentityService()).isNotNull();
148         assertThat(site2.getIdentityService().getId()).isEqualTo(site2.getIdentityServiceId());
149
150         assertThat(site3.getId()).isEqualTo("MTINJVCC101");
151         assertThat(site3.getRegionId()).isEqualTo("regionTwo");
152         assertThat(site3.getIdentityServiceId()).isEqualTo("MTINJVCC101_DCP");
153         assertThat(site3.getIdentityService()).isNotNull();
154         assertThat(site3.getIdentityService().getId()).isEqualTo(site3.getIdentityServiceId());
155
156         assertThat(site4.getId()).isEqualTo("MTSNJA4LCP1");
157         assertThat(site4.getRegionId()).isEqualTo("mtsnjlcp1");
158         assertThat(site4.getIdentityServiceId()).isEqualTo("MTSNJA3DCP1");
159         assertThat(site4.getIdentityService()).isNotNull();
160         assertThat(site4.getIdentityService().getId()).isEqualTo(site4.getIdentityServiceId());
161     }
162
163     private void checkIdentityServices() {
164         Map<String, CloudIdentity> identityMap = testedObject.getIdentityServices();
165         assertThat(identityMap).isNotEmpty().hasSize(NUMBER_OF_IDENTITY_SERVICES_IN_JSON_FILE);
166
167         CloudIdentity identity1 = identityMap.get("MT_KEYSTONE");
168         CloudIdentity identity2 = identityMap.get("DAN_KEYSTONE");
169         CloudIdentity identity3 = identityMap.get("MTINJVCC101_DCP");
170         CloudIdentity identity4 = identityMap.get("MTSNJA3DCP1");
171
172         assertThat(identity1.getMsoId()).isEqualTo("john");
173         assertThat(identity1.getMsoPass()).isEqualTo("changeme");
174         assertThat(identity1.getAdminTenant()).isEqualTo("admin");
175         assertThat(identity1.getMemberRole()).isEqualTo("_member_");
176         assertThat(identity1.hasTenantMetadata()).isFalse();
177
178         assertThat(identity2.getMsoId()).isEqualTo("mockId");
179         assertThat(identity2.getMsoPass()).isEqualTo("stack123");
180         assertThat(identity2.getAdminTenant()).isEqualTo("service");
181         assertThat(identity2.getMemberRole()).isEqualTo("_member_");
182         assertThat(identity2.hasTenantMetadata()).isFalse();
183
184         assertThat(identity3.getMsoId()).isEqualTo("mockIdToo");
185         assertThat(identity3.getMsoPass()).isEqualTo("AICG@mm@@2015");
186         assertThat(identity3.getAdminTenant()).isEqualTo("service");
187         assertThat(identity3.getMemberRole()).isEqualTo("admin");
188         assertThat(identity3.hasTenantMetadata()).isTrue();
189
190         assertThat(identity4.getMsoId()).isEqualTo("mockIdToo");
191         assertThat(identity4.getMsoPass()).isEqualTo("2315QRS2015srq");
192         assertThat(identity4.getAdminTenant()).isEqualTo("service");
193         assertThat(identity4.getMemberRole()).isEqualTo("admin");
194         assertThat(identity4.hasTenantMetadata()).isTrue();
195     }
196
197     @Test
198     public void cloneSuccessful() throws NoSuchFieldException, IllegalAccessException {
199         setCloudSitesMap();
200         setIdentityServiceMap();
201         assertThat(testedObject.clone()).isEqualTo(testedObject);
202     }
203
204     private void setCloudSitesMap() throws NoSuchFieldException, IllegalAccessException {
205         Field field = testedObject.getClass().getDeclaredField(CLOUD_SITES_FIELD_NAME);
206         field.setAccessible(true);
207         Map<String, CloudSite> cloudSites = new HashMap<>();
208         cloudSite = createCloudSite("idTest1", "clliTest1");
209         cloudSiteDefault = createCloudSite(CLOUD_SITE_DEFAULT, "clliTest2");
210         cloudSites.put(cloudSite.getId(), cloudSite);
211         cloudSites.put(cloudSiteDefault.getId(), cloudSiteDefault);
212         field.set(testedObject, cloudSites);
213     }
214
215     private void setIdentityServiceMap() throws NoSuchFieldException, IllegalAccessException {
216         Field field = testedObject.getClass().getDeclaredField(IDENTITY_SERVICE_FIELD_NAME);
217         field.setAccessible(true);
218
219         Map<String, CloudIdentity> cloudIdentityMap = new HashMap<>();
220         CloudIdentity cloudIdentity = createCloudIdentity();
221         cloudIdentityMap.put(cloudIdentity.getId(), cloudIdentity);
222         field.set(testedObject, cloudIdentityMap);
223     }
224
225     private CloudIdentity createCloudIdentity() {
226         CloudIdentity cloudIdentity = new CloudIdentity();
227         cloudIdentity.setId("identityTestId");
228         cloudIdentity.setMsoId("msoTestId");
229         return cloudIdentity;
230     }
231
232     private CloudSite createCloudSite(String id, String clli) {
233         CloudSite cloudSite = new CloudSite();
234         cloudSite.setId(id);
235         cloudSite.setClli(clli);
236         cloudSite.setAic_version("2.5");
237         cloudSite.setIdentityService(createCloudIdentity());
238         return cloudSite;
239     }
240 }