BUG 2839: Config: remove dependencies on commons-io
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / AbstractConfigTest.java
index a7e3fa6c6fa29343f62bffb163fb35719aadae88..ab44bb34958da1d22fed964b8ed701a2a7427c24 100644 (file)
@@ -7,33 +7,52 @@
  */
 package org.opendaylight.controller.config.manager.impl;
 
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.base.Preconditions;
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Dictionary;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.RuntimeMBeanException;
 import org.junit.After;
+import org.junit.Before;
 import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.config.api.jmx.CommitStatus;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
 import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator;
+import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
+import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
+import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
 import org.opendaylight.controller.config.spi.Module;
 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
+import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-import java.io.Closeable;
-import java.lang.management.ManagementFactory;
-import java.util.Dictionary;
-import java.util.Set;
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
 /**
  * Each test that relies on
  * {@link org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl}
@@ -50,40 +69,97 @@ public abstract class AbstractConfigTest extends
     protected ConfigRegistryJMXClient configRegistryClient;
     protected BaseJMXRegistrator baseJmxRegistrator;
     protected InternalJMXRegistrator internalJmxRegistrator;
-    protected BundleContext mockedContext = mock(BundleContext.class);
+    @Mock
+    protected BundleContext mockedContext;
+    @Mock
     protected ServiceRegistration<?> mockedServiceRegistration;
 
+    @Before
+    public void setUpMocks() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+
+    // Default handler for OSGi service registration
+    protected static class RecordingBundleContextServiceRegistrationHandler implements BundleContextServiceRegistrationHandler {
+        private final List<RegistrationHolder> registrations = new LinkedList<>();
+        @Override
+        public void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props) {
+
+            registrations.add(new RegistrationHolder(clazz, serviceInstance, props));
+        }
+
+        public List<RegistrationHolder> getRegistrations() {
+            return registrations;
+        }
+
+        protected static class RegistrationHolder {
+            protected final Class<?> clazz;
+            protected final Object instance;
+            protected final Dictionary<String, ?> props;
+
+            public RegistrationHolder(Class<?> clazz, Object instance, Dictionary<String, ?> props) {
+                this.clazz = clazz;
+                this.instance = instance;
+                this.props = props;
+            }
+        }
+
+    }
+
+    protected BundleContextServiceRegistrationHandler currentBundleContextServiceRegistrationHandler;
+
+    protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(Class<?> serviceType) {
+        return currentBundleContextServiceRegistrationHandler;
+    }
+
     // this method should be called in @Before
     protected void initConfigTransactionManagerImpl(
             ModuleFactoriesResolver resolver) {
+
         final MBeanServer platformMBeanServer = ManagementFactory
                 .getPlatformMBeanServer();
 
-        configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(
-                platformMBeanServer);
-        this.mockedServiceRegistration = mock(ServiceRegistration.class);
-        doNothing().when(mockedServiceRegistration).unregister();
-        doReturn(mockedServiceRegistration).when(mockedContext).registerService(
-                Matchers.any(String[].class), any(Closeable.class),
-                any(Dictionary.class));
+        configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(platformMBeanServer);
+        initBundleContext();
 
         internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer);
         baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator);
 
-        configRegistry = new ConfigRegistryImpl(resolver,
-                platformMBeanServer, baseJmxRegistrator);
+        configRegistry = new ConfigRegistryImpl(resolver, platformMBeanServer, baseJmxRegistrator, new BindingContextProvider() {
+            @Override
+            public synchronized void update(final ClassLoadingStrategy classLoadingStrategy, final SchemaContextProvider ctxProvider) {
+                // NOOP
+            }
+
+            @Override
+            public synchronized BindingRuntimeContext getBindingContext() {
+                return getBindingRuntimeContext();
+            }
+        });
+
         try {
             configRegistryJMXRegistrator.registerToJMX(configRegistry);
         } catch (InstanceAlreadyExistsException e) {
             throw new RuntimeException(e);
         }
         configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer);
+        currentBundleContextServiceRegistrationHandler = new RecordingBundleContextServiceRegistrationHandler();
+    }
+
+    private void initBundleContext() {
+        doNothing().when(mockedServiceRegistration).unregister();
+        RegisterServiceAnswer answer = new RegisterServiceAnswer();
+        doAnswer(answer).when(mockedContext).registerService(Matchers.<String>any(), any(), Matchers.<Dictionary<String, ?>>any());
+        doAnswer(answer).when(mockedContext).registerService(Matchers.<Class>any(), any(), Matchers.<Dictionary<String, ?>>any());
     }
 
     @After
     public final void cleanUpConfigTransactionManagerImpl() {
         configRegistryJMXRegistrator.close();
         configRegistry.close();
+        TestingFixedThreadPool.cleanUp();
+        TestingScheduledThreadPoolImpl.cleanUp();
     }
 
     /**
@@ -102,42 +178,21 @@ public abstract class AbstractConfigTest extends
         transaction.commit();
     }
 
-    protected void assertSame(ObjectName oN1, ObjectName oN2) {
-        assertEquals(oN1.getKeyProperty("instanceName"),
-                oN2.getKeyProperty("instanceName"));
-        assertEquals(oN1.getKeyProperty("interfaceName"),
-                oN2.getKeyProperty("interfaceName"));
-    }
-
     protected void assertStatus(CommitStatus status, int expectedNewInstances,
             int expectedRecreatedInstances, int expectedReusedInstances) {
-        assertEquals(expectedNewInstances, status.getNewInstances().size());
-        assertEquals(expectedRecreatedInstances, status.getRecreatedInstances()
+        assertEquals("New instances mismatch in " + status, expectedNewInstances, status.getNewInstances().size());
+        assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, status.getRecreatedInstances()
                 .size());
-        assertEquals(expectedReusedInstances, status.getReusedInstances()
+        assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, status.getReusedInstances()
                 .size());
     }
 
-    protected ObjectName createTestConfigBean(
-            ConfigTransactionJMXClient transaction, String implementationName,
-            String name) throws InstanceAlreadyExistsException {
-        ObjectName nameCreated = transaction.createModule(implementationName,
-                name);
-        return nameCreated;
-    }
 
     protected void assertBeanCount(int i, String configMXBeanName) {
         assertEquals(i, configRegistry.lookupConfigBeans(configMXBeanName)
                 .size());
     }
 
-    protected void assertBeanExists(int count, String moduleName,
-            String instanceName) {
-        assertEquals(1,
-                configRegistry.lookupConfigBeans(moduleName, instanceName)
-                        .size());
-    }
-
     /**
      *
      * @param configBeanClass
@@ -150,4 +205,112 @@ public abstract class AbstractConfigTest extends
             Class<? extends Module> configBeanClass, String implementationName) {
         return new ClassBasedModuleFactory(implementationName, configBeanClass);
     }
+
+    protected BindingRuntimeContext getBindingRuntimeContext() {
+        return mock(BindingRuntimeContext.class);
+    }
+
+    public static interface BundleContextServiceRegistrationHandler {
+
+        void handleServiceRegistration(Class<?> clazz, Object serviceInstance, Dictionary<String, ?> props);
+
+    }
+
+    private class RegisterServiceAnswer implements Answer<ServiceRegistration<?>> {
+
+        @Override
+        public ServiceRegistration<?> answer(InvocationOnMock invocation) throws Throwable {
+            Object[] args = invocation.getArguments();
+
+            Preconditions.checkArgument(args.length == 3, "Unexpected arguments size (expected 3 was %s)", args.length);
+
+            Object serviceTypeRaw = args[0];
+            Object serviceInstance = args[1];
+            Dictionary<String, ?> props = (Dictionary<String, ?>) args[2];
+
+            if (serviceTypeRaw instanceof Class) {
+                Class<?> serviceType = (Class<?>) serviceTypeRaw;
+                invokeServiceHandler(serviceInstance, serviceType, props);
+
+            } else if(serviceTypeRaw instanceof String[]) {
+                for (String className : (String[]) serviceTypeRaw) {
+                    invokeServiceHandler(serviceInstance, className, props);
+                }
+            } else if (serviceTypeRaw instanceof String) {
+                invokeServiceHandler(serviceInstance, (String) serviceTypeRaw, props);
+            } else {
+                throw new IllegalStateException("Not handling service registration of type, Unknown type" +  serviceTypeRaw);
+            }
+
+
+            return mockedServiceRegistration;
+        }
+
+        public void invokeServiceHandler(Object serviceInstance, String className, Dictionary<String, ?> props) {
+            try {
+                Class<?> serviceType = Class.forName(className);
+                invokeServiceHandler(serviceInstance, serviceType, props);
+            } catch (ClassNotFoundException e) {
+                throw new IllegalStateException("Not handling service registration of type " +  className, e);
+            }
+        }
+
+        private void invokeServiceHandler(Object serviceInstance, Class<?> serviceType, Dictionary<String, ?> props) {
+            BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType);
+
+            if (serviceRegistrationHandler != null) {
+                serviceRegistrationHandler.handleServiceRegistration(serviceType, serviceInstance, props);
+            }
+        }
+    }
+
+    /**
+     * Expand inner exception wrapped by JMX
+     *
+     * @param innerObject jmx proxy which will be wrapped and returned
+     */
+    protected <T> T rethrowCause(final T innerObject) {
+
+        Object proxy = Proxy.newProxyInstance(innerObject.getClass().getClassLoader(),
+                innerObject.getClass().getInterfaces(), new InvocationHandler() {
+                        @Override
+                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                            try {
+                                return method.invoke(innerObject, args);
+                            } catch (InvocationTargetException e) {
+                                try {
+                                    throw e.getTargetException();
+                                } catch (RuntimeMBeanException e2) {
+                                    throw e2.getTargetException();
+                                }
+                            }
+                        }
+                    }
+        );
+        return (T) proxy;
+    }
+
+    /**
+     * removes contents of the directory
+     * @param dir to be cleaned
+     * @throws IOException
+     */
+    protected void cleanDirectory(File dir) throws IOException {
+        if (!dir.isDirectory()) {
+            throw new IllegalStateException("dir must be a directory");
+        }
+
+        final File[] files = dir.listFiles();
+        if (files == null) {
+            throw new IOException("Failed to list contents of " + dir);
+        }
+
+        for (File file : files) {
+            if (file.isDirectory()) {
+                cleanDirectory(dir);
+            }
+            file.delete();
+        }
+    }
+
 }