Modify ModuleInfoBundleTracker to track RESOLVED bundles
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / ModuleFactoryBundleTrackerTest.java
index 66063c98b36ed2df8611f5e52401d52e0353357c..9ac570e2eed166045ac7930425b77932b5229263 100644 (file)
@@ -15,12 +15,14 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyObject;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doAnswer;
+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 static org.mockito.Mockito.verifyZeroInteractions;
-
+import java.util.Collection;
 import java.util.Dictionary;
+import java.util.Map.Entry;
 import java.util.Set;
 import org.junit.Before;
 import org.junit.Test;
@@ -47,6 +49,8 @@ public class ModuleFactoryBundleTrackerTest {
     private BundleContext context;
     @Mock
     private ServiceRegistration<?> reg;
+    @Mock
+    private BlankTransactionServiceTracker blankTxTracker;
 
     @Before
     public void setUp() throws Exception {
@@ -58,14 +62,18 @@ public class ModuleFactoryBundleTrackerTest {
             }
         }).when(bundle).loadClass(anyString());
         doReturn("mockBundle").when(bundle).toString();
+        doReturn("mockBundleContext").when(context).toString();
         doReturn(context).when(bundle).getBundleContext();
+        doReturn(100L).when(bundle).getBundleId();
         doReturn(reg).when(context).registerService(anyString(), anyObject(), any(Dictionary.class));
     }
 
     @Test
     public void testRegisterFactory() throws Exception {
-        ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
-        verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
+        Entry<ModuleFactory, Bundle> entry = ModuleFactoryBundleTracker.registerFactory(
+                TestingFactory.class.getName(), bundle);
+        assertEquals(TestingFactory.currentInstance, entry.getKey());
+        assertEquals(bundle, entry.getValue());
     }
 
     @Test
@@ -122,15 +130,28 @@ public class ModuleFactoryBundleTrackerTest {
         fail("Cannot register without extend");
     }
 
-    @Mock
-    private BlankTransactionServiceTracker blankTxTracker;
-
     @Test
-    public void testAddingBundle() throws Exception {
+    public void testBundleAddAndRemove() throws Exception {
         final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
         doReturn(getClass().getResource("/module-factories/module-factory-ok")).when(bundle).getEntry(anyString());
         tracker.addingBundle(bundle, mock(BundleEvent.class));
-        verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
+
+        Collection<Entry<ModuleFactory, BundleContext>> entries = tracker.getModuleFactoryEntries();
+        assertNotNull(entries);
+        assertEquals(1, entries.size());
+        Entry<ModuleFactory, BundleContext> entry = entries.iterator().next();
+        assertEquals(TestingFactory.currentInstance, entry.getKey());
+        assertEquals(context, entry.getValue());
+
+        doNothing().when(blankTxTracker).blankTransaction();;
+
+        tracker.removedBundle(bundle, mock(BundleEvent.class), bundle);
+
+        entries = tracker.getModuleFactoryEntries();
+        assertNotNull(entries);
+        assertEquals(0, entries.size());
+
+        verify(blankTxTracker).blankTransaction();;
     }
 
     @Test