Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / osgi / ModuleFactoryBundleTrackerTest.java
index 9ac570e2eed166045ac7930425b77932b5229263..7f6253ea46b0deda09af34570ca81f4a17348a85 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2017 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,
@@ -15,21 +15,17 @@ 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;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.config.api.DependencyResolver;
 import org.opendaylight.controller.config.api.DependencyResolverFactory;
 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
@@ -49,38 +45,29 @@ public class ModuleFactoryBundleTrackerTest {
     private BundleContext context;
     @Mock
     private ServiceRegistration<?> reg;
-    @Mock
-    private BlankTransactionServiceTracker blankTxTracker;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(final InvocationOnMock invocation) throws Throwable {
-                return getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]);
-            }
-        }).when(bundle).loadClass(anyString());
+        doAnswer(invocation -> getClass().getClassLoader().loadClass((String) invocation.getArguments()[0]))
+                .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 {
-        Entry<ModuleFactory, Bundle> entry = ModuleFactoryBundleTracker.registerFactory(
-                TestingFactory.class.getName(), bundle);
-        assertEquals(TestingFactory.currentInstance, entry.getKey());
-        assertEquals(bundle, entry.getValue());
+        ModuleFactoryBundleTracker.registerFactory(TestingFactory.class.getName(), bundle);
+        verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
     }
 
     @Test
+    @SuppressWarnings("IllegalCatch")
     public void testRegisterFactoryInstantiateEx() throws Exception {
         try {
             ModuleFactoryBundleTracker.registerFactory(WrongConstructorTestingFactory.class.getName(), bundle);
-        } catch (Exception e) {
+        } catch (final Exception e) {
             verifyZeroInteractions(context);
             assertNotNull(e.getCause());
             assertEquals(InstantiationException.class, e.getCause().getClass());
@@ -91,10 +78,11 @@ public class ModuleFactoryBundleTrackerTest {
     }
 
     @Test
+    @SuppressWarnings("IllegalCatch")
     public void testRegisterFactoryInstantiateExAccess() throws Exception {
         try {
             ModuleFactoryBundleTracker.registerFactory(NoAccessConstructorTestingFactory.class.getName(), bundle);
-        } catch (Exception e) {
+        } catch (final Exception e) {
             verifyZeroInteractions(context);
             assertNotNull(e.getCause());
             assertEquals(IllegalAccessException.class, e.getCause().getClass());
@@ -105,10 +93,11 @@ public class ModuleFactoryBundleTrackerTest {
     }
 
     @Test
+    @SuppressWarnings("IllegalCatch")
     public void testRegisterFactoryNotExtending() throws Exception {
         try {
             ModuleFactoryBundleTracker.registerFactory(NotExtendingTestingFactory.class.getName(), bundle);
-        } catch (Exception e) {
+        } catch (final Exception e) {
             verifyZeroInteractions(context);
             return;
         }
@@ -117,10 +106,11 @@ public class ModuleFactoryBundleTrackerTest {
     }
 
     @Test
+    @SuppressWarnings("IllegalCatch")
     public void testRegisterFactoryNotExisting() throws Exception {
         try {
             ModuleFactoryBundleTracker.registerFactory("Unknown class", bundle);
-        } catch (Exception e) {
+        } catch (final Exception e) {
             verifyZeroInteractions(context);
             assertNotNull(e.getCause());
             assertEquals(ClassNotFoundException.class, e.getCause().getClass());
@@ -130,37 +120,25 @@ public class ModuleFactoryBundleTrackerTest {
         fail("Cannot register without extend");
     }
 
+    @Mock
+    private BlankTransactionServiceTracker blankTxTracker;
+
     @Test
-    public void testBundleAddAndRemove() throws Exception {
+    public void testAddingBundle() 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));
-
-        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();;
+        verify(context).registerService(ModuleFactory.class.getName(), TestingFactory.currentInstance, null);
     }
 
     @Test
+    @SuppressWarnings("IllegalCatch")
     public void testAddingBundleError() throws Exception {
         final ModuleFactoryBundleTracker tracker = new ModuleFactoryBundleTracker(blankTxTracker);
         doReturn(getClass().getResource("/module-factories/module-factory-fail")).when(bundle).getEntry(anyString());
         try {
             tracker.addingBundle(bundle, mock(BundleEvent.class));
-        } catch (Exception e) {
+        } catch (final Exception e) {
             verifyZeroInteractions(context);
             return;
         }
@@ -173,9 +151,10 @@ public class ModuleFactoryBundleTrackerTest {
         }
     }
 
-    static class NotExtendingTestingFactory {}
+    static class NotExtendingTestingFactory {
+    }
 
-    static class NoAccessConstructorTestingFactory extends TestingFactory {
+    static final class NoAccessConstructorTestingFactory extends TestingFactory {
         private NoAccessConstructorTestingFactory() {
         }
     }
@@ -194,17 +173,20 @@ public class ModuleFactoryBundleTrackerTest {
         }
 
         @Override
-        public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
+        public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
+                final BundleContext bundleContext) {
             throw new UnsupportedOperationException();
         }
 
         @Override
-        public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
+        public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
+                final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
             throw new UnsupportedOperationException();
         }
 
         @Override
-        public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
+        public boolean isModuleImplementingServiceInterface(
+                final Class<? extends AbstractServiceInterface> serviceInterface) {
             throw new UnsupportedOperationException();
         }
 
@@ -214,7 +196,8 @@ public class ModuleFactoryBundleTrackerTest {
         }
 
         @Override
-        public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
+        public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory,
+                final BundleContext bundleContext) {
             throw new UnsupportedOperationException();
         }
     }