3e3e0c07946f6a551efcb43400f72401027664b0
[ovsdb.git] / utils / config / src / test / java / org / opendaylight / ovsdb / utils / config / ConfigPropertiesTest.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.ovsdb.utils.config;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.mockStatic;
14 import static org.mockito.Mockito.when;
15
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.osgi.framework.Bundle;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.FrameworkUtil;
22
23 @RunWith(MockitoJUnitRunner.StrictStubs.class)
24 public class ConfigPropertiesTest {
25
26     private static final String TEST_PROPERTY_KEY1 = "foobar34465$3467";
27     private static final String TEST_PROPERTY_MOCK_VALUE1 = "xbarMock1";
28     private static final String TEST_PROPERTY_KEY2 = "foobar34465$3468";
29     private static final String TEST_PROPERTY_MOCK_VALUE2 = "xbarMock2";
30     private static final String TEST_PROPERTY_KEY_NOT_FOUND = "foobarKey2_12445^%346";
31     private static final String DEFAULT_PROPERTY_VALUE = "xbarDefaultValue";
32
33     @Test
34     public void testGetProperty() {
35         try (var frameworkUtil = mockStatic(FrameworkUtil.class)) {
36             final var bundleWithoutBundleNoContext = mock(Bundle.class);
37             final var bundle = mock(Bundle.class);
38
39             // mock #1
40             frameworkUtil.when(() -> FrameworkUtil.getBundle(getClass()))
41                 .thenReturn(bundle);
42             frameworkUtil.when(() -> FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
43                 .thenReturn(bundleWithoutBundleNoContext);
44
45             // mock #2
46             BundleContext bundleContext = mock(BundleContext.class);
47             when(bundle.getBundleContext()).thenReturn(bundleContext);
48             when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
49             // mock #3
50             when(bundleContext.getProperty(TEST_PROPERTY_KEY1)).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
51             when(bundleContext.getProperty(TEST_PROPERTY_KEY2)).thenReturn(null);
52             when(bundleContext.getProperty(TEST_PROPERTY_KEY_NOT_FOUND)).thenReturn(null);
53
54             // mock #4, Mockito says:
55             //    It is not possible to mock static methods of java.lang.System to avoid interfering with class loading
56             //    what leads to infinite loops
57             //
58             // To work around this limitation, we will simply add the property explicitly into System
59             System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
60
61             // test 1. bundle is null, returned from a mock
62             assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
63             assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
64             assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
65                 bundleWithoutBundleNoContext);
66
67             // test 2. bundleContext is null
68             assertNull(bundleWithoutBundleNoContext.getBundleContext());
69             assertEquals(bundle.getBundleContext(), bundleContext);
70
71             // test 3. value returned from bundleContext.getProperty() is null.
72             // Then System.getProperty() is called and can return a valid value if key is found.
73             assertEquals(TEST_PROPERTY_MOCK_VALUE2, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY2));
74
75             // test 4. value returned from ConfigProperties.getProperty is null
76             final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
77             assertNull(value41);  // class has no bundle
78             final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
79                 TEST_PROPERTY_KEY1);
80             assertNull(value42);  // class has no bundleContext
81             final String value43 = ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
82             assertNull(value43);  // bundleContext will not know about key provided
83
84             // test 5. value returned from ConfigProperties.getProperty is the default value provided
85             assertEquals(DEFAULT_PROPERTY_VALUE,
86                 ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE));
87
88             // test 6. value returned from ConfigProperties.getProperty is the mocked value
89             assertEquals(TEST_PROPERTY_MOCK_VALUE1, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1));
90             assertEquals(TEST_PROPERTY_MOCK_VALUE1,
91                 ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE));
92         }
93     }
94
95     // Helper classes used to de-mux mock behaviors
96     private static class ConfigPropertiesTestMockingBundleNoContext {
97     }
98
99     private static class ConfigPropertiesTestMocking {
100     }
101 }