95429f0530fc1b26402f8151a41d39104646594d
[appc.git] / appc-core / appc-common-bundle / 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  * 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
16  * 
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  * 
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.
24  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.configuration;
29
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;
44
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;
53
54     private Properties prop = new Properties();
55     private DefaultConfiguration defaultConfiguration;
56
57     @Before
58     public void setUp() throws Exception {
59         prop.setProperty(propKey1, propValue1);
60         prop.setProperty(propKey2, propValue2);
61
62         defaultConfiguration = new DefaultConfiguration();
63         PowerMockito.mockStatic(EncryptionTool.class);
64         encryptionTool = Mockito.mock(EncryptionTool.class);
65         PowerMockito.when(EncryptionTool.getInstance()).thenReturn(encryptionTool);
66     }
67
68     @Test
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());
74     }
75
76     @Test
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);
84     }
85
86     @Test
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"));
92
93         // test with not match DefaultConfiguration object
94         defaultConfiguration.setProperties(prop);
95         DefaultConfiguration newConfig = new DefaultConfiguration();
96         Assert.assertFalse(defaultConfiguration.equals(newConfig));
97
98         // test with matching DefaultConfiguration object
99         newConfig.setProperties(prop);
100         Assert.assertTrue(defaultConfiguration.equals(newConfig));
101     }
102
103     @Test
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));
120     }
121
122     @Test
123     public void testSetPropAndGetBooleanPropertyForEncryptedValue()
124     {
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));
129     }
130
131     @Test
132     public void testSetPropAndGetBooleanPropertyForEncryptedValueException()
133     {
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));
138     }
139
140     @Test
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));
158     }
159
160     @Test
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));
168         // test normal
169         defaultConfiguration.setProperty(doubleKey, "1.1");
170         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
171     }
172
173     @Test
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));
181         // test normal
182         defaultConfiguration.setProperty(doubleKey, "1.1");
183         Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
184     }
185
186     @Test
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));
194         // test normal
195         defaultConfiguration.setProperty(integerKey, "100");
196         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
197     }
198
199     @Test
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));
207         // test normal
208         defaultConfiguration.setProperty(integerKey, "100");
209         Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
210     }
211
212     @Test
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));
220         // test normal
221         defaultConfiguration.setProperty(longKey, "100");
222         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
223     }
224
225     @Test
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));
233         // test normal
234         defaultConfiguration.setProperty(longKey, "100");
235         Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
236     }
237
238     @Test
239     public void testSetAndGetProperties() throws Exception {
240         Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
241         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
242
243         defaultConfiguration.setProperties(prop);
244         internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
245         Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
246     }
247
248     @Test
249     public void testSetAndGetProperty() throws Exception {
250         String key = "key";
251         // test default value
252         Assert.assertTrue(null == defaultConfiguration.getProperty(key));
253         // test normal
254         defaultConfiguration.setProperty(key, "abc");
255         Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
256     }
257
258     @Test
259     public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
260         String key = "key";
261         // test default value
262         Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
263         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
264         // test normal
265         defaultConfiguration.setProperty(key, "abc");
266         Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
267     }
268
269     @Test
270     public void testHashCode() throws Exception {
271         Properties properties = null;
272         Whitebox.setInternalState(defaultConfiguration, "properties", properties);
273         Assert.assertEquals(0, defaultConfiguration.hashCode());
274
275
276         Whitebox.setInternalState(defaultConfiguration, "properties", prop);
277         Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
278     }
279
280     @Test
281     public void testIsPropertyDefined() throws Exception {
282         String key = "key";
283         // test not exist
284         Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
285         // test exist
286         defaultConfiguration.setProperty(key, "abc");
287         Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
288     }
289
290     @Test
291     public void testIsValidBoolean() throws Exception {
292         String key = "key";
293         // test not exist
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));
303     }
304
305     @Test
306     public void testIsValidDouble() throws Exception {
307         String key = "key";
308         // test not exist
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));
318     }
319
320     @Test
321     public void testIsValidInteger() throws Exception {
322         String key = "key";
323         // test not exist
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));
333     }
334
335     @Test
336     public void testIsValidLong() throws Exception {
337         String key = "key";
338         // test not exist
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));
348     }
349
350     @Test
351     public void testSetPropertiesWithInputStream() throws Exception {
352         InputStream mockIS = mock(InputStream.class);
353         defaultConfiguration.setProperties(mockIS);
354
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
360     }
361
362     @Test
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());
368     }
369 }