Merge "Reorder modifiers"
[so.git] / adapters / mso-adapter-utils / src / test / java / org / openecomp / mso / cloud / CloudConfigFactoryTest.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.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.lang.reflect.Field;
30 import java.util.HashMap;
31 import java.util.Map;
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;
37
38 public class CloudConfigFactoryTest {
39
40     private static final String CLOUD_CONFIG_FIELD_NAME = "cloudConfigCache";
41     private static final int REFRESH_TIMER_VALUE = 1;
42
43     private CloudConfigFactory testedObject;
44     private CloudConfig cloudConfigMock;
45     private CloudConfig savedCloudConfig;
46
47     @Before
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);
55     }
56
57     @After
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);
62     }
63
64     @Test
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);
70     }
71
72     @Test
73     public void getValidCloudConfig() {
74         when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
75
76         testedObject.getCloudConfig();
77
78         verify(cloudConfigMock).clone();
79     }
80
81     @Test
82     public void reload_CloudConfigValid() throws IOException, MsoCloudIdentityNotFound {
83         when(cloudConfigMock.isValidCloudConfig()).thenReturn(true);
84
85         testedObject.reloadCloudConfig();
86
87         verify(cloudConfigMock).clone();
88         verify(cloudConfigMock).reloadPropertiesFile();
89     }
90
91     @Test
92     public void reload_CloudConfigNotValid()
93             throws IOException, MsoCloudIdentityNotFound {
94         when(cloudConfigMock.isValidCloudConfig()).thenReturn(false);
95
96         testedObject.reloadCloudConfig();
97
98         verify(cloudConfigMock).reloadPropertiesFile();
99     }
100
101     @Test
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();
106
107         assertThat(response.getStatus()).isEqualTo(200);
108         assertThat(response.getEntity().toString()).containsPattern("CloudSite:.*IdTest576")
109                 .containsPattern("Cloud Identity Service:.*identityTest456");
110
111     }
112
113     @Test
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.");
118     }
119
120     @Test
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.");
125     }
126
127     @Test
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);
133     }
134
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);
145
146         Map<String, CloudIdentity> identityServicesMap = new HashMap<>();
147         CloudIdentity cloudIdentity = new CloudIdentity();
148         cloudIdentity.setId(identityServiceId);
149         identityServicesMap.put("identityKey", cloudIdentity);
150
151         Field identityServicesField = cloudConfig.getClass().getDeclaredField("identityServices");
152         identityServicesField.setAccessible(true);
153         identityServicesField.set(cloudConfig, identityServicesMap);
154
155         return cloudConfig;
156     }
157
158     private void setCloudConfig()
159             throws NoSuchFieldException, IllegalAccessException {
160     }
161
162 }