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