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;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(EncryptionTool.class)
47 public class DefaultConfigurationTest {
48 private static final String propKey1 = "testKey1";
49 private static final String propKey2 = "testKey2";
50 private static final String propValue1 = "testValue1";
51 private static final String propValue2 = "testValue2";
52 private EncryptionTool encryptionTool;
54 private Properties prop = new Properties();
55 private DefaultConfiguration defaultConfiguration;
58 public void setUp() throws Exception {
59 prop.setProperty(propKey1, propValue1);
60 prop.setProperty(propKey2, propValue2);
62 defaultConfiguration = new DefaultConfiguration();
63 PowerMockito.mockStatic(EncryptionTool.class);
64 encryptionTool = Mockito.mock(EncryptionTool.class);
65 PowerMockito.when(EncryptionTool.getInstance()).thenReturn(encryptionTool);
69 public void testClear() throws Exception {
70 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
71 defaultConfiguration.clear();
72 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
73 Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
77 public void testClone() throws Exception {
78 Object clonedObject = defaultConfiguration.clone();
79 Assert.assertTrue("Should be DefaultConfiguration",
80 clonedObject instanceof DefaultConfiguration);
81 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
82 Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties");
83 Assert.assertEquals(internalProp, clonedInternalProp);
87 public void testEquals() throws Exception {
88 // test compare with null
89 Assert.assertFalse(defaultConfiguration.equals(null));
90 // test with non-DefaultConfiguration object
91 Assert.assertFalse(defaultConfiguration.equals("abc"));
93 // test with not match DefaultConfiguration object
94 defaultConfiguration.setProperties(prop);
95 DefaultConfiguration newConfig = new DefaultConfiguration();
96 Assert.assertFalse(defaultConfiguration.equals(newConfig));
98 // test with matching DefaultConfiguration object
99 newConfig.setProperties(prop);
100 Assert.assertTrue(defaultConfiguration.equals(newConfig));
104 public void testSetPropAndGetBooleanProperty() throws Exception {
105 String booleanKey = "booleanKey";
106 // test default value
107 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
108 // test match value true
109 defaultConfiguration.setProperty(booleanKey, "true");
110 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
111 defaultConfiguration.setProperty(booleanKey, "True");
112 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
113 defaultConfiguration.setProperty(booleanKey, "TrUe");
114 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
115 // test not matching true values
116 defaultConfiguration.setProperty(booleanKey, "false");
117 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
118 defaultConfiguration.setProperty(booleanKey, "abc");
119 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
123 public void testSetPropAndGetBooleanPropertyForEncryptedValue()
125 String booleanKey = "booleanKey";
126 Mockito.when(encryptionTool.decrypt("enc:true")).thenReturn("true");
127 defaultConfiguration.setProperty(booleanKey, "enc:true");
128 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
132 public void testSetPropAndGetBooleanPropertyForEncryptedValueException()
134 String booleanKey = "booleanKey";
135 Mockito.when(encryptionTool.decrypt("enc:false")).thenThrow(new RuntimeException());
136 defaultConfiguration.setProperty(booleanKey, "enc:false");
137 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
141 public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
142 String booleanKey = "booleanKey";
143 // test default value
144 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
145 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
146 // test match value true
147 defaultConfiguration.setProperty(booleanKey, "true");
148 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
149 defaultConfiguration.setProperty(booleanKey, "True");
150 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
151 defaultConfiguration.setProperty(booleanKey, "TrUe");
152 Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
153 // test not matching true values
154 defaultConfiguration.setProperty(booleanKey, "false");
155 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
156 defaultConfiguration.setProperty(booleanKey, "abc");
157 Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
161 public void testSetPropAndGetDoubleProperty() throws Exception {
162 String doubleKey = "doubleKey";
163 // test default value
164 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
165 // test NumberFormatException
166 defaultConfiguration.setProperty(doubleKey, "abc");
167 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
169 defaultConfiguration.setProperty(doubleKey, "1.1");
170 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
174 public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
175 String doubleKey = "doubleKey";
176 // test default value
177 Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
178 // test NumberFormatException
179 defaultConfiguration.setProperty(doubleKey, "abc");
180 Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
182 defaultConfiguration.setProperty(doubleKey, "1.1");
183 Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
187 public void testSetPropAndGetIntegerProperty() throws Exception {
188 String integerKey = "integerKey";
189 // test default value
190 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
191 // test NumberFormatException
192 defaultConfiguration.setProperty(integerKey, "abc");
193 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
195 defaultConfiguration.setProperty(integerKey, "100");
196 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
200 public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
201 String integerKey = "integerKey";
202 // test default value
203 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
204 // test NumberFormatException
205 defaultConfiguration.setProperty(integerKey, "abc");
206 Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
208 defaultConfiguration.setProperty(integerKey, "100");
209 Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
213 public void testSetPropAndGetLongProperty() throws Exception {
214 String longKey = "longKey";
215 // test default value
216 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
217 // test NumberFormatException
218 defaultConfiguration.setProperty(longKey, "abc");
219 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
221 defaultConfiguration.setProperty(longKey, "100");
222 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
226 public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
227 String longKey = "longKey";
228 // test default value
229 Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
230 // test NumberFormatException
231 defaultConfiguration.setProperty(longKey, "abc");
232 Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
234 defaultConfiguration.setProperty(longKey, "100");
235 Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
239 public void testSetAndGetProperties() throws Exception {
240 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
241 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
243 defaultConfiguration.setProperties(prop);
244 internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
245 Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
249 public void testSetAndGetProperty() throws Exception {
251 // test default value
252 Assert.assertTrue(null == defaultConfiguration.getProperty(key));
254 defaultConfiguration.setProperty(key, "abc");
255 Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
259 public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
261 // test default value
262 Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
263 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
265 defaultConfiguration.setProperty(key, "abc");
266 Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
270 public void testHashCode() throws Exception {
271 Properties properties = null;
272 Whitebox.setInternalState(defaultConfiguration, "properties", properties);
273 Assert.assertEquals(0, defaultConfiguration.hashCode());
276 Whitebox.setInternalState(defaultConfiguration, "properties", prop);
277 Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
281 public void testIsPropertyDefined() throws Exception {
284 Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
286 defaultConfiguration.setProperty(key, "abc");
287 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
291 public void testIsValidBoolean() throws Exception {
294 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
295 // test exist with invalid
296 defaultConfiguration.setProperty(key, "abc");
297 Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
298 // test exist with valid
299 defaultConfiguration.setProperty(key, "True");
300 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
301 defaultConfiguration.setProperty(key, "FaLse");
302 Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
306 public void testIsValidDouble() throws Exception {
309 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
310 // test exist with invalid
311 defaultConfiguration.setProperty(key, "abc");
312 Assert.assertFalse(defaultConfiguration.isValidDouble(key));
313 // test exist with valid
314 defaultConfiguration.setProperty(key, "2");
315 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
316 defaultConfiguration.setProperty(key, "3.45");
317 Assert.assertTrue(defaultConfiguration.isValidDouble(key));
321 public void testIsValidInteger() throws Exception {
324 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
325 // test exist with invalid
326 defaultConfiguration.setProperty(key, "abc");
327 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
328 defaultConfiguration.setProperty(key, "3.45");
329 Assert.assertFalse(defaultConfiguration.isValidInteger(key));
330 // test exist with valid
331 defaultConfiguration.setProperty(key, "2");
332 Assert.assertTrue(defaultConfiguration.isValidInteger(key));
336 public void testIsValidLong() throws Exception {
339 Assert.assertFalse(defaultConfiguration.isValidLong(key));
340 // test exist with invalid
341 defaultConfiguration.setProperty(key, "abc");
342 Assert.assertFalse(defaultConfiguration.isValidLong(key));
343 defaultConfiguration.setProperty(key, "3.45");
344 Assert.assertFalse(defaultConfiguration.isValidLong(key));
345 // test exist with valid
346 defaultConfiguration.setProperty(key, "2");
347 Assert.assertTrue(defaultConfiguration.isValidLong(key));
351 public void testSetPropertiesWithInputStream() throws Exception {
352 InputStream mockIS = mock(InputStream.class);
353 defaultConfiguration.setProperties(mockIS);
355 Properties mockProp = mock(Properties.class);
356 Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
357 Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
358 defaultConfiguration.setProperties(mockIS);
359 // Should come here without exception
363 public void testToString() throws Exception {
364 Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
365 Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
366 internalProp.size(), internalProp.keySet().toString()),
367 defaultConfiguration.toString());