2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification Copyright (C) 2018 IBM.
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.configuration;
28 import static org.mockito.Mockito.mock;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Properties;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.powermock.reflect.Whitebox;
38 public class DefaultConfigurationTest {
39 private static final String propKey1 = "testKey1";
40 private static final String propKey2 = "testKey2";
41 private static final String propValue1 = "testValue1";
42 private static final String propValue2 = "testValue2";
44 private Properties prop = new Properties();
45 private DefaultConfiguration defaultConfiguration;
48 public void setUp() throws Exception {
49 prop.setProperty(propKey1, propValue1);
50 prop.setProperty(propKey2, propValue2);
52 defaultConfiguration = new DefaultConfiguration();
56 public void testClear() throws Exception {
57 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
58 defaultConfiguration.clear();
59 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
60 Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
64 public void testClone() throws Exception {
65 Object clonedObject = defaultConfiguration.clone();
66 Assert.assertTrue("Should be DefaultConfiguration",
67 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 testSetPropAndGetBooleanPropertyForEncryptedValue()
112 String booleanKey = "booleanKey";
113 defaultConfiguration.setProperty(booleanKey, "enc:true");
114 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
119 public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
120 String booleanKey = "booleanKey";
121 // test default value
122 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
123 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
124 // test match value true
125 defaultConfiguration.setProperty(booleanKey, "true");
126 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
127 defaultConfiguration.setProperty(booleanKey, "True");
128 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
129 defaultConfiguration.setProperty(booleanKey, "TrUe");
130 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
131 // test not matching true values
132 defaultConfiguration.setProperty(booleanKey, "false");
133 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
134 defaultConfiguration.setProperty(booleanKey, "abc");
135 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
139 public void testSetPropAndGetDoubleProperty() throws Exception {
140 String doubleKey = "doubleKey";
141 // test default value
142 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
143 // test NumberFormatException
144 defaultConfiguration.setProperty(doubleKey, "abc");
145 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
147 defaultConfiguration.setProperty(doubleKey, "1.1");
148 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
152 public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
153 String doubleKey = "doubleKey";
154 // test default value
155 Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
156 // test NumberFormatException
157 defaultConfiguration.setProperty(doubleKey, "abc");
158 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
160 defaultConfiguration.setProperty(doubleKey, "1.1");
161 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
165 public void testSetPropAndGetIntegerProperty() throws Exception {
166 String integerKey = "integerKey";
167 // test default value
168 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
169 // test NumberFormatException
170 defaultConfiguration.setProperty(integerKey, "abc");
171 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
173 defaultConfiguration.setProperty(integerKey, "100");
174 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
178 public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
179 String integerKey = "integerKey";
180 // test default value
181 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
182 // test NumberFormatException
183 defaultConfiguration.setProperty(integerKey, "abc");
184 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
186 defaultConfiguration.setProperty(integerKey, "100");
187 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
191 public void testSetPropAndGetLongProperty() throws Exception {
192 String longKey = "longKey";
193 // test default value
194 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
195 // test NumberFormatException
196 defaultConfiguration.setProperty(longKey, "abc");
197 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
199 defaultConfiguration.setProperty(longKey, "100");
200 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
204 public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
205 String longKey = "longKey";
206 // test default value
207 Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
208 // test NumberFormatException
209 defaultConfiguration.setProperty(longKey, "abc");
210 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
212 defaultConfiguration.setProperty(longKey, "100");
213 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
217 public void testSetAndGetProperties() throws Exception {
218 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
219 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
221 defaultConfiguration.setProperties(prop);
222 internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
223 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
227 public void testSetAndGetProperty() throws Exception {
229 // test default value
230 Assert.assertTrue(null == defaultConfiguration.getProperty(key));
232 defaultConfiguration.setProperty(key, "abc");
233 Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
237 public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
239 // test default value
240 Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
241 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
243 defaultConfiguration.setProperty(key, "abc");
244 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
248 public void testHashCode() throws Exception {
249 Properties properties = null;
250 Whitebox.setInternalState(defaultConfiguration, "properties", properties);
251 Assert.assertEquals(0, defaultConfiguration.hashCode());
254 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
255 Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
259 public void testIsPropertyDefined() throws Exception {
262 Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
264 defaultConfiguration.setProperty(key, "abc");
265 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
269 public void testIsValidBoolean() throws Exception {
272 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
273 // test exist with invalid
274 defaultConfiguration.setProperty(key, "abc");
275 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
276 // test exist with valid
277 defaultConfiguration.setProperty(key, "True");
278 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
279 defaultConfiguration.setProperty(key, "FaLse");
280 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
284 public void testIsValidDouble() throws Exception {
287 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
288 // test exist with invalid
289 defaultConfiguration.setProperty(key, "abc");
290 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
291 // test exist with valid
292 defaultConfiguration.setProperty(key, "2");
293 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
294 defaultConfiguration.setProperty(key, "3.45");
295 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
299 public void testIsValidInteger() throws Exception {
302 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
303 // test exist with invalid
304 defaultConfiguration.setProperty(key, "abc");
305 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
306 defaultConfiguration.setProperty(key, "3.45");
307 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
308 // test exist with valid
309 defaultConfiguration.setProperty(key, "2");
310 Assert.assertTrue(defaultConfiguration.isValidInteger(key));
314 public void testIsValidLong() throws Exception {
317 Assert.assertFalse(defaultConfiguration.isValidLong(key));
318 // test exist with invalid
319 defaultConfiguration.setProperty(key, "abc");
320 Assert.assertFalse(defaultConfiguration.isValidLong(key));
321 defaultConfiguration.setProperty(key, "3.45");
322 Assert.assertFalse(defaultConfiguration.isValidLong(key));
323 // test exist with valid
324 defaultConfiguration.setProperty(key, "2");
325 Assert.assertTrue(defaultConfiguration.isValidLong(key));
329 public void testSetPropertiesWithInputStream() throws Exception {
330 InputStream mockIS = mock(InputStream.class);
331 defaultConfiguration.setProperties(mockIS);
333 Properties mockProp = mock(Properties.class);
334 Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
335 Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
336 defaultConfiguration.setProperties(mockIS);
337 // Should come here without exception
341 public void testToString() throws Exception {
342 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
343 Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
344 internalProp.size(), internalProp.keySet().toString()),
345 defaultConfiguration.toString());