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.asdc.client.tests;
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.Test;
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;
43 * THis class tests the ASDC Controller by using the ASDC Mock CLient
47 public class ASDCConfigurationTest {
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);
59 public static final void prepareBeforeAllTests() {
60 msoPropertiesFactory.removeAllMsoProperties();
64 public final void prepareBeforeEachTest () throws MsoPropertiesException {
65 msoPropertiesFactory.initializeMsoProperties(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
69 public final void cleanAfterEachTest () {
70 msoPropertiesFactory.removeAllMsoProperties();
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()));
79 assertNotNull(asdcConfig.getPassword());
80 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
82 assertNotNull(asdcConfig.getConsumerGroup());
83 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
85 assertNotNull(asdcConfig.getConsumerID());
86 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
88 assertNotNull(asdcConfig.getEnvironmentName());
89 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
91 assertNotNull(asdcConfig.getAsdcAddress());
92 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
94 assertNotNull(asdcConfig.getPollingInterval());
95 assertTrue(asdcConfig.getPollingInterval() == 10);
97 assertNotNull(asdcConfig.getPollingTimeout());
98 assertTrue(asdcConfig.getPollingTimeout() == 30);
100 assertNotNull(asdcConfig.getRelevantArtifactTypes());
101 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
103 assertFalse(asdcConfig.activateServerTLSAuth());
108 public final void testAllParametersMethod() throws ASDCParametersException, IOException {
109 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
111 // No exception should be raised
112 asdcConfig.testAllParameters();
116 public final void testTheRefreshConfigFalseCase() throws ASDCParametersException, IOException {
117 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
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());
123 assertNotNull(asdcConfig.getUser());
124 assertTrue("User".equals(asdcConfig.getUser()));
126 assertNotNull(asdcConfig.getPassword());
127 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
129 assertNotNull(asdcConfig.getConsumerGroup());
130 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
132 assertNotNull(asdcConfig.getConsumerID());
133 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
135 assertNotNull(asdcConfig.getEnvironmentName());
136 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
138 assertNotNull(asdcConfig.getAsdcAddress());
139 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
141 assertNotNull(asdcConfig.getPollingInterval());
142 assertTrue(asdcConfig.getPollingInterval() == 10);
144 assertNotNull(asdcConfig.getPollingTimeout());
145 assertTrue(asdcConfig.getPollingTimeout() == 30);
147 assertNotNull(asdcConfig.getRelevantArtifactTypes());
148 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
150 msoPropertiesFactory.removeAllMsoProperties();
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")));
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")));
170 public final void testToChangeTheFileAndRefresh () throws ASDCParametersException, IOException, MsoPropertiesException {
171 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
173 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP2);
174 msoPropertiesFactory.reloadMsoProperties();
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());
181 assertNotNull(asdcConfig.getUser());
182 assertTrue("User".equals(asdcConfig.getUser()));
184 assertNotNull(asdcConfig.getPassword());
185 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
187 assertNotNull(asdcConfig.getConsumerGroup());
188 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
190 assertNotNull(asdcConfig.getConsumerID());
191 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
193 assertNotNull(asdcConfig.getEnvironmentName());
194 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
196 assertNotNull(asdcConfig.getAsdcAddress());
197 assertTrue("hostname".equals(asdcConfig.getAsdcAddress()));
199 assertNotNull(asdcConfig.getPollingInterval());
200 assertTrue(asdcConfig.getPollingInterval() == 10);
202 assertNotNull(asdcConfig.getPollingTimeout());
203 assertTrue(asdcConfig.getPollingTimeout() == 30);
205 assertNotNull(asdcConfig.getRelevantArtifactTypes());
206 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
208 // Set another file that has some attributes changed
209 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP3);
210 msoPropertiesFactory.reloadMsoProperties();
212 // SHould be the same file untouched just a different file name, so new config
213 assertTrue(asdcConfig.hasASDCConfigChanged());
214 assertTrue(asdcConfig.refreshASDCConfig());
216 assertNotNull(asdcConfig.getUser());
217 assertTrue("User".equals(asdcConfig.getUser()));
219 assertNotNull(asdcConfig.getPassword());
220 assertTrue("ThePassword".equals(asdcConfig.getPassword()));
222 assertNotNull(asdcConfig.getConsumerGroup());
223 assertTrue("consumerGroup".equals(asdcConfig.getConsumerGroup()));
225 assertNotNull(asdcConfig.getConsumerID());
226 assertTrue("consumerId".equals(asdcConfig.getConsumerID()));
228 assertNotNull(asdcConfig.getEnvironmentName());
229 assertTrue("environmentName".equals(asdcConfig.getEnvironmentName()));
231 // only this field has been changed
232 assertNotNull(asdcConfig.getAsdcAddress());
233 assertTrue("hostname1".equals(asdcConfig.getAsdcAddress()));
235 assertNotNull(asdcConfig.getPollingInterval());
236 assertTrue(asdcConfig.getPollingInterval() == 10);
238 assertNotNull(asdcConfig.getPollingTimeout());
239 assertTrue(asdcConfig.getPollingTimeout() == 30);
241 assertNotNull(asdcConfig.getRelevantArtifactTypes());
242 assertTrue(asdcConfig.getRelevantArtifactTypes().size() == ASDCConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size());
245 // reload the good property file for other test cases
246 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
247 msoPropertiesFactory.reloadMsoProperties();
252 public final void testAllParametersCheck () throws ASDCParametersException, IOException, MsoPropertiesException {
253 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
255 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
256 msoPropertiesFactory.reloadMsoProperties();
257 // SHould be a bad file, it should raise an exception
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")));
266 // reload the good property file for other test cases
267 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
268 msoPropertiesFactory.reloadMsoProperties();
270 assertTrue(asdcConfig.refreshASDCConfig());
275 public final void testConsumerGroupWithNULL () throws MsoPropertiesException, ASDCParametersException, IOException {
276 ASDCConfiguration asdcConfig = new ASDCConfiguration("asdc-controller1");
278 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_WITH_NULL);
279 msoPropertiesFactory.reloadMsoProperties();
281 asdcConfig.refreshASDCConfig();
282 assertTrue(asdcConfig.getConsumerGroup()==null);
284 // reload the good property file for other test cases
285 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP);
286 msoPropertiesFactory.reloadMsoProperties();
288 assertTrue(asdcConfig.refreshASDCConfig());
294 public final void testGetAllDefinedControllers() throws MsoPropertiesException, ASDCParametersException, IOException {
295 List<String> listControllers = ASDCConfiguration.getAllDefinedControllers();
297 assertTrue(listControllers.size()==1);
298 assertTrue("asdc-controller1".equals(listControllers.get(0)));
300 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
301 assertTrue(asdcConfiguration.getAsdcControllerName().equals("asdc-controller1"));
304 // Try to reload a wrong Json file
305 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP_BAD);
306 msoPropertiesFactory.reloadMsoProperties();
308 listControllers = ASDCConfiguration.getAllDefinedControllers();
309 assertTrue(listControllers.size()==0);
314 public final void testABadInit() throws MsoPropertiesException {
315 msoPropertiesFactory.removeAllMsoProperties();
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!");
328 public final void testFileDoesNotExist() throws MsoPropertiesException, ASDCParametersException, IOException {
330 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
332 msoPropertiesFactory.removeAllMsoProperties();
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")));
343 public final void testWithTLS () throws ASDCParametersException, IOException, MsoPropertiesException {
344 ASDCConfiguration asdcConfiguration = new ASDCConfiguration("asdc-controller1");
346 msoPropertiesFactory.changeMsoPropertiesFilePath(ASDCConfiguration.MSO_PROP_ASDC, ASDC_PROP4_WITH_TLS);
347 msoPropertiesFactory.reloadMsoProperties();
349 asdcConfiguration.refreshASDCConfig();
351 assertTrue(asdcConfiguration.activateServerTLSAuth());
352 assertTrue("/test".equals(asdcConfiguration.getKeyStorePath()));
353 assertTrue("ThePassword".equals(asdcConfiguration.getKeyStorePassword()));