Bug 5947: added some tests for osgi in dom-broker 56/42556/13
authorPeter Nosal <peter.nosal@pantheon.tech>
Tue, 26 Jul 2016 11:32:04 +0000 (13:32 +0200)
committerRobert Varga <nite@hq.sk>
Sat, 30 Jul 2016 00:14:07 +0000 (00:14 +0000)
Change-Id: I3d404e70bd34daa47a86f072420c5d06c7674425
Signed-off-by: Peter Nosal <peter.nosal@pantheon.tech>
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/OsgiBundleScanningSchemaServiceTest.java [new file with mode: 0644]
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/SchemaServiceActivatorTest.java [new file with mode: 0644]

diff --git a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/OsgiBundleScanningSchemaServiceTest.java b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/OsgiBundleScanningSchemaServiceTest.java
new file mode 100644 (file)
index 0000000..ff59055
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * 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
+ */
+package org.opendaylight.mdsal.dom.broker.osgi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Method;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.dom.broker.util.TestModel;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+
+public class OsgiBundleScanningSchemaServiceTest {
+
+    private OsgiBundleScanningSchemaService osgiService;
+
+    @Before
+    public void setUp() throws Exception {
+        destroyInstance();
+        final BundleContext bundleContext = mock(BundleContext.class, "bundleContext");
+        doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
+        doNothing().when(bundleContext).addBundleListener(any());
+        doReturn(new Bundle[] {}).when(bundleContext).getBundles();
+        doNothing().when(bundleContext).addServiceListener(any(), any());
+        doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
+        doNothing().when(bundleContext).removeBundleListener(any());
+        doNothing().when(bundleContext).removeServiceListener(any());
+        osgiService = OsgiBundleScanningSchemaService.createInstance(bundleContext);
+        assertEquals(osgiService, OsgiBundleScanningSchemaService.getInstance());
+        assertEquals(bundleContext, osgiService.getContext());
+    }
+
+    @After
+    public void destroyInstance() throws Exception {
+        try {
+            OsgiBundleScanningSchemaService.getInstance();
+            OsgiBundleScanningSchemaService.destroyInstance();
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalStateException);
+        }
+    }
+
+    @Test
+    public void basicTest() throws Exception {
+        final SchemaContext schemaContext = TestModel.createTestContext();
+        final SchemaContextListener schemaContextListener = mock(SchemaContextListener.class);
+        doNothing().when(schemaContextListener).onGlobalContextUpdated(schemaContext);
+        osgiService.registerSchemaContextListener(schemaContextListener);
+
+        final Method schemaContextUpdate =
+                OsgiBundleScanningSchemaService.class.getDeclaredMethod("updateContext", SchemaContext.class);
+        schemaContextUpdate.setAccessible(true);
+        schemaContextUpdate.invoke(osgiService, schemaContext);
+
+        osgiService.registerSchemaContextListener(schemaContextListener);
+        assertNull(osgiService.getSchemaContext());
+        osgiService.close();
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void sessionContextTest() throws Exception {
+        osgiService.getSessionContext();
+    }
+}
\ No newline at end of file
diff --git a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/SchemaServiceActivatorTest.java b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/osgi/SchemaServiceActivatorTest.java
new file mode 100644 (file)
index 0000000..a0751b6
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * 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
+ */
+package org.opendaylight.mdsal.dom.broker.osgi;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.lang.reflect.Field;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+public class SchemaServiceActivatorTest {
+
+    @Test
+    public void basicTest() throws Exception {
+        final BundleContext bundleContext = mock(BundleContext.class);
+        doReturn(mock(Filter.class)).when(bundleContext).createFilter(any());
+        doNothing().when(bundleContext).addBundleListener(any());
+        doReturn(new Bundle[] {}).when(bundleContext).getBundles();
+        doNothing().when(bundleContext).addServiceListener(any(), any());
+        doReturn(new ServiceReference<?>[] {}).when(bundleContext).getServiceReferences(anyString(), any());
+        doReturn(mock(ServiceRegistration.class)).when(bundleContext).registerService(any(Class.class), any(), any());
+        doNothing().when(bundleContext).removeBundleListener(any());
+        doNothing().when(bundleContext).removeServiceListener(any());
+        final SchemaServiceActivator schemaServiceActivator = new SchemaServiceActivator();
+        schemaServiceActivator.start(bundleContext);
+
+        final ServiceRegistration registration = mock(ServiceRegistration.class);
+        final OsgiBundleScanningSchemaService osgiBundle =
+                mock(OsgiBundleScanningSchemaService.class, CALLS_REAL_METHODS);
+
+        final Field schemaServiceRegField = SchemaServiceActivator.class.getDeclaredField("schemaServiceReg");
+        schemaServiceRegField.setAccessible(true);
+        schemaServiceRegField.set(schemaServiceActivator, registration);
+
+        final Field schemaServiceField = SchemaServiceActivator.class.getDeclaredField("schemaService");
+        schemaServiceField.setAccessible(true);
+        schemaServiceField.set(schemaServiceActivator, osgiBundle);
+
+        doNothing().when(registration).unregister();
+        doNothing().when(osgiBundle).close();
+
+        schemaServiceActivator.stop(bundleContext);
+        verify(registration).unregister();
+        verify(osgiBundle).close();
+    }
+
+    @After
+    @Before
+    public void destroyInstance() throws Exception {
+        try {
+            OsgiBundleScanningSchemaService.getInstance();
+            OsgiBundleScanningSchemaService.destroyInstance();
+        } catch (Exception e) {
+            assertTrue(e instanceof IllegalStateException);
+        }
+    }
+}
\ No newline at end of file