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.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.lang.reflect.Field;
30 import java.util.HashMap;
32 import javax.ws.rs.core.Response;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
38 public class CloudConfigFactoryTest {
40 private static final String CLOUD_CONFIG_FIELD_NAME = "cloudConfigCache";
41 private static final int REFRESH_TIMER_VALUE = 1;
43 private CloudConfigFactory testedObject;
44 private CloudConfig cloudConfigMock;
45 private CloudConfig savedCloudConfig;
48 public void init() throws NoSuchFieldException, IllegalAccessException {
49 cloudConfigMock = mock(CloudConfig.class);
50 testedObject = new CloudConfigFactory();
51 Field field = CloudConfigFactory.class.getDeclaredField(CLOUD_CONFIG_FIELD_NAME);
52 field.setAccessible(true);
53 savedCloudConfig = (CloudConfig) field.get(null);
54 field.set(null, cloudConfigMock);
58 public void reset() throws NoSuchFieldException, IllegalAccessException {
59 Field field = CloudConfigFactory.class.getDeclaredField(CLOUD_CONFIG_FIELD_NAME);
60 field.setAccessible(true);
61 field.set(null, savedCloudConfig);
65 public void initializeCloudConfigSuccessful() throws MsoCloudIdentityNotFound, IOException {
66 ClassLoader classLoader = CloudConfigFactoryTest.class.getClassLoader();
67 String cloudConfigJsonFilePath = classLoader.getResource("cloud_config.json").getPath();
68 testedObject.initializeCloudConfig(cloudConfigJsonFilePath, REFRESH_TIMER_VALUE);
69 verify(cloudConfigMock).loadCloudConfig(cloudConfigJsonFilePath, REFRESH_TIMER_VALUE);
73 public void getValidCloudConfig() {
74 when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
76 testedObject.getCloudConfig();
78 verify(cloudConfigMock).clone();
82 public void reload_CloudConfigValid() throws IOException, MsoCloudIdentityNotFound {
83 when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
85 testedObject.reloadCloudConfig();
87 verify(cloudConfigMock).clone();
88 verify(cloudConfigMock).reloadPropertiesFile();
92 public void reload_CloudConfigNotValid()
93 throws IOException, MsoCloudIdentityNotFound {
94 when(cloudConfigMock.isValidCloudConfig()).thenReturn(false);
96 testedObject.reloadCloudConfig();
98 verify(cloudConfigMock).reloadPropertiesFile();
102 public void showCloudConfig() throws NoSuchFieldException, IllegalAccessException {
103 when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
104 when(cloudConfigMock.clone()).thenReturn(createCloudConfig("IdTest576", "identityTest456"));
105 Response response = testedObject.showCloudConfig();
107 assertThat(response.getStatus()).isEqualTo(200);
108 assertThat(response.getEntity().toString()).containsPattern("CloudSite:.*IdTest576")
109 .containsPattern("Cloud Identity Service:.*identityTest456");
114 public void resetClientCaches_Successful() {
115 Response response = testedObject.resetClientCaches();
116 assertThat(response.getStatus()).isEqualTo(200);
117 assertThat(response.getEntity().toString()).isEqualTo("Client caches reset. All entries removed.");
121 public void cleanUpClientCache_Successful() {
122 Response response = testedObject.cleanupClientCaches();
123 assertThat(response.getStatus()).isEqualTo(200);
124 assertThat(response.getEntity().toString()).isEqualTo("Client caches cleaned up. All expired entries removed.");
128 public void encryptPassword_Successful() {
129 Response response = testedObject.encryptPassword("passTest123");
130 String expectedEncryptedPassword = CloudIdentity.encryptPassword("passTest123");
131 assertThat(response.getStatus()).isEqualTo(200);
132 assertThat(response.getEntity().toString()).isEqualTo("Encrypted Password = "+expectedEncryptedPassword);
135 private CloudConfig createCloudConfig(String cloudSiteId, String identityServiceId)
136 throws NoSuchFieldException, IllegalAccessException {
137 CloudConfig cloudConfig = new CloudConfig();
138 Map<String, CloudSite> cloudSiteMap = new HashMap<>();
139 CloudSite cs = new CloudSite();
140 cs.setId(cloudSiteId);
141 cloudSiteMap.put("keyTest", cs);
142 Field cloudSitesField = cloudConfig.getClass().getDeclaredField("cloudSites");
143 cloudSitesField.setAccessible(true);
144 cloudSitesField.set(cloudConfig, cloudSiteMap);
146 Map<String, CloudIdentity> identityServicesMap = new HashMap<>();
147 CloudIdentity cloudIdentity = new CloudIdentity();
148 cloudIdentity.setId(identityServiceId);
149 identityServicesMap.put("identityKey", cloudIdentity);
151 Field identityServicesField = cloudConfig.getClass().getDeclaredField("identityServices");
152 identityServicesField.setAccessible(true);
153 identityServicesField.set(cloudConfig, identityServicesMap);
158 private void setCloudConfig()
159 throws NoSuchFieldException, IllegalAccessException {