aboutsummaryrefslogtreecommitdiffstats
path: root/appc-core/appc-common-bundle/src/test/java/org/onap/appc/configuration/DefaultConfigurationTest.java
blob: 95429f0530fc1b26402f8151a41d39104646594d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Modification Copyright (C) 2018 IBM.
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.configuration;

import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.appc.encryption.EncryptionTool;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.reflect.Whitebox;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(EncryptionTool.class)
public class DefaultConfigurationTest {
    private static final String propKey1 = "testKey1";
    private static final String propKey2 = "testKey2";
    private static final String propValue1 = "testValue1";
    private static final String propValue2 = "testValue2";
    private EncryptionTool encryptionTool;

    private Properties prop = new Properties();
    private DefaultConfiguration defaultConfiguration;

    @Before
    public void setUp() throws Exception {
        prop.setProperty(propKey1, propValue1);
        prop.setProperty(propKey2, propValue2);

        defaultConfiguration = new DefaultConfiguration();
        PowerMockito.mockStatic(EncryptionTool.class);
        encryptionTool = Mockito.mock(EncryptionTool.class);
        PowerMockito.when(EncryptionTool.getInstance()).thenReturn(encryptionTool);
    }

    @Test
    public void testClear() throws Exception {
        Whitebox.setInternalState(defaultConfiguration, "properties", prop);
        defaultConfiguration.clear();
        Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
        Assert.assertTrue("internal properties should be cleared", internalProp.isEmpty());
    }

    @Test
    public void testClone() throws Exception {
        Object clonedObject = defaultConfiguration.clone();
        Assert.assertTrue("Should be DefaultConfiguration",
                clonedObject instanceof DefaultConfiguration);
        Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
        Properties clonedInternalProp = Whitebox.getInternalState(clonedObject, "properties");
        Assert.assertEquals(internalProp, clonedInternalProp);
    }

    @Test
    public void testEquals() throws Exception {
        // test compare with null
        Assert.assertFalse(defaultConfiguration.equals(null));
        // test with non-DefaultConfiguration object
        Assert.assertFalse(defaultConfiguration.equals("abc"));

        // test with not match DefaultConfiguration object
        defaultConfiguration.setProperties(prop);
        DefaultConfiguration newConfig = new DefaultConfiguration();
        Assert.assertFalse(defaultConfiguration.equals(newConfig));

        // test with matching DefaultConfiguration object
        newConfig.setProperties(prop);
        Assert.assertTrue(defaultConfiguration.equals(newConfig));
    }

    @Test
    public void testSetPropAndGetBooleanProperty() throws Exception {
        String booleanKey = "booleanKey";
        // test default value
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
        // test match value true
        defaultConfiguration.setProperty(booleanKey, "true");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
        defaultConfiguration.setProperty(booleanKey, "True");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
        defaultConfiguration.setProperty(booleanKey, "TrUe");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
        // test not matching true values
        defaultConfiguration.setProperty(booleanKey, "false");
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
        defaultConfiguration.setProperty(booleanKey, "abc");
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
    }

    @Test
    public void testSetPropAndGetBooleanPropertyForEncryptedValue()
    {
        String booleanKey = "booleanKey";
        Mockito.when(encryptionTool.decrypt("enc:true")).thenReturn("true");
        defaultConfiguration.setProperty(booleanKey, "enc:true");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey));
    }

    @Test
    public void testSetPropAndGetBooleanPropertyForEncryptedValueException()
    {
        String booleanKey = "booleanKey";
        Mockito.when(encryptionTool.decrypt("enc:false")).thenThrow(new RuntimeException());
        defaultConfiguration.setProperty(booleanKey, "enc:false");
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey));
    }

    @Test
    public void testSetPropAndGetBooleanPropertyWithDefaultValue() throws Exception {
        String booleanKey = "booleanKey";
        // test default value
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, false));
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, true));
        // test match value true
        defaultConfiguration.setProperty(booleanKey, "true");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
        defaultConfiguration.setProperty(booleanKey, "True");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
        defaultConfiguration.setProperty(booleanKey, "TrUe");
        Assert.assertTrue(defaultConfiguration.getBooleanProperty(booleanKey, false));
        // test not matching true values
        defaultConfiguration.setProperty(booleanKey, "false");
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
        defaultConfiguration.setProperty(booleanKey, "abc");
        Assert.assertFalse(defaultConfiguration.getBooleanProperty(booleanKey, true));
    }

    @Test
    public void testSetPropAndGetDoubleProperty() throws Exception {
        String doubleKey = "doubleKey";
        // test default value
        Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
        // test NumberFormatException
        defaultConfiguration.setProperty(doubleKey, "abc");
        Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey));
        // test normal
        defaultConfiguration.setProperty(doubleKey, "1.1");
        Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey));
    }

    @Test
    public void testSetPropAndGetDoublePropertyWithDefaultValue() throws Exception {
        String doubleKey = "doubleKey";
        // test default value
        Assert.assertTrue(2.2 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
        // test NumberFormatException
        defaultConfiguration.setProperty(doubleKey, "abc");
        Assert.assertTrue(0.0 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
        // test normal
        defaultConfiguration.setProperty(doubleKey, "1.1");
        Assert.assertTrue(1.1 == defaultConfiguration.getDoubleProperty(doubleKey, 2.2));
    }

    @Test
    public void testSetPropAndGetIntegerProperty() throws Exception {
        String integerKey = "integerKey";
        // test default value
        Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
        // test NumberFormatException
        defaultConfiguration.setProperty(integerKey, "abc");
        Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey));
        // test normal
        defaultConfiguration.setProperty(integerKey, "100");
        Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey));
    }

    @Test
    public void testSetPropAndGetIntegerPropertyWithDefaultValue() throws Exception {
        String integerKey = "integerKey";
        // test default value
        Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 100));
        // test NumberFormatException
        defaultConfiguration.setProperty(integerKey, "abc");
        Assert.assertTrue(0 == defaultConfiguration.getIntegerProperty(integerKey, 100));
        // test normal
        defaultConfiguration.setProperty(integerKey, "100");
        Assert.assertTrue(100 == defaultConfiguration.getIntegerProperty(integerKey, 10));
    }

    @Test
    public void testSetPropAndGetLongProperty() throws Exception {
        String longKey = "longKey";
        // test default value
        Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
        // test NumberFormatException
        defaultConfiguration.setProperty(longKey, "abc");
        Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey));
        // test normal
        defaultConfiguration.setProperty(longKey, "100");
        Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey));
    }

    @Test
    public void testSetPropAndGetLongPropertyWithDefaultVaue() throws Exception {
        String longKey = "longKey";
        // test default value
        Assert.assertTrue(10 == defaultConfiguration.getLongProperty(longKey, 10));
        // test NumberFormatException
        defaultConfiguration.setProperty(longKey, "abc");
        Assert.assertTrue(0 == defaultConfiguration.getLongProperty(longKey, 10));
        // test normal
        defaultConfiguration.setProperty(longKey, "100");
        Assert.assertTrue(100 == defaultConfiguration.getLongProperty(longKey, 10));
    }

    @Test
    public void testSetAndGetProperties() throws Exception {
        Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
        Assert.assertEquals(internalProp, defaultConfiguration.getProperties());

        defaultConfiguration.setProperties(prop);
        internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
        Assert.assertEquals(internalProp, defaultConfiguration.getProperties());
    }

    @Test
    public void testSetAndGetProperty() throws Exception {
        String key = "key";
        // test default value
        Assert.assertTrue(null == defaultConfiguration.getProperty(key));
        // test normal
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertEquals("abc", defaultConfiguration.getProperty(key));
    }

    @Test
    public void testSetPropAndGetPropertyWithDefaultValue() throws Exception {
        String key = "key";
        // test default value
        Assert.assertTrue(null == defaultConfiguration.getProperty(key, null));
        Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abc"));
        // test normal
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertEquals("abc", defaultConfiguration.getProperty(key, "abcd"));
    }

    @Test
    public void testHashCode() throws Exception {
        Properties properties = null;
        Whitebox.setInternalState(defaultConfiguration, "properties", properties);
        Assert.assertEquals(0, defaultConfiguration.hashCode());


        Whitebox.setInternalState(defaultConfiguration, "properties", prop);
        Assert.assertEquals(prop.hashCode(), defaultConfiguration.hashCode());
    }

    @Test
    public void testIsPropertyDefined() throws Exception {
        String key = "key";
        // test not exist
        Assert.assertFalse(defaultConfiguration.isPropertyDefined(key));
        // test exist
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
    }

    @Test
    public void testIsValidBoolean() throws Exception {
        String key = "key";
        // test not exist
        Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
        // test exist with invalid
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertFalse(defaultConfiguration.isValidBoolean(key));
        // test exist with valid
        defaultConfiguration.setProperty(key, "True");
        Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
        defaultConfiguration.setProperty(key, "FaLse");
        Assert.assertTrue(defaultConfiguration.isPropertyDefined(key));
    }

    @Test
    public void testIsValidDouble() throws Exception {
        String key = "key";
        // test not exist
        Assert.assertFalse(defaultConfiguration.isValidDouble(key));
        // test exist with invalid
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertFalse(defaultConfiguration.isValidDouble(key));
        // test exist with valid
        defaultConfiguration.setProperty(key, "2");
        Assert.assertTrue(defaultConfiguration.isValidDouble(key));
        defaultConfiguration.setProperty(key, "3.45");
        Assert.assertTrue(defaultConfiguration.isValidDouble(key));
    }

    @Test
    public void testIsValidInteger() throws Exception {
        String key = "key";
        // test not exist
        Assert.assertFalse(defaultConfiguration.isValidInteger(key));
        // test exist with invalid
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertFalse(defaultConfiguration.isValidInteger(key));
        defaultConfiguration.setProperty(key, "3.45");
        Assert.assertFalse(defaultConfiguration.isValidInteger(key));
        // test exist with valid
        defaultConfiguration.setProperty(key, "2");
        Assert.assertTrue(defaultConfiguration.isValidInteger(key));
    }

    @Test
    public void testIsValidLong() throws Exception {
        String key = "key";
        // test not exist
        Assert.assertFalse(defaultConfiguration.isValidLong(key));
        // test exist with invalid
        defaultConfiguration.setProperty(key, "abc");
        Assert.assertFalse(defaultConfiguration.isValidLong(key));
        defaultConfiguration.setProperty(key, "3.45");
        Assert.assertFalse(defaultConfiguration.isValidLong(key));
        // test exist with valid
        defaultConfiguration.setProperty(key, "2");
        Assert.assertTrue(defaultConfiguration.isValidLong(key));
    }

    @Test
    public void testSetPropertiesWithInputStream() throws Exception {
        InputStream mockIS = mock(InputStream.class);
        defaultConfiguration.setProperties(mockIS);

        Properties mockProp = mock(Properties.class);
        Mockito.doThrow(new IOException("testing exception")).when(mockProp).load(mockIS);
        Whitebox.setInternalState(defaultConfiguration, "properties", mockProp);
        defaultConfiguration.setProperties(mockIS);
        // Should come here without exception
    }

    @Test
    public void testToString() throws Exception {
        Properties internalProp = Whitebox.getInternalState(defaultConfiguration, "properties");
        Assert.assertEquals(String.format("Configuration: %d properties, keys:[%s]",
                internalProp.size(), internalProp.keySet().toString()),
                defaultConfiguration.toString());
    }
}