aed414192bcc53bab3d0b54d68edde273af10081
[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  * 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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.configuration;
27
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;
37
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";
43
44     private Properties prop = new Properties();
45     private DefaultConfiguration defaultConfiguration;
46
47     @Before
48     public void setUp() throws Exception {
49         prop.setProperty(propKey1, propValue1);
50         prop.setProperty(propKey2, propValue2);
51
52         defaultConfiguration = new DefaultConfiguration();
53     }
54
55     @Test
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());
61     }
62
63     @Test
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);
71     }
72
73     @Test
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"));
79
80         // test with not match DefaultConfiguration object
81         defaultConfiguration.setProperties(prop);
82         DefaultConfiguration newConfig = new DefaultConfiguration();
83         Assert.assertFalse(defaultConfiguration.equals(newConfig));
84
85         // test with matching DefaultConfiguration object
86         newConfig.setProperties(prop);
87         Assert.assertTrue(defaultConfiguration.equals(newConfig));
88     }
89
90     @Test
91     public void testSetPropAndGetBooleanProperty() throws Exception {
92         String booleanKey = "booleanKey";
93         // test default value
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));
107     }
108     
109     @Test
110     public void testSetPropAndGetBooleanPropertyForEncryptedValue()
111     {
112         String booleanKey = "booleanKey";
113         defaultConfiguration.setProperty(booleanKey, "enc:true");
114         Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
115     }
116     
117    
118     @Test
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));
136     }
137
138     @Test
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));
146         // test normal
147         defaultConfiguration.setProperty(doubleKey, "1.1");
148         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
149     }
150
151     @Test
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));
159         // test normal
160         defaultConfiguration.setProperty(doubleKey, "1.1");
161         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
162     }
163
164     @Test
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));
172         // test normal
173         defaultConfiguration.setProperty(integerKey, "100");
174         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
175     }
176
177     @Test
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));
185         // test normal
186         defaultConfiguration.setProperty(integerKey, "100");
187         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
188     }
189
190     @Test
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));
198         // test normal
199         defaultConfiguration.setProperty(longKey, "100");
200         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
201     }
202
203     @Test
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));
211         // test normal
212         defaultConfiguration.setProperty(longKey, "100");
213         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
214     }
215
216     @Test
217     public void testSetAndGetProperties() throws Exception {
218         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
219         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
220
221         defaultConfiguration.setProperties(prop);
222         internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
223         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
224     }
225
226     @Test
227     public void testSetAndGetProperty() throws Exception {
228         String key = "key";
229         // test default value
230         Assert.assertTrue(null == defaultConfiguration.getProperty(key));
231         // test normal
232         defaultConfiguration.setProperty(key, "abc");
233         Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
234     }
235
236     @Test
237     public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
238         String key = "key";
239         // test default value
240         Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
241         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
242         // test normal
243         defaultConfiguration.setProperty(key, "abc");
244         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
245     }
246
247     @Test
248     public void testHashCode() throws Exception {
249         Properties properties = null;
250         Whitebox.setInternalState(defaultConfiguration, "properties", properties);
251         Assert.assertEquals(0, defaultConfiguration.hashCode());
252
253
254         Whitebox.setInternalState(defaultConfiguration, "properties", prop);
255         Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
256     }
257
258     @Test
259     public void testIsPropertyDefined() throws Exception {
260         String key = "key";
261         // test not exist
262         Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
263         // test exist
264         defaultConfiguration.setProperty(key, "abc");
265         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
266     }
267
268     @Test
269     public void testIsValidBoolean() throws Exception {
270         String key = "key";
271         // test not exist
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));
281     }
282
283     @Test
284     public void testIsValidDouble() throws Exception {
285         String key = "key";
286         // test not exist
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));
296     }
297
298     @Test
299     public void testIsValidInteger() throws Exception {
300         String key = "key";
301         // test not exist
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));
311     }
312
313     @Test
314     public void testIsValidLong() throws Exception {
315         String key = "key";
316         // test not exist
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));
326     }
327
328     @Test
329     public void testSetPropertiesWithInputStream() throws Exception {
330         InputStream mockIS = mock(InputStream.class);
331         defaultConfiguration.setProperties(mockIS);
332
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
338     }
339
340     @Test
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());
346     }
347 }