2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.configuration;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.powermock.reflect.Whitebox;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Properties;
37 import static org.mockito.Mockito.mock;
39 public class DefaultConfigurationTest {
40 private static final String propKey1 = "testKey1";
41 private static final String propKey2 = "testKey2";
42 private static final String propValue1 = "testValue1";
43 private static final String propValue2 = "testValue2";
45 private Properties prop = new Properties();
46 private DefaultConfiguration defaultConfiguration;
49 public void setUp() throws Exception {
50 prop.setProperty(propKey1, propValue1);
51 prop.setProperty(propKey2, propValue2);
53 defaultConfiguration = new DefaultConfiguration();
57 public void testClear() throws Exception {
58 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
59 defaultConfiguration.clear();
60 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
61 Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
65 public void testClone() throws Exception {
66 Object clonedObject = defaultConfiguration.clone();
67 Assert.assertTrue("Should be DefaultConfiguration", clonedObject instanceof DefaultConfiguration);
68 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
69 Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties");
70 Assert.assertEquals(internalProp, clonedInternalProp);
74 public void testEquals() throws Exception {
75 // test compare with null
76 Assert.assertFalse(defaultConfiguration.equals(null));
77 // test with non-DefaultConfiguration object
78 Assert.assertFalse(defaultConfiguration.equals("abc"));
80 // test with not match DefaultConfiguration object
81 defaultConfiguration.setProperties(prop);
82 DefaultConfiguration newConfig = new DefaultConfiguration();
83 Assert.assertFalse(defaultConfiguration.equals(newConfig));
85 // test with matching DefaultConfiguration object
86 newConfig.setProperties(prop);
87 Assert.assertTrue(defaultConfiguration.equals(newConfig));
91 public void testSetPropAndGetBooleanProperty() throws Exception {
92 String booleanKey = "booleanKey";
94 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
95 // test match value true
96 defaultConfiguration.setProperty(booleanKey, "true");
97 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
98 defaultConfiguration.setProperty(booleanKey, "True");
99 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
100 defaultConfiguration.setProperty(booleanKey, "TrUe");
101 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
102 // test not matching true values
103 defaultConfiguration.setProperty(booleanKey, "false");
104 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
105 defaultConfiguration.setProperty(booleanKey, "abc");
106 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
110 public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
111 String booleanKey = "booleanKey";
112 // test default value
113 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
114 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
115 // test match value true
116 defaultConfiguration.setProperty(booleanKey, "true");
117 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
118 defaultConfiguration.setProperty(booleanKey, "True");
119 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
120 defaultConfiguration.setProperty(booleanKey, "TrUe");
121 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
122 // test not matching true values
123 defaultConfiguration.setProperty(booleanKey, "false");
124 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
125 defaultConfiguration.setProperty(booleanKey, "abc");
126 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
130 public void testSetPropAndGetDoubleProperty() throws Exception {
131 String doubleKey = "doubleKey";
132 // test default value
133 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
134 // test NumberFormatException
135 defaultConfiguration.setProperty(doubleKey, "abc");
136 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
138 defaultConfiguration.setProperty(doubleKey, "1.1");
139 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
143 public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
144 String doubleKey = "doubleKey";
145 // test default value
146 Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
147 // test NumberFormatException
148 defaultConfiguration.setProperty(doubleKey, "abc");
149 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
151 defaultConfiguration.setProperty(doubleKey, "1.1");
152 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
156 public void testSetPropAndGetIntegerProperty() throws Exception {
157 String integerKey = "integerKey";
158 // test default value
159 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
160 // test NumberFormatException
161 defaultConfiguration.setProperty(integerKey, "abc");
162 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
164 defaultConfiguration.setProperty(integerKey, "100");
165 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
169 public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
170 String integerKey = "integerKey";
171 // test default value
172 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
173 // test NumberFormatException
174 defaultConfiguration.setProperty(integerKey, "abc");
175 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
177 defaultConfiguration.setProperty(integerKey, "100");
178 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
182 public void testSetPropAndGetLongProperty() throws Exception {
183 String longKey = "longKey";
184 // test default value
185 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
186 // test NumberFormatException
187 defaultConfiguration.setProperty(longKey, "abc");
188 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
190 defaultConfiguration.setProperty(longKey, "100");
191 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
195 public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
196 String longKey = "longKey";
197 // test default value
198 Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
199 // test NumberFormatException
200 defaultConfiguration.setProperty(longKey, "abc");
201 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
203 defaultConfiguration.setProperty(longKey, "100");
204 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
208 public void testSetAndGetProperties() throws Exception {
209 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
210 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
212 defaultConfiguration.setProperties(prop);
213 internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
214 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
218 public void testSetAndGetProperty() throws Exception {
220 // test default value
221 Assert.assertTrue(null == defaultConfiguration.getProperty(key));
223 defaultConfiguration.setProperty(key, "abc");
224 Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
228 public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
230 // test default value
231 Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
232 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
234 defaultConfiguration.setProperty(key, "abc");
235 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
239 public void testHashCode() throws Exception {
240 Properties properties = null;
241 Whitebox.setInternalState(defaultConfiguration, "properties", properties);
242 Assert.assertEquals(0, defaultConfiguration.hashCode());
245 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
246 Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
250 public void testIsPropertyDefined() throws Exception {
253 Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
255 defaultConfiguration.setProperty(key, "abc");
256 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
260 public void testIsValidBoolean() throws Exception {
263 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
264 // test exist with invalid
265 defaultConfiguration.setProperty(key, "abc");
266 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
267 // test exist with valid
268 defaultConfiguration.setProperty(key, "True");
269 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
270 defaultConfiguration.setProperty(key, "FaLse");
271 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
275 public void testIsValidDouble() throws Exception {
278 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
279 // test exist with invalid
280 defaultConfiguration.setProperty(key, "abc");
281 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
282 // test exist with valid
283 defaultConfiguration.setProperty(key, "2");
284 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
285 defaultConfiguration.setProperty(key, "3.45");
286 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
290 public void testIsValidInteger() throws Exception {
293 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
294 // test exist with invalid
295 defaultConfiguration.setProperty(key, "abc");
296 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
297 defaultConfiguration.setProperty(key, "3.45");
298 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
299 // test exist with valid
300 defaultConfiguration.setProperty(key, "2");
301 Assert.assertTrue(defaultConfiguration.isValidInteger(key));
305 public void testIsValidLong() throws Exception {
308 Assert.assertFalse(defaultConfiguration.isValidLong(key));
309 // test exist with invalid
310 defaultConfiguration.setProperty(key, "abc");
311 Assert.assertFalse(defaultConfiguration.isValidLong(key));
312 defaultConfiguration.setProperty(key, "3.45");
313 Assert.assertFalse(defaultConfiguration.isValidLong(key));
314 // test exist with valid
315 defaultConfiguration.setProperty(key, "2");
316 Assert.assertTrue(defaultConfiguration.isValidLong(key));
320 public void testSetPropertiesWithInputStream() throws Exception {
321 InputStream mockIS = mock(InputStream.class);
322 defaultConfiguration.setProperties(mockIS);
324 Properties mockProp = mock(Properties.class);
325 Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
326 Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
327 defaultConfiguration.setProperties(mockIS);
328 // Should come here without exception
332 public void testToString() throws Exception {
333 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
334 Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
335 internalProp.size(), internalProp.keySet().toString()), defaultConfiguration.toString());