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