168f830f83b556c19c30ac4b7bd057be9ab7558d
[so.git] / asdc-controller / src / test / java / org / openecomp / mso / asdc / client / tests / ASDCConfigurationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.asdc.client.tests;
22
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.io.IOException;
30 import java.util.List;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36
37 import org.openecomp.mso.asdc.client.ASDCConfiguration;
38 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
39 import org.openecomp.mso.properties.MsoPropertiesException;
40 import org.openecomp.mso.properties.MsoPropertiesFactory;
41
42 /**
43  * THis class tests the ASDC Controller by using the ASDC Mock CLient
44  * 
45  *
46  */
47 public class ASDCConfigurationTest {
48         
49         public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
50         public final String ASDC_PROP = ASDCConfigurationTest.class.getClassLoader().getResource("mso.json").toString().substring(5);
51         public final String ASDC_PROP2 = ASDCConfigurationTest.class.getClassLoader().getResource("mso2.json").toString().substring(5);
52         public final String ASDC_PROP3 = ASDCConfigurationTest.class.getClassLoader().getResource("mso3.json").toString().substring(5);
53         public final String ASDC_PROP_BAD = ASDCConfigurationTest.class.getClassLoader().getResource("mso-bad.json").toString().substring(5);
54         public final String ASDC_PROP_WITH_NULL = ASDCConfigurationTest.class.getClassLoader().getResource("mso-with-NULL.json").toString().substring(5);
55         public final String ASDC_PROP_DOUBLE_CONFIG = ASDCConfigurationTest.class.getClassLoader().getResource("mso-two-configs.json").toString().substring(5);
56         public final String ASDC_PROP4_WITH_TLS = ASDCConfigurationTest.class.getClassLoader().getResource("mso4-with-TLS.json").toString().substring(5);
57         
58         @BeforeClass
59         public static final void prepareBeforeAllTests() {
60                 msoPropertiesFactory.removeAllMsoProperties();
61         }
62         
63         @Before
64         public final void prepareBeforeEachTest () throws MsoPropertiesException {
65                 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
66         }
67         
68         @After
69         public final void cleanAfterEachTest () {
70                 msoPropertiesFactory.removeAllMsoProperties();
71         }
72         
73         @Test
74         public final void testTheInit() throws ASDCParametersException, IOException {
75                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
76                 assertNotNull(asdcConfig.getUser());
77                 assertTrue("User".equals(asdcConfig.getUser()));
78                 
79                 assertNotNull(asdcConfig.getPassword());
80                 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
81                 
82                 assertNotNull(asdcConfig.getConsumerGroup());
83                 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
84                 
85                 assertNotNull(asdcConfig.getConsumerID());
86                 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
87                 
88                 assertNotNull(asdcConfig.getEnvironmentName());
89                 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
90                 
91                 assertNotNull(asdcConfig.getAsdcAddress());
92                 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
93                 
94                 assertNotNull(asdcConfig.getPollingInterval());
95                 assertTrue(asdcConfig.getPollingInterval() == 10);
96                 
97                 assertNotNull(asdcConfig.getPollingTimeout());
98                 assertTrue(asdcConfig.getPollingTimeout() == 30);
99                 
100                 assertNotNull(asdcConfig.getRelevantArtifactTypes());
101                 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
102                 
103                 assertFalse(asdcConfig.activateServerTLSAuth());
104                 
105         }
106         
107         @Test
108         public final void testAllParametersMethod() throws ASDCParametersException, IOException {
109                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
110                 
111                 // No exception should be raised
112                 asdcConfig.testAllParameters();
113         }
114
115         @Test
116         public final void testTheRefreshConfigFalseCase() throws ASDCParametersException, IOException {
117                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
118                 
119                 // No update should be done as we use the mso.properties located in the resource folder for testing
120                 assertFalse(asdcConfig.hasASDCConfigChanged());
121                 assertFalse(asdcConfig.refreshASDCConfig());
122                 
123                 assertNotNull(asdcConfig.getUser());
124                 assertTrue("User".equals(asdcConfig.getUser()));
125                 
126                 assertNotNull(asdcConfig.getPassword());
127                 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
128                 
129                 assertNotNull(asdcConfig.getConsumerGroup());
130                 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
131                 
132                 assertNotNull(asdcConfig.getConsumerID());
133                 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
134                 
135                 assertNotNull(asdcConfig.getEnvironmentName());
136                 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
137                 
138                 assertNotNull(asdcConfig.getAsdcAddress());
139                 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
140                 
141                 assertNotNull(asdcConfig.getPollingInterval());
142                 assertTrue(asdcConfig.getPollingInterval() == 10);
143                 
144                 assertNotNull(asdcConfig.getPollingTimeout());
145                 assertTrue(asdcConfig.getPollingTimeout() == 30);
146                 
147                 assertNotNull(asdcConfig.getRelevantArtifactTypes());
148                 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
149                 
150                 msoPropertiesFactory.removeAllMsoProperties();
151                 
152                 try {
153                         asdcConfig.refreshASDCConfig();
154                         fail("Should have thrown an ASDCParametersException because config does not exist anymore!");
155                 } catch (ASDCParametersException e) {
156                         assertTrue(e.getMessage().contains(("mso.asdc.json not initialized properly, ASDC config cannot be reloaded")));
157                 }
158                 
159                 try {
160                         asdcConfig.hasASDCConfigChanged();
161                         fail("Should have thrown an ASDCParametersException because config does not exist anymore!");
162                 } catch (ASDCParametersException e) {
163                         assertTrue(e.getMessage().contains(("mso.asdc.json not initialized properly, ASDC config cannot be read")));
164                 }
165                 
166         }       
167         
168         
169         @Test
170         public final void testToChangeTheFileAndRefresh () throws ASDCParametersException, IOException, MsoPropertiesException  {
171                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
172
173                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP2);
174                 msoPropertiesFactory.reloadMsoProperties();
175                 
176                 // SHould be the same file untouched just a different file name, there should be no difference between them
177                 // In a normal case a different Filename should force the system to reload the config but not here as we have changed the filename by reflection
178                 assertFalse(asdcConfig.hasASDCConfigChanged());
179                 assertFalse(asdcConfig.refreshASDCConfig());
180
181                 assertNotNull(asdcConfig.getUser());
182                 assertTrue("User".equals(asdcConfig.getUser()));
183
184                 assertNotNull(asdcConfig.getPassword());
185                 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
186
187                 assertNotNull(asdcConfig.getConsumerGroup());
188                 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
189
190                 assertNotNull(asdcConfig.getConsumerID());
191                 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
192
193                 assertNotNull(asdcConfig.getEnvironmentName());
194                 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
195
196                 assertNotNull(asdcConfig.getAsdcAddress());
197                 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
198
199                 assertNotNull(asdcConfig.getPollingInterval());
200                 assertTrue(asdcConfig.getPollingInterval() == 10);
201
202                 assertNotNull(asdcConfig.getPollingTimeout());
203                 assertTrue(asdcConfig.getPollingTimeout() == 30);
204
205                 assertNotNull(asdcConfig.getRelevantArtifactTypes());
206                 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
207
208                 // Set another file that has some attributes changed
209                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP3);
210                 msoPropertiesFactory.reloadMsoProperties();
211                 
212                 // SHould be the same file untouched just a different file name, so new config
213                 assertTrue(asdcConfig.hasASDCConfigChanged());
214                 assertTrue(asdcConfig.refreshASDCConfig());
215
216                 assertNotNull(asdcConfig.getUser());
217                 assertTrue("User".equals(asdcConfig.getUser()));
218
219                 assertNotNull(asdcConfig.getPassword());
220                 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
221
222                 assertNotNull(asdcConfig.getConsumerGroup());
223                 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
224
225                 assertNotNull(asdcConfig.getConsumerID());
226                 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
227
228                 assertNotNull(asdcConfig.getEnvironmentName());
229                 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
230
231                 // only this field has been changed
232                 assertNotNull(asdcConfig.getAsdcAddress());
233                 assertTrue("hostname1".equals(asdcConfig.getAsdcAddress()));
234
235                 assertNotNull(asdcConfig.getPollingInterval());
236                 assertTrue(asdcConfig.getPollingInterval() == 10);
237
238                 assertNotNull(asdcConfig.getPollingTimeout());
239                 assertTrue(asdcConfig.getPollingTimeout() == 30);
240
241                 assertNotNull(asdcConfig.getRelevantArtifactTypes());
242                 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
243
244
245                 // reload the good property file for other test cases
246                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
247                 msoPropertiesFactory.reloadMsoProperties();
248
249         }
250         
251         @Test
252         public final void testAllParametersCheck () throws ASDCParametersException, IOException, MsoPropertiesException  {
253                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
254
255                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
256                 msoPropertiesFactory.reloadMsoProperties();
257                 // SHould be a bad file, it should raise an exception
258                 try {
259                         asdcConfig.refreshASDCConfig();
260                         fail("Should have thrown an ASDCControllerException because one param is missing!");
261                 } catch (ASDCParametersException e) {
262                         assertTrue(e.getMessage().contains(("consumerGroup parameter cannot be found in config mso.properties")));
263                 }
264
265
266                 // reload the good property file for other test cases
267                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
268                 msoPropertiesFactory.reloadMsoProperties();
269                 
270                 assertTrue(asdcConfig.refreshASDCConfig());
271  
272         }
273         
274         @Test
275         public final void testConsumerGroupWithNULL () throws MsoPropertiesException, ASDCParametersException, IOException {
276                 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
277                 
278                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_WITH_NULL);
279                 msoPropertiesFactory.reloadMsoProperties();
280
281                 asdcConfig.refreshASDCConfig();
282                 assertTrue(asdcConfig.getConsumerGroup()==null);
283
284                 // reload the good property file for other test cases
285                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
286                 msoPropertiesFactory.reloadMsoProperties();
287                 
288                 assertTrue(asdcConfig.refreshASDCConfig());
289
290
291         }
292
293         @Test
294         public final void testGetAllDefinedControllers() throws MsoPropertiesException, ASDCParametersException, IOException {
295                 List<String> listControllers = ASDCConfiguration.getAllDefinedControllers();
296                 
297                 assertTrue(listControllers.size()==1);
298                 assertTrue("asdc-controller1".equals(listControllers.get(0)));
299                 
300                 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
301                 assertTrue(asdcConfiguration.getAsdcControllerName().equals("asdc-controller1"));
302                 
303                 
304                 // Try to reload a wrong Json file
305                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
306                 msoPropertiesFactory.reloadMsoProperties();
307                 
308                 listControllers = ASDCConfiguration.getAllDefinedControllers();
309                 assertTrue(listControllers.size()==0);
310
311         }
312         
313         @Test
314         public final void testABadInit() throws MsoPropertiesException {
315                 msoPropertiesFactory.removeAllMsoProperties();          
316                 
317                 try {
318                         ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
319                         fail("Should have thrown an ASDCParametersException because prop factory is empty!");
320                 } catch (ASDCParametersException e) {
321                         assertTrue(e.getMessage().contains(("mso.asdc.json not initialized properly, ASDC config cannot be reloaded")));
322                 } catch (IOException e) {
323                         fail("Should have thrown an ASDCParametersException, not IOException because file is corrupted!");
324                 }
325         }
326         
327         @Test
328         public final void testFileDoesNotExist() throws MsoPropertiesException, ASDCParametersException, IOException {
329                                 
330                         ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
331                 
332                         msoPropertiesFactory.removeAllMsoProperties();
333                         
334                 try {   
335                         asdcConfiguration.refreshASDCConfig();
336                         fail("Should have thrown an ASDCParametersException because factory is empty!");
337                 } catch (ASDCParametersException e) {
338                         assertTrue(e.getMessage().contains(("mso.asdc.json not initialized properly, ASDC config cannot be reloaded")));
339                 } 
340         }
341
342         @Test
343         public final void testWithTLS () throws ASDCParametersException, IOException, MsoPropertiesException {
344                 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
345                 
346                 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP4_WITH_TLS);
347                 msoPropertiesFactory.reloadMsoProperties();
348
349                 asdcConfiguration.refreshASDCConfig();
350                 
351                 assertTrue(asdcConfiguration.activateServerTLSAuth());
352                 assertTrue("/test".equals(asdcConfiguration.getKeyStorePath()));
353                 assertTrue("ThePassword".equals(asdcConfiguration.getKeyStorePassword()));
354         }
355         
356 }