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