Add unit-tests for ConfigProperties 75/12475/4
authorFlavio Fernandes <ffernand@redhat.com>
Mon, 3 Nov 2014 21:40:27 +0000 (16:40 -0500)
committerFlavio Fernandes <ffernand@redhat.com>
Wed, 5 Nov 2014 15:39:57 +0000 (10:39 -0500)
Patch 4: Implement feedback from shague

Change-Id: I23cc7b70032e3b884171b00d52fa5c8630fda587
Signed-off-by: Flavio Fernandes <ffernand@redhat.com>
utils/config/pom.xml
utils/config/src/test/java/org/opendaylight/ovsdb/utils/config/ConfigPropertiesTest.java [new file with mode: 0644]

index eb4514d59d2316465c332da0cc367b71501beb11..d6b58e7a8e7b423f13eeeeaf7221f76243b823c5 100644 (file)
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-mockito</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-module-junit4</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
diff --git a/utils/config/src/test/java/org/opendaylight/ovsdb/utils/config/ConfigPropertiesTest.java b/utils/config/src/test/java/org/opendaylight/ovsdb/utils/config/ConfigPropertiesTest.java
new file mode 100644 (file)
index 0000000..af56f14
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+* Copyright (C) 2014 Red Hat, Inc.
+*
+* This program and the accompanying materials are made available under the
+* terms of the Eclipse Public License v1.0 which accompanies this distribution,
+* and is available at http://www.eclipse.org/legal/epl-v10.html
+*
+* Authors : Flavio Fernandes
+*/
+package org.opendaylight.ovsdb.utils.config;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.lang.System;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.powermock.api.mockito.PowerMockito;
+import org.osgi.framework.FrameworkUtil;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest( {System.class, FrameworkUtil.class} )
+ public class ConfigPropertiesTest {
+
+    private static final String TEST_PROPERTY_KEY1 = "foobar34465$3467";
+    private static final String TEST_PROPERTY_MOCK_VALUE1 = "xbarMock1";
+    private static final String TEST_PROPERTY_KEY2 = "foobar34465$3468";
+    private static final String TEST_PROPERTY_MOCK_VALUE2 = "xbarMock2";
+    private static final String TEST_PROPERTY_KEY_NOT_FOUND = "foobarKey2_12445^%346";
+    private static final String DEFAULT_PROPERTY_VALUE = "xbarDefaultValue";
+
+    @Test
+    public void testGetProperty() {
+        Bundle bundleWithoutBundleNoContext = mock(Bundle.class);
+        Bundle bundle = mock(Bundle.class);
+        BundleContext bundleContext = mock(BundleContext.class);
+
+        // mock #1
+        PowerMockito.mockStatic(FrameworkUtil.class);
+        PowerMockito.when(FrameworkUtil.getBundle(this.getClass())).thenReturn(bundle);
+        PowerMockito.when(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
+                .thenReturn(bundleWithoutBundleNoContext);
+        // mock #2
+        when(bundle.getBundleContext()).thenReturn(bundleContext);
+        when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
+        // mock #3
+        when(bundleContext.getProperty(eq(TEST_PROPERTY_KEY1))).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
+        // mock #4
+        // PowerMockito.mockStatic(System.class);
+        // PowerMockito.when(System.getProperty(eq(TEST_PROPERTY_KEY2))).thenReturn(TEST_PROPERTY_MOCK_VALUE2);
+        // NOTE: The mock #4 above is not supported by PowerMockito. To work around this limitation,
+        // we will simply add the property explicitly into System (instead of the solution mentioned in link below).
+        // See: http://javax0.wordpress.com/2013/01/29/how-to-mock-the-system-class/
+        System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
+
+        // test 1. bundle is null, returned from a mock
+        assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
+        assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
+        assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
+                     bundleWithoutBundleNoContext);
+
+        // test 2. bundleContext is null
+        assertNull(bundleWithoutBundleNoContext.getBundleContext());
+        assertEquals(bundle.getBundleContext(), bundleContext);
+
+        // test 3. value returned from bundleContext.getProperty() is null.
+        // Then System.getProperty() is called and can return a valid value if key is found.
+        final String value31 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY2);
+        assertEquals(TEST_PROPERTY_MOCK_VALUE2, value31);
+
+        // test 4. value returned from ConfigProperties.getProperty is null
+        final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
+        assertNull(value41);  // class has no bundle
+        final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
+                                                            TEST_PROPERTY_KEY1);
+        assertNull(value42);  // class has no bundleContext
+        final String value43 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
+        assertNull(value43);  // bundleContext will not know about key provided
+
+        // test 5. value returned from ConfigProperties.getProperty is the default value provided
+        final String value5 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE);
+        assertEquals(DEFAULT_PROPERTY_VALUE, value5);
+
+        // test 6. value returned from ConfigProperties.getProperty is the mocked value
+        final String value61 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1);
+        assertEquals(TEST_PROPERTY_MOCK_VALUE1, value61);
+        final String value62 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE);
+        assertEquals(TEST_PROPERTY_MOCK_VALUE1, value62);
+    }
+
+    // Helper classes used to de-mux mock behaviors
+    private class ConfigPropertiesTestMockingBundleNoContext {
+    }
+    private class ConfigPropertiesTestMocking {
+    }
+}