2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.cloud;
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;
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.util.HashMap;
31 import java.util.Optional;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
36 public class CloudConfigTest {
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";
46 private CloudConfig testedObject;
47 private CloudSite cloudSite;
48 private CloudSite cloudSiteDefault;
52 testedObject = new CloudConfig();
56 public void cloudSite_returnEmptyOptionalIfIdIsNull() {
57 Optional<CloudSite> cloudConfigOpt = new CloudConfig().getCloudSite(null);
58 assertThat(cloudConfigOpt).isEmpty();
62 public void cloudSiteIsGotById_when_IdFound() throws NoSuchFieldException, IllegalAccessException {
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());
71 public void cloudSiteIsGotByClli_when_IdNotFound() throws NoSuchFieldException, IllegalAccessException {
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());
80 public void cloudSiteIsGotByDefault_when_IdAndClliNotFound() throws NoSuchFieldException, IllegalAccessException {
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());
89 public void cloudSiteNotFound_returnNull() {
90 assertThat(testedObject.getCloudSite("not_existing_id")).isEmpty();
94 public void identityServiceFoundById() throws NoSuchFieldException, IllegalAccessException {
95 CloudIdentity cloudIdentity = createCloudIdentity();
96 setIdentityServiceMap();
97 CloudIdentity cloudIdentityResult = testedObject.getIdentityService(cloudIdentity.getId());
99 assertThat(cloudIdentityResult).isNotNull();
100 assertThat(cloudIdentityResult.getId()).isEqualTo(cloudIdentity.getId());
101 assertThat(cloudIdentityResult.getMsoId()).isEqualTo(cloudIdentity.getMsoId());
105 public void defaultClodeSiteNotFound_returnNull() {
106 assertThat(testedObject.getIdentityService("not_existing_id")).isNull();
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();
116 checkIdentityServices();
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();
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");
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());
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());
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());
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());
163 private void checkIdentityServices() {
164 Map<String, CloudIdentity> identityMap = testedObject.getIdentityServices();
165 assertThat(identityMap).isNotEmpty().hasSize(NUMBER_OF_IDENTITY_SERVICES_IN_JSON_FILE);
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");
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();
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();
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();
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();
198 public void cloneSuccessful() throws NoSuchFieldException, IllegalAccessException {
200 setIdentityServiceMap();
201 assertThat(testedObject.clone()).isEqualTo(testedObject);
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);
215 private void setIdentityServiceMap() throws NoSuchFieldException, IllegalAccessException {
216 Field field = testedObject.getClass().getDeclaredField(IDENTITY_SERVICE_FIELD_NAME);
217 field.setAccessible(true);
219 Map<String, CloudIdentity> cloudIdentityMap = new HashMap<>();
220 CloudIdentity cloudIdentity = createCloudIdentity();
221 cloudIdentityMap.put(cloudIdentity.getId(), cloudIdentity);
222 field.set(testedObject, cloudIdentityMap);
225 private CloudIdentity createCloudIdentity() {
226 CloudIdentity cloudIdentity = new CloudIdentity();
227 cloudIdentity.setId("identityTestId");
228 cloudIdentity.setMsoId("msoTestId");
229 return cloudIdentity;
232 private CloudSite createCloudSite(String id, String clli) {
233 CloudSite cloudSite = new CloudSite();
235 cloudSite.setClli(clli);
236 cloudSite.setAic_version("2.5");
237 cloudSite.setIdentityService(createCloudIdentity());