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