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 * Modifications Copyright (C) 2019 Ericsson
12 * =============================================================================
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.configuration;
30 import static org.mockito.Mockito.mock;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.Properties;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.appc.encryption.EncryptionTool;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.reflect.Whitebox;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import static org.junit.Assert.assertNotNull;
46 @RunWith(PowerMockRunner.class)
47 @PrepareForTest(EncryptionTool.class)
48 public class DefaultConfigurationTest {
49 private static final String propKey1 = "testKey1";
50 private static final String propKey2 = "testKey2";
51 private static final String propValue1 = "testValue1";
52 private static final String propValue2 = "testValue2";
53 private EncryptionTool encryptionTool;
55 private Properties prop = new Properties();
56 private DefaultConfiguration defaultConfiguration;
59 public void setUp() throws Exception {
60 prop.setProperty(propKey1, propValue1);
61 prop.setProperty(propKey2, propValue2);
63 defaultConfiguration = new DefaultConfiguration();
64 PowerMockito.mockStatic(EncryptionTool.class);
65 encryptionTool = Mockito.mock(EncryptionTool.class);
66 PowerMockito.when(EncryptionTool.getInstance()).thenReturn(encryptionTool);
70 public void testClear() throws Exception {
71 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
72 defaultConfiguration.clear();
73 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
74 Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
78 public void testClone() throws Exception {
79 Object clonedObject = defaultConfiguration.clone();
80 Assert.assertTrue("Should be DefaultConfiguration",
81 clonedObject instanceof DefaultConfiguration);
82 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
83 Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties");
84 Assert.assertEquals(internalProp, clonedInternalProp);
88 public void testEquals() throws Exception {
89 // test compare with null
90 Assert.assertFalse(defaultConfiguration.equals(null));
91 // test with non-DefaultConfiguration object
92 Assert.assertFalse(defaultConfiguration.equals("abc"));
94 // test with not match DefaultConfiguration object
95 defaultConfiguration.setProperties(prop);
96 DefaultConfiguration newConfig = new DefaultConfiguration();
97 Assert.assertFalse(defaultConfiguration.equals(newConfig));
99 // test with matching DefaultConfiguration object
100 newConfig.setProperties(prop);
101 Assert.assertTrue(defaultConfiguration.equals(newConfig));
105 public void testSetPropAndGetBooleanProperty() throws Exception {
106 String booleanKey = "booleanKey";
107 // test default value
108 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
109 // test match value true
110 defaultConfiguration.setProperty(booleanKey, "true");
111 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
112 defaultConfiguration.setProperty(booleanKey, "True");
113 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
114 defaultConfiguration.setProperty(booleanKey, "TrUe");
115 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
116 // test not matching true values
117 defaultConfiguration.setProperty(booleanKey, "false");
118 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
119 defaultConfiguration.setProperty(booleanKey, "abc");
120 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
124 public void testSetPropAndGetBooleanPropertyForEncryptedValue()
126 String booleanKey = "booleanKey";
127 Mockito.when(encryptionTool.decrypt("enc:true")).thenReturn("true");
128 defaultConfiguration.setProperty(booleanKey, "enc:true");
129 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
133 public void testSetPropAndGetBooleanPropertyForEncryptedValueException()
135 String booleanKey = "booleanKey";
136 Mockito.when(encryptionTool.decrypt("enc:false")).thenThrow(new RuntimeException());
137 defaultConfiguration.setProperty(booleanKey, "enc:false");
138 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
142 public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
143 String booleanKey = "booleanKey";
144 // test default value
145 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
146 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
147 // test match value true
148 defaultConfiguration.setProperty(booleanKey, "true");
149 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
150 defaultConfiguration.setProperty(booleanKey, "True");
151 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
152 defaultConfiguration.setProperty(booleanKey, "TrUe");
153 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
154 // test not matching true values
155 defaultConfiguration.setProperty(booleanKey, "false");
156 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
157 defaultConfiguration.setProperty(booleanKey, "abc");
158 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
162 public void testSetPropAndGetDoubleProperty() throws Exception {
163 String doubleKey = "doubleKey";
164 // test default value
165 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
166 // test NumberFormatException
167 defaultConfiguration.setProperty(doubleKey, "abc");
168 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
170 defaultConfiguration.setProperty(doubleKey, "1.1");
171 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
175 public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
176 String doubleKey = "doubleKey";
177 // test default value
178 Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
179 // test NumberFormatException
180 defaultConfiguration.setProperty(doubleKey, "abc");
181 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
183 defaultConfiguration.setProperty(doubleKey, "1.1");
184 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
188 public void testSetPropAndGetIntegerProperty() throws Exception {
189 String integerKey = "integerKey";
190 // test default value
191 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
192 // test NumberFormatException
193 defaultConfiguration.setProperty(integerKey, "abc");
194 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
196 defaultConfiguration.setProperty(integerKey, "100");
197 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
201 public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
202 String integerKey = "integerKey";
203 // test default value
204 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
205 // test NumberFormatException
206 defaultConfiguration.setProperty(integerKey, "abc");
207 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
209 defaultConfiguration.setProperty(integerKey, "100");
210 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
214 public void testSetPropAndGetLongProperty() throws Exception {
215 String longKey = "longKey";
216 // test default value
217 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
218 // test NumberFormatException
219 defaultConfiguration.setProperty(longKey, "abc");
220 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
222 defaultConfiguration.setProperty(longKey, "100");
223 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
227 public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
228 String longKey = "longKey";
229 // test default value
230 Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
231 // test NumberFormatException
232 defaultConfiguration.setProperty(longKey, "abc");
233 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
235 defaultConfiguration.setProperty(longKey, "100");
236 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
240 public void testSetAndGetProperties() throws Exception {
241 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
242 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
244 defaultConfiguration.setProperties(prop);
245 internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
246 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
250 public void testSetAndGetProperty() throws Exception {
252 // test default value
253 Assert.assertTrue(null == defaultConfiguration.getProperty(key));
255 defaultConfiguration.setProperty(key, "abc");
256 Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
260 public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
262 // test default value
263 Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
264 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
266 defaultConfiguration.setProperty(key, "abc");
267 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
271 public void testHashCode() throws Exception {
272 Properties properties = null;
273 Whitebox.setInternalState(defaultConfiguration, "properties", properties);
274 Assert.assertEquals(0, defaultConfiguration.hashCode());
277 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
278 Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
282 public void testIsPropertyDefined() throws Exception {
285 Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
287 defaultConfiguration.setProperty(key, "abc");
288 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
292 public void testIsValidBoolean() throws Exception {
295 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
296 // test exist with invalid
297 defaultConfiguration.setProperty(key, "abc");
298 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
299 // test exist with valid
300 defaultConfiguration.setProperty(key, "True");
301 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
302 defaultConfiguration.setProperty(key, "FaLse");
303 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
307 public void testIsValidDouble() throws Exception {
310 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
311 // test exist with invalid
312 defaultConfiguration.setProperty(key, "abc");
313 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
314 // test exist with valid
315 defaultConfiguration.setProperty(key, "2");
316 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
317 defaultConfiguration.setProperty(key, "3.45");
318 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
322 public void testIsValidInteger() throws Exception {
325 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
326 // test exist with invalid
327 defaultConfiguration.setProperty(key, "abc");
328 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
329 defaultConfiguration.setProperty(key, "3.45");
330 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
331 // test exist with valid
332 defaultConfiguration.setProperty(key, "2");
333 Assert.assertTrue(defaultConfiguration.isValidInteger(key));
337 public void testIsValidLong() throws Exception {
340 Assert.assertFalse(defaultConfiguration.isValidLong(key));
341 // test exist with invalid
342 defaultConfiguration.setProperty(key, "abc");
343 Assert.assertFalse(defaultConfiguration.isValidLong(key));
344 defaultConfiguration.setProperty(key, "3.45");
345 Assert.assertFalse(defaultConfiguration.isValidLong(key));
346 // test exist with valid
347 defaultConfiguration.setProperty(key, "2");
348 Assert.assertTrue(defaultConfiguration.isValidLong(key));
352 public void testSetPropertiesWithInputStream() throws Exception {
353 InputStream mockIS = mock(InputStream.class);
354 defaultConfiguration.setProperties(mockIS);
356 Properties mockProp = mock(Properties.class);
357 Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
358 Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
359 defaultConfiguration.setProperties(mockIS);
360 assertNotNull(mockIS);
361 // Should come here without exception
365 public void testToString() throws Exception {
366 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
367 Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
368 internalProp.size(), internalProp.keySet().toString()),
369 defaultConfiguration.toString());