2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.configuration;
26 import static org.mockito.Mockito.mock;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Properties;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.powermock.reflect.Whitebox;
36 public class DefaultConfigurationTest {
37 private static final String propKey1 = "testKey1";
38 private static final String propKey2 = "testKey2";
39 private static final String propValue1 = "testValue1";
40 private static final String propValue2 = "testValue2";
42 private Properties prop = new Properties();
43 private DefaultConfiguration defaultConfiguration;
46 public void setUp() throws Exception {
47 prop.setProperty(propKey1, propValue1);
48 prop.setProperty(propKey2, propValue2);
50 defaultConfiguration = new DefaultConfiguration();
54 public void testClear() throws Exception {
55 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
56 defaultConfiguration.clear();
57 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
58 Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
62 public void testClone() throws Exception {
63 Object clonedObject = defaultConfiguration.clone();
64 Assert.assertTrue("Should be DefaultConfiguration",
65 clonedObject instanceof DefaultConfiguration);
66 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
67 Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties");
68 Assert.assertEquals(internalProp, clonedInternalProp);
72 public void testEquals() throws Exception {
73 // test compare with null
74 Assert.assertFalse(defaultConfiguration.equals(null));
75 // test with non-DefaultConfiguration object
76 Assert.assertFalse(defaultConfiguration.equals("abc"));
78 // test with not match DefaultConfiguration object
79 defaultConfiguration.setProperties(prop);
80 DefaultConfiguration newConfig = new DefaultConfiguration();
81 Assert.assertFalse(defaultConfiguration.equals(newConfig));
83 // test with matching DefaultConfiguration object
84 newConfig.setProperties(prop);
85 Assert.assertTrue(defaultConfiguration.equals(newConfig));
89 public void testSetPropAndGetBooleanProperty() throws Exception {
90 String booleanKey = "booleanKey";
92 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
93 // test match value true
94 defaultConfiguration.setProperty(booleanKey, "true");
95 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
96 defaultConfiguration.setProperty(booleanKey, "True");
97 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
98 defaultConfiguration.setProperty(booleanKey, "TrUe");
99 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
100 // test not matching true values
101 defaultConfiguration.setProperty(booleanKey, "false");
102 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
103 defaultConfiguration.setProperty(booleanKey, "abc");
104 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
108 public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
109 String booleanKey = "booleanKey";
110 // test default value
111 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
112 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
113 // test match value true
114 defaultConfiguration.setProperty(booleanKey, "true");
115 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
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 // test not matching true values
121 defaultConfiguration.setProperty(booleanKey, "false");
122 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
123 defaultConfiguration.setProperty(booleanKey, "abc");
124 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
128 public void testSetPropAndGetDoubleProperty() throws Exception {
129 String doubleKey = "doubleKey";
130 // test default value
131 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
132 // test NumberFormatException
133 defaultConfiguration.setProperty(doubleKey, "abc");
134 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
136 defaultConfiguration.setProperty(doubleKey, "1.1");
137 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
141 public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
142 String doubleKey = "doubleKey";
143 // test default value
144 Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
145 // test NumberFormatException
146 defaultConfiguration.setProperty(doubleKey, "abc");
147 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
149 defaultConfiguration.setProperty(doubleKey, "1.1");
150 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
154 public void testSetPropAndGetIntegerProperty() throws Exception {
155 String integerKey = "integerKey";
156 // test default value
157 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
158 // test NumberFormatException
159 defaultConfiguration.setProperty(integerKey, "abc");
160 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
162 defaultConfiguration.setProperty(integerKey, "100");
163 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
167 public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
168 String integerKey = "integerKey";
169 // test default value
170 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
171 // test NumberFormatException
172 defaultConfiguration.setProperty(integerKey, "abc");
173 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
175 defaultConfiguration.setProperty(integerKey, "100");
176 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
180 public void testSetPropAndGetLongProperty() throws Exception {
181 String longKey = "longKey";
182 // test default value
183 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
184 // test NumberFormatException
185 defaultConfiguration.setProperty(longKey, "abc");
186 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
188 defaultConfiguration.setProperty(longKey, "100");
189 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
193 public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
194 String longKey = "longKey";
195 // test default value
196 Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
197 // test NumberFormatException
198 defaultConfiguration.setProperty(longKey, "abc");
199 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
201 defaultConfiguration.setProperty(longKey, "100");
202 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
206 public void testSetAndGetProperties() throws Exception {
207 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
208 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
210 defaultConfiguration.setProperties(prop);
211 internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
212 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
216 public void testSetAndGetProperty() throws Exception {
218 // test default value
219 Assert.assertTrue(null == defaultConfiguration.getProperty(key));
221 defaultConfiguration.setProperty(key, "abc");
222 Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
226 public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
228 // test default value
229 Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
230 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
232 defaultConfiguration.setProperty(key, "abc");
233 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
237 public void testHashCode() throws Exception {
238 Properties properties = null;
239 Whitebox.setInternalState(defaultConfiguration, "properties", properties);
240 Assert.assertEquals(0, defaultConfiguration.hashCode());
243 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
244 Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
248 public void testIsPropertyDefined() throws Exception {
251 Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
253 defaultConfiguration.setProperty(key, "abc");
254 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
258 public void testIsValidBoolean() throws Exception {
261 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
262 // test exist with invalid
263 defaultConfiguration.setProperty(key, "abc");
264 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
265 // test exist with valid
266 defaultConfiguration.setProperty(key, "True");
267 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
268 defaultConfiguration.setProperty(key, "FaLse");
269 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
273 public void testIsValidDouble() throws Exception {
276 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
277 // test exist with invalid
278 defaultConfiguration.setProperty(key, "abc");
279 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
280 // test exist with valid
281 defaultConfiguration.setProperty(key, "2");
282 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
283 defaultConfiguration.setProperty(key, "3.45");
284 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
288 public void testIsValidInteger() throws Exception {
291 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
292 // test exist with invalid
293 defaultConfiguration.setProperty(key, "abc");
294 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
295 defaultConfiguration.setProperty(key, "3.45");
296 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
297 // test exist with valid
298 defaultConfiguration.setProperty(key, "2");
299 Assert.assertTrue(defaultConfiguration.isValidInteger(key));
303 public void testIsValidLong() throws Exception {
306 Assert.assertFalse(defaultConfiguration.isValidLong(key));
307 // test exist with invalid
308 defaultConfiguration.setProperty(key, "abc");
309 Assert.assertFalse(defaultConfiguration.isValidLong(key));
310 defaultConfiguration.setProperty(key, "3.45");
311 Assert.assertFalse(defaultConfiguration.isValidLong(key));
312 // test exist with valid
313 defaultConfiguration.setProperty(key, "2");
314 Assert.assertTrue(defaultConfiguration.isValidLong(key));
318 public void testSetPropertiesWithInputStream() throws Exception {
319 InputStream mockIS = mock(InputStream.class);
320 defaultConfiguration.setProperties(mockIS);
322 Properties mockProp = mock(Properties.class);
323 Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
324 Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
325 defaultConfiguration.setProperties(mockIS);
326 // Should come here without exception
330 public void testToString() throws Exception {
331 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
332 Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
333 internalProp.size(), internalProp.keySet().toString()),
334 defaultConfiguration.toString());