Continue removing ECOMP trademark from lic header.
[appc.git] / appc-common / src / test / java / org / onap / appc / configuration / DefaultConfigurationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.configuration;
25
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;
35
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";
41
42     private Properties prop = new Properties();
43     private DefaultConfiguration defaultConfiguration;
44
45     @Before
46     public void setUp() throws Exception {
47         prop.setProperty(propKey1, propValue1);
48         prop.setProperty(propKey2, propValue2);
49
50         defaultConfiguration = new DefaultConfiguration();
51     }
52
53     @Test
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());
59     }
60
61     @Test
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);
69     }
70
71     @Test
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"));
77
78         // test with not match DefaultConfiguration object
79         defaultConfiguration.setProperties(prop);
80         DefaultConfiguration newConfig = new DefaultConfiguration();
81         Assert.assertFalse(defaultConfiguration.equals(newConfig));
82
83         // test with matching DefaultConfiguration object
84         newConfig.setProperties(prop);
85         Assert.assertTrue(defaultConfiguration.equals(newConfig));
86     }
87
88     @Test
89     public void testSetPropAndGetBooleanProperty() throws Exception {
90         String booleanKey = "booleanKey";
91         // test default value
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));
105     }
106
107     @Test
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));
125     }
126
127     @Test
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));
135         // test normal
136         defaultConfiguration.setProperty(doubleKey, "1.1");
137         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
138     }
139
140     @Test
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));
148         // test normal
149         defaultConfiguration.setProperty(doubleKey, "1.1");
150         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
151     }
152
153     @Test
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));
161         // test normal
162         defaultConfiguration.setProperty(integerKey, "100");
163         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
164     }
165
166     @Test
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));
174         // test normal
175         defaultConfiguration.setProperty(integerKey, "100");
176         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
177     }
178
179     @Test
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));
187         // test normal
188         defaultConfiguration.setProperty(longKey, "100");
189         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
190     }
191
192     @Test
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));
200         // test normal
201         defaultConfiguration.setProperty(longKey, "100");
202         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
203     }
204
205     @Test
206     public void testSetAndGetProperties() throws Exception {
207         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
208         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
209
210         defaultConfiguration.setProperties(prop);
211         internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
212         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
213     }
214
215     @Test
216     public void testSetAndGetProperty() throws Exception {
217         String key = "key";
218         // test default value
219         Assert.assertTrue(null == defaultConfiguration.getProperty(key));
220         // test normal
221         defaultConfiguration.setProperty(key, "abc");
222         Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
223     }
224
225     @Test
226     public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
227         String key = "key";
228         // test default value
229         Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
230         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
231         // test normal
232         defaultConfiguration.setProperty(key, "abc");
233         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
234     }
235
236     @Test
237     public void testHashCode() throws Exception {
238         Properties properties = null;
239         Whitebox.setInternalState(defaultConfiguration, "properties", properties);
240         Assert.assertEquals(0, defaultConfiguration.hashCode());
241
242
243         Whitebox.setInternalState(defaultConfiguration, "properties", prop);
244         Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
245     }
246
247     @Test
248     public void testIsPropertyDefined() throws Exception {
249         String key = "key";
250         // test not exist
251         Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
252         // test exist
253         defaultConfiguration.setProperty(key, "abc");
254         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
255     }
256
257     @Test
258     public void testIsValidBoolean() throws Exception {
259         String key = "key";
260         // test not exist
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));
270     }
271
272     @Test
273     public void testIsValidDouble() throws Exception {
274         String key = "key";
275         // test not exist
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));
285     }
286
287     @Test
288     public void testIsValidInteger() throws Exception {
289         String key = "key";
290         // test not exist
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));
300     }
301
302     @Test
303     public void testIsValidLong() throws Exception {
304         String key = "key";
305         // test not exist
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));
315     }
316
317     @Test
318     public void testSetPropertiesWithInputStream() throws Exception {
319         InputStream mockIS = mock(InputStream.class);
320         defaultConfiguration.setProperties(mockIS);
321
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
327     }
328
329     @Test
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());
335     }
336 }