Third part of onap rename
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.configuration;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.powermock.reflect.Whitebox;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Properties;
36
37 import static org.mockito.Mockito.mock;
38
39 public class DefaultConfigurationTest {
40     private static final String propKey1 = "testKey1";
41     private static final String propKey2 = "testKey2";
42     private static final String propValue1 = "testValue1";
43     private static final String propValue2 = "testValue2";
44
45     private Properties prop = new Properties();
46     private DefaultConfiguration defaultConfiguration;
47
48     @Before
49     public void setUp() throws Exception {
50         prop.setProperty(propKey1, propValue1);
51         prop.setProperty(propKey2, propValue2);
52
53         defaultConfiguration = new DefaultConfiguration();
54     }
55
56     @Test
57     public void testClear() throws Exception {
58         Whitebox.setInternalState(defaultConfiguration, "properties", prop);
59         defaultConfiguration.clear();
60         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
61         Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
62     }
63
64     @Test
65     public void testClone() throws Exception {
66         Object clonedObject = defaultConfiguration.clone();
67         Assert.assertTrue("Should be DefaultConfiguration", 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 testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
111         String booleanKey = "booleanKey";
112         // test default value
113         Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
114         Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
115         // test match value true
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         defaultConfiguration.setProperty(booleanKey, "TrUe");
121         Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
122         // test not matching true values
123         defaultConfiguration.setProperty(booleanKey, "false");
124         Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
125         defaultConfiguration.setProperty(booleanKey, "abc");
126         Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
127     }
128
129     @Test
130     public void testSetPropAndGetDoubleProperty() throws Exception {
131         String doubleKey = "doubleKey";
132         // test default value
133         Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
134         // test NumberFormatException
135         defaultConfiguration.setProperty(doubleKey, "abc");
136         Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
137         // test normal
138         defaultConfiguration.setProperty(doubleKey, "1.1");
139         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
140     }
141
142     @Test
143     public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
144         String doubleKey = "doubleKey";
145         // test default value
146         Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
147         // test NumberFormatException
148         defaultConfiguration.setProperty(doubleKey, "abc");
149         Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
150         // test normal
151         defaultConfiguration.setProperty(doubleKey, "1.1");
152         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
153     }
154
155     @Test
156     public void testSetPropAndGetIntegerProperty() throws Exception {
157         String integerKey = "integerKey";
158         // test default value
159         Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
160         // test NumberFormatException
161         defaultConfiguration.setProperty(integerKey, "abc");
162         Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
163         // test normal
164         defaultConfiguration.setProperty(integerKey, "100");
165         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
166     }
167
168     @Test
169     public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
170         String integerKey = "integerKey";
171         // test default value
172         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
173         // test NumberFormatException
174         defaultConfiguration.setProperty(integerKey, "abc");
175         Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
176         // test normal
177         defaultConfiguration.setProperty(integerKey, "100");
178         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
179     }
180
181     @Test
182     public void testSetPropAndGetLongProperty() throws Exception {
183         String longKey = "longKey";
184         // test default value
185         Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
186         // test NumberFormatException
187         defaultConfiguration.setProperty(longKey, "abc");
188         Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
189         // test normal
190         defaultConfiguration.setProperty(longKey, "100");
191         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
192     }
193
194     @Test
195     public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
196         String longKey = "longKey";
197         // test default value
198         Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
199         // test NumberFormatException
200         defaultConfiguration.setProperty(longKey, "abc");
201         Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
202         // test normal
203         defaultConfiguration.setProperty(longKey, "100");
204         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
205     }
206
207     @Test
208     public void testSetAndGetProperties() throws Exception {
209         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
210         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
211
212         defaultConfiguration.setProperties(prop);
213         internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
214         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
215     }
216
217     @Test
218     public void testSetAndGetProperty() throws Exception {
219         String key = "key";
220         // test default value
221         Assert.assertTrue(null == defaultConfiguration.getProperty(key));
222         // test normal
223         defaultConfiguration.setProperty(key, "abc");
224         Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
225     }
226
227     @Test
228     public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
229         String key = "key";
230         // test default value
231         Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
232         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
233         // test normal
234         defaultConfiguration.setProperty(key, "abc");
235         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
236     }
237
238     @Test
239     public void testHashCode() throws Exception {
240         Properties properties = null;
241         Whitebox.setInternalState(defaultConfiguration, "properties", properties);
242         Assert.assertEquals(0, defaultConfiguration.hashCode());
243
244
245         Whitebox.setInternalState(defaultConfiguration, "properties", prop);
246         Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
247     }
248
249     @Test
250     public void testIsPropertyDefined() throws Exception {
251         String key = "key";
252         // test not exist
253         Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
254         // test exist
255         defaultConfiguration.setProperty(key, "abc");
256         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
257     }
258
259     @Test
260     public void testIsValidBoolean() throws Exception {
261         String key = "key";
262         // test not exist
263         Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
264         // test exist with invalid
265         defaultConfiguration.setProperty(key, "abc");
266         Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
267         // test exist with valid
268         defaultConfiguration.setProperty(key, "True");
269         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
270         defaultConfiguration.setProperty(key, "FaLse");
271         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
272     }
273
274     @Test
275     public void testIsValidDouble() throws Exception {
276         String key = "key";
277         // test not exist
278         Assert.assertFalse(defaultConfiguration.isValidDouble(key));
279         // test exist with invalid
280         defaultConfiguration.setProperty(key, "abc");
281         Assert.assertFalse(defaultConfiguration.isValidDouble(key));
282         // test exist with valid
283         defaultConfiguration.setProperty(key, "2");
284         Assert.assertTrue(defaultConfiguration.isValidDouble(key));
285         defaultConfiguration.setProperty(key, "3.45");
286         Assert.assertTrue(defaultConfiguration.isValidDouble(key));
287     }
288
289     @Test
290     public void testIsValidInteger() throws Exception {
291         String key = "key";
292         // test not exist
293         Assert.assertFalse(defaultConfiguration.isValidInteger(key));
294         // test exist with invalid
295         defaultConfiguration.setProperty(key, "abc");
296         Assert.assertFalse(defaultConfiguration.isValidInteger(key));
297         defaultConfiguration.setProperty(key, "3.45");
298         Assert.assertFalse(defaultConfiguration.isValidInteger(key));
299         // test exist with valid
300         defaultConfiguration.setProperty(key, "2");
301         Assert.assertTrue(defaultConfiguration.isValidInteger(key));
302     }
303
304     @Test
305     public void testIsValidLong() throws Exception {
306         String key = "key";
307         // test not exist
308         Assert.assertFalse(defaultConfiguration.isValidLong(key));
309         // test exist with invalid
310         defaultConfiguration.setProperty(key, "abc");
311         Assert.assertFalse(defaultConfiguration.isValidLong(key));
312         defaultConfiguration.setProperty(key, "3.45");
313         Assert.assertFalse(defaultConfiguration.isValidLong(key));
314         // test exist with valid
315         defaultConfiguration.setProperty(key, "2");
316         Assert.assertTrue(defaultConfiguration.isValidLong(key));
317     }
318
319     @Test
320     public void testSetPropertiesWithInputStream() throws Exception {
321         InputStream mockIS = mock(InputStream.class);
322         defaultConfiguration.setProperties(mockIS);
323
324         Properties mockProp = mock(Properties.class);
325         Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
326         Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
327         defaultConfiguration.setProperties(mockIS);
328         // Should come here without exception
329     }
330
331     @Test
332     public void testToString() throws Exception {
333         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
334         Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
335                 internalProp.size(), internalProp.keySet().toString()), defaultConfiguration.toString());
336     }
337 }