Convert ServiceReferenceUtilTest to JUnit5 92/107792/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Sep 2023 15:59:50 +0000 (17:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Sep 2023 16:00:29 +0000 (18:00 +0200)
This is a very simple test. Convert it to JUnit5 and use Mockito to
driver the ServiceReference implementation.

Change-Id: I9e57a055e19912a2fdb0f23ab954e1d52a689253
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bundles-test-lib/src/test/java/org/opendaylight/odlparent/bundlestest/lib/ServiceReferenceUtilTest.java

index 2c746a721818046d99b5bcab695dc1f1dc2334db..f15684b99d26ae4c6cce9162c8d570e7aeefbfff 100644 (file)
@@ -10,13 +10,13 @@ package org.opendaylight.odlparent.bundlestest.lib;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
 
-import java.util.Dictionary;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import org.junit.Test;
-import org.osgi.framework.Bundle;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.osgi.framework.ServiceReference;
 
 /**
@@ -24,16 +24,25 @@ import org.osgi.framework.ServiceReference;
  *
  * @author Michael Vorburger.ch
  */
-public class ServiceReferenceUtilTest {
+@ExtendWith(MockitoExtension.class)
+class ServiceReferenceUtilTest {
+    @Mock
+    private ServiceReference<?> serviceReference;
 
     @Test
-    public void testGetUsingBundleSymbolicNames() {
-        assertEquals(List.of(), ServiceReferenceUtil.getUsingBundleSymbolicNames(getServiceReference()));
+    void testGetUsingBundleSymbolicNames() {
+        doReturn(null).when(serviceReference).getUsingBundles();
+        assertEquals(List.of(), ServiceReferenceUtil.getUsingBundleSymbolicNames(serviceReference));
     }
 
     @Test
-    public void testGetProperties() {
-        final var map = ServiceReferenceUtil.getProperties(getServiceReference());
+    void testGetProperties() {
+        doReturn(new String[] { "property1", "property2", "property3" }).when(serviceReference).getPropertyKeys();
+        doReturn("value1").when(serviceReference).getProperty("property1");
+        doReturn(List.of("value2.1", "value2.2")).when(serviceReference).getProperty("property2");
+        doReturn(null).when(serviceReference).getProperty("property3");
+
+        final var map = ServiceReferenceUtil.getProperties(serviceReference);
         assertEquals(3, map.size());
         assertEquals("value1", map.get("property1"));
         assertEquals(List.of("value2.1", "value2.2"), map.get("property2"));
@@ -41,60 +50,4 @@ public class ServiceReferenceUtilTest {
         assertTrue(map.containsKey("property3"));
         assertNull(map.get("property3"));
     }
-
-    private static ServiceReference<?> getServiceReference() {
-        return new TestServiceReference();
-    }
-
-    private static final class TestServiceReference implements ServiceReference<Object> {
-
-        private final Map<String, Object> properties = new HashMap<>();
-
-        TestServiceReference() {
-            properties.put("property1", "value1");
-            properties.put("property2", List.of("value2.1", "value2.2"));
-            properties.put("property3", null);
-        }
-
-        @Override
-        public Object getProperty(String key) {
-            return properties.get(key);
-        }
-
-        @Override
-        public String[] getPropertyKeys() {
-            return properties.keySet().stream().toArray(String[]::new);
-        }
-
-        @Override
-        public Bundle getBundle() {
-            return null;
-        }
-
-        @Override
-        public Bundle[] getUsingBundles() {
-            return null;
-        }
-
-        @Override
-        public boolean isAssignableTo(Bundle bundle, String className) {
-            return false;
-        }
-
-        @Override
-        public int compareTo(Object reference) {
-            return 0;
-        }
-
-        @Override
-        public Dictionary<String, Object> getProperties() {
-            return null;
-        }
-
-        @Override
-        public <A> A adapt(Class<A> type) {
-            throw new UnsupportedOperationException();
-        }
-    }
-
 }