2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei 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.asdc.client;
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;
29 import java.io.IOException;
30 import java.util.List;
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;
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;
44 * THis class tests the ASDC Controller by using the ASDC Mock CLient
48 public class ASDCConfigurationTest {
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);
60 public static final void prepareBeforeAllTests() {
61 msoPropertiesFactory.removeAllMsoProperties();
65 public final void prepareBeforeEachTest () throws MsoPropertiesException {
66 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
70 public final void cleanAfterEachTest () {
71 msoPropertiesFactory.removeAllMsoProperties();
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()));
80 assertNotNull(asdcConfig.getPassword());
81 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
83 assertNotNull(asdcConfig.getConsumerGroup());
84 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
86 assertNotNull(asdcConfig.getConsumerID());
87 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
89 assertNotNull(asdcConfig.getEnvironmentName());
90 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
92 assertNotNull(asdcConfig.getAsdcAddress());
93 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
95 assertNotNull(asdcConfig.getPollingInterval());
96 assertTrue(asdcConfig.getPollingInterval() == 10);
98 assertNotNull(asdcConfig.getPollingTimeout());
99 assertTrue(asdcConfig.getPollingTimeout() == 30);
101 assertNotNull(asdcConfig.getRelevantArtifactTypes());
102 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
104 assertFalse(asdcConfig.activateServerTLSAuth());
109 public final void testAllParametersMethod() throws ASDCParametersException, IOException {
110 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
112 // No exception should be raised
113 asdcConfig.testAllParameters();
117 public final void testTheRefreshConfigFalseCase() throws ASDCParametersException, IOException {
118 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
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());
124 assertNotNull(asdcConfig.getUser());
125 assertTrue("User".equals(asdcConfig.getUser()));
127 assertNotNull(asdcConfig.getPassword());
128 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
130 assertNotNull(asdcConfig.getConsumerGroup());
131 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
133 assertNotNull(asdcConfig.getConsumerID());
134 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
136 assertNotNull(asdcConfig.getEnvironmentName());
137 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
139 assertNotNull(asdcConfig.getAsdcAddress());
140 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
142 assertNotNull(asdcConfig.getPollingInterval());
143 assertTrue(asdcConfig.getPollingInterval() == 10);
145 assertNotNull(asdcConfig.getPollingTimeout());
146 assertTrue(asdcConfig.getPollingTimeout() == 30);
148 assertNotNull(asdcConfig.getRelevantArtifactTypes());
149 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
151 msoPropertiesFactory.removeAllMsoProperties();
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")));
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")));
171 @Ignore // 1802 merge
172 public final void testToChangeTheFileAndRefresh () throws ASDCParametersException, IOException, MsoPropertiesException {
173 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
175 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP2);
176 msoPropertiesFactory.reloadMsoProperties();
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());
183 assertNotNull(asdcConfig.getUser());
184 assertTrue("User".equals(asdcConfig.getUser()));
186 assertNotNull(asdcConfig.getPassword());
187 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
189 assertNotNull(asdcConfig.getConsumerGroup());
190 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
192 assertNotNull(asdcConfig.getConsumerID());
193 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
195 assertNotNull(asdcConfig.getEnvironmentName());
196 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
198 assertNotNull(asdcConfig.getAsdcAddress());
199 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
201 assertNotNull(asdcConfig.getPollingInterval());
202 assertTrue(asdcConfig.getPollingInterval() == 10);
204 assertNotNull(asdcConfig.getPollingTimeout());
205 assertTrue(asdcConfig.getPollingTimeout() == 30);
207 assertNotNull(asdcConfig.getRelevantArtifactTypes());
208 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
210 // Set another file that has some attributes changed
211 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP3);
212 msoPropertiesFactory.reloadMsoProperties();
214 // SHould be the same file untouched just a different file name, so new config
215 assertTrue(asdcConfig.hasASDCConfigChanged());
216 assertTrue(asdcConfig.refreshASDCConfig());
218 assertNotNull(asdcConfig.getUser());
219 assertTrue("User".equals(asdcConfig.getUser()));
221 assertNotNull(asdcConfig.getPassword());
222 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
224 assertNotNull(asdcConfig.getConsumerGroup());
225 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
227 assertNotNull(asdcConfig.getConsumerID());
228 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
230 assertNotNull(asdcConfig.getEnvironmentName());
231 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
233 // only this field has been changed
234 assertNotNull(asdcConfig.getAsdcAddress());
235 assertTrue("hostname1".equals(asdcConfig.getAsdcAddress()));
237 assertNotNull(asdcConfig.getPollingInterval());
238 assertTrue(asdcConfig.getPollingInterval() == 10);
240 assertNotNull(asdcConfig.getPollingTimeout());
241 assertTrue(asdcConfig.getPollingTimeout() == 30);
243 assertNotNull(asdcConfig.getRelevantArtifactTypes());
244 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
247 // reload the good property file for other test cases
248 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
249 msoPropertiesFactory.reloadMsoProperties();
254 @Ignore // 1802 merge
255 public final void testAllParametersCheck () throws ASDCParametersException, IOException, MsoPropertiesException {
256 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
258 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
259 msoPropertiesFactory.reloadMsoProperties();
260 // SHould be a bad file, it should raise an exception
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")));
269 // reload the good property file for other test cases
270 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
271 msoPropertiesFactory.reloadMsoProperties();
273 assertTrue(asdcConfig.refreshASDCConfig());
278 public final void testConsumerGroupWithNULL () throws MsoPropertiesException, ASDCParametersException, IOException {
279 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
281 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_WITH_NULL);
282 msoPropertiesFactory.reloadMsoProperties();
284 asdcConfig.refreshASDCConfig();
285 assertTrue(asdcConfig.getConsumerGroup()==null);
287 // reload the good property file for other test cases
288 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
289 msoPropertiesFactory.reloadMsoProperties();
291 assertTrue(asdcConfig.refreshASDCConfig());
297 @Ignore // 1802 merge
298 public final void testGetAllDefinedControllers() throws MsoPropertiesException, ASDCParametersException, IOException {
299 List<String> listControllers = ASDCConfiguration.getAllDefinedControllers();
301 assertTrue(listControllers.size()==1);
302 assertTrue("asdc-controller1".equals(listControllers.get(0)));
304 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
305 assertTrue(asdcConfiguration.getAsdcControllerName().equals("asdc-controller1"));
308 // Try to reload a wrong Json file
309 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
310 msoPropertiesFactory.reloadMsoProperties();
312 listControllers = ASDCConfiguration.getAllDefinedControllers();
313 assertTrue(listControllers.size()==0);
318 public final void testABadInit() throws MsoPropertiesException {
319 msoPropertiesFactory.removeAllMsoProperties();
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!");
332 public final void testFileDoesNotExist() throws MsoPropertiesException, ASDCParametersException, IOException {
334 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
336 msoPropertiesFactory.removeAllMsoProperties();
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")));
347 public final void testWithTLS () throws ASDCParametersException, IOException, MsoPropertiesException {
348 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
350 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP4_WITH_TLS);
351 msoPropertiesFactory.reloadMsoProperties();
353 asdcConfiguration.refreshASDCConfig();
355 assertTrue(asdcConfiguration.activateServerTLSAuth());
356 assertTrue("/test".equals(asdcConfiguration.getKeyStorePath()));
357 assertTrue("ThePassword".equals(asdcConfiguration.getKeyStorePassword()));