X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2FAbstractConfigTest.java;h=919aeda5ae299ae465db50f2ca073a9a53c602ed;hp=d9bbeb4a2c9de5ab420eebbaaf702271e381b8a1;hb=db89afdefb3cfa4454dfdc6008e1d54d3ede66fb;hpb=20500c9eb46d1ceb99d742d1c110dcb7c558dc2b diff --git a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java index d9bbeb4a2c..919aeda5ae 100644 --- a/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java +++ b/opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java @@ -7,51 +7,50 @@ */ 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 junit.framework.Assert; +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.jmx.JMXNotifierConfigRegistry; +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.yang.data.impl.codec.CodecRegistry; +import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy; +import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext; +import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.management.InstanceAlreadyExistsException; -import javax.management.MBeanServer; -import javax.management.ObjectName; -import javax.management.RuntimeMBeanException; -import java.io.Closeable; -import java.io.InputStream; -import java.lang.management.ManagementFactory; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -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.List; -import java.util.Set; - -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; /** * Each test that relies on @@ -62,85 +61,97 @@ import static org.mockito.Mockito.mock; * {@link #initConfigTransactionManagerImpl(org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver)} * typically during setting up the each test. */ -public abstract class AbstractConfigTest extends - AbstractLockedPlatformMBeanServerTest { +public abstract class AbstractConfigTest extends AbstractLockedPlatformMBeanServerTest { protected ConfigRegistryJMXRegistrator configRegistryJMXRegistrator; protected ConfigRegistryImpl configRegistry; + private JMXNotifierConfigRegistry notifyingConfigRegistry; protected ConfigRegistryJMXClient configRegistryClient; protected BaseJMXRegistrator baseJmxRegistrator; - protected InternalJMXRegistrator internalJmxRegistrator; - protected BundleContext mockedContext = mock(BundleContext.class); + @Mock + protected BundleContext mockedContext; + @Mock protected ServiceRegistration mockedServiceRegistration; + protected BundleContextServiceRegistrationHandler currentBundleContextServiceRegistrationHandler; - private static final Logger logger = LoggerFactory.getLogger(AbstractConfigTest.class); + @Before + public void setUpMocks() { + MockitoAnnotations.initMocks(this); + } // Default handler for OSGi service registration - private static final BundleContextServiceRegistrationHandler noopServiceRegHandler = new BundleContextServiceRegistrationHandler() { + protected static class RecordingBundleContextServiceRegistrationHandler implements BundleContextServiceRegistrationHandler { + private final List registrations = new LinkedList<>(); @Override - public void handleServiceRegistration(Object serviceInstance) {} - }; + public void handleServiceRegistration(final Class clazz, final Object serviceInstance, final Dictionary props) { + this.registrations.add(new RegistrationHolder(clazz, serviceInstance, props)); + } + + public List getRegistrations() { + return this.registrations; + } + + protected static class RegistrationHolder { + protected final Class clazz; + protected final Object instance; + protected final Dictionary props; + + public RegistrationHolder(final Class clazz, final Object instance, final Dictionary props) { + this.clazz = clazz; + this.instance = instance; + this.props = props; + } + } + } - protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(Class serviceType) { - return noopServiceRegHandler; + protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(final Class serviceType) { + return this.currentBundleContextServiceRegistrationHandler; } // this method should be called in @Before - protected void initConfigTransactionManagerImpl( - ModuleFactoriesResolver resolver) { - final MBeanServer platformMBeanServer = ManagementFactory - .getPlatformMBeanServer(); + protected void initConfigTransactionManagerImpl(final ModuleFactoriesResolver resolver) { - configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator( - platformMBeanServer); + final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); + + this.configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(platformMBeanServer); initBundleContext(); - internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer); - baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator); + this.baseJmxRegistrator = new BaseJMXRegistrator(platformMBeanServer); - configRegistry = new ConfigRegistryImpl(resolver, - platformMBeanServer, baseJmxRegistrator, getCodecRegistry()); + this.configRegistry = new ConfigRegistryImpl(resolver, platformMBeanServer, this.baseJmxRegistrator, new BindingContextProvider() { + @Override + public synchronized void update(final ClassLoadingStrategy classLoadingStrategy, final SchemaContextProvider ctxProvider) { + // NOOP + } + + @Override + public synchronized BindingRuntimeContext getBindingContext() { + return getBindingRuntimeContext(); + } + }); + this.notifyingConfigRegistry = new JMXNotifierConfigRegistry(this.configRegistry, platformMBeanServer); try { - configRegistryJMXRegistrator.registerToJMX(configRegistry); - } catch (InstanceAlreadyExistsException e) { + this.configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry); + this.configRegistryJMXRegistrator.registerToJMX(this.notifyingConfigRegistry); + } catch (final InstanceAlreadyExistsException e) { throw new RuntimeException(e); } - configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer); + this.configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer); + this.currentBundleContextServiceRegistrationHandler = new RecordingBundleContextServiceRegistrationHandler(); } private void initBundleContext() { - this.mockedServiceRegistration = mock(ServiceRegistration.class); - doNothing().when(mockedServiceRegistration).unregister(); - - RegisterServiceAnswer answer = new RegisterServiceAnswer(); - - doAnswer(answer).when(mockedContext).registerService(Matchers.any(String[].class), - any(Closeable.class), Matchers.>any()); - doAnswer(answer).when(mockedContext).registerService(Matchers.>any(), any(Closeable.class), - Matchers.>any()); - } - - - public Collection getFilesAsInputStreams(List paths) { - final Collection resources = new ArrayList<>(); - List failedToFind = new ArrayList<>(); - for (String path : paths) { - InputStream resourceAsStream = getClass().getResourceAsStream(path); - if (resourceAsStream == null) { - failedToFind.add(path); - } else { - resources.add(resourceAsStream); - } - } - Assert.assertEquals("Some files were not found", Collections.emptyList(), failedToFind); - - return resources; + doNothing().when(this.mockedServiceRegistration).unregister(); + final RegisterServiceAnswer answer = new RegisterServiceAnswer(); + doAnswer(answer).when(this.mockedContext).registerService(Matchers.any(), any(), Matchers.>any()); + doAnswer(answer).when(this.mockedContext).registerService(Matchers.any(), any(), Matchers.>any()); } @After public final void cleanUpConfigTransactionManagerImpl() { - configRegistryJMXRegistrator.close(); - configRegistry.close(); + this.configRegistryJMXRegistrator.close(); + this.notifyingConfigRegistry.close(); + this.configRegistry.close(); TestingFixedThreadPool.cleanUp(); TestingScheduledThreadPoolImpl.cleanUp(); } @@ -150,7 +161,7 @@ public abstract class AbstractConfigTest extends * would be discarded by closing config beans in this method */ protected void destroyAllConfigBeans() throws Exception { - ConfigTransactionJMXClient transaction = configRegistryClient + final ConfigTransactionJMXClient transaction = this.configRegistryClient .createTransaction(); Set all = transaction.lookupConfigBeans(); // workaround for getting same Module more times @@ -161,39 +172,18 @@ 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) { + protected void assertStatus(final CommitStatus status, final int expectedNewInstances, + final int expectedRecreatedInstances, final int expectedReusedInstances) { assertEquals("New instances mismatch in " + status, expectedNewInstances, status.getNewInstances().size()); - assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, status.getRecreatedInstances() - .size()); - assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, status.getReusedInstances() - .size()); + assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, + status.getRecreatedInstances().size()); + assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, + status.getReusedInstances().size()); } - protected ObjectName createTestConfigBean( - ConfigTransactionJMXClient transaction, String implementationName, - String name) throws InstanceAlreadyExistsException { - return transaction.createModule(implementationName, - name); - } - 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()); + protected void assertBeanCount(final int i, final String configMXBeanName) { + assertEquals(i, this.configRegistry.lookupConfigBeans(configMXBeanName).size()); } /** @@ -204,58 +194,62 @@ public abstract class AbstractConfigTest extends * @param implementationName * @return */ - protected ClassBasedModuleFactory createClassBasedCBF( - Class configBeanClass, String implementationName) { + protected ClassBasedModuleFactory createClassBasedCBF(final Class configBeanClass, + final String implementationName) { return new ClassBasedModuleFactory(implementationName, configBeanClass); } - protected CodecRegistry getCodecRegistry() { - return mock(CodecRegistry.class); + protected BindingRuntimeContext getBindingRuntimeContext() { + return mock(BindingRuntimeContext.class); } - - public static interface BundleContextServiceRegistrationHandler { - - void handleServiceRegistration(Object serviceInstance); - + public interface BundleContextServiceRegistrationHandler { + void handleServiceRegistration(Class clazz, Object serviceInstance, Dictionary props); } private class RegisterServiceAnswer implements Answer> { - @Override - public ServiceRegistration answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); + public ServiceRegistration answer(final InvocationOnMock invocation) throws Throwable { + final 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]; + final Object serviceTypeRaw = args[0]; + final Object serviceInstance = args[1]; + @SuppressWarnings("unchecked") + final + Dictionary props = (Dictionary) args[2]; if (serviceTypeRaw instanceof Class) { - Class serviceType = (Class) serviceTypeRaw; - invokeServiceHandler(serviceInstance, serviceType); - - } else if(serviceTypeRaw instanceof String[]) { - for (String className : (String[]) serviceTypeRaw) { - try { - Class serviceType = Class.forName(className); - invokeServiceHandler(serviceInstance, serviceType); - } catch (ClassNotFoundException e) { - logger.warn("Not handling service registration of type {} ", className, e); - } + final Class serviceType = (Class) serviceTypeRaw; + invokeServiceHandler(serviceInstance, serviceType, props); + } else if (serviceTypeRaw instanceof String[]) { + for (final 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); + } - } else - logger.debug("Not handling service registration of type {}, Unknown type", serviceTypeRaw); + return AbstractConfigTest.this.mockedServiceRegistration; + } - return mockedServiceRegistration; + public void invokeServiceHandler(final Object serviceInstance, final String className, final Dictionary props) { + try { + final Class serviceType = Class.forName(className); + invokeServiceHandler(serviceInstance, serviceType, props); + } catch (final ClassNotFoundException e) { + throw new IllegalStateException("Not handling service registration of type " + className, e); + } } - private void invokeServiceHandler(Object serviceInstance, Class serviceType) { - BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType); + private void invokeServiceHandler(final Object serviceInstance, final Class serviceType, final Dictionary props) { + final BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType); if (serviceRegistrationHandler != null) { - serviceRegistrationHandler.handleServiceRegistration(serviceType.cast(serviceInstance)); + serviceRegistrationHandler.handleServiceRegistration(serviceType, serviceInstance, props); } } } @@ -266,23 +260,46 @@ public abstract class AbstractConfigTest extends * @param innerObject jmx proxy which will be wrapped and returned */ protected T rethrowCause(final T innerObject) { - - Object proxy = Proxy.newProxyInstance(innerObject.getClass().getClassLoader(), + @SuppressWarnings("unchecked") + final T proxy = (T)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(); + @Override + public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { + try { + return method.invoke(innerObject, args); + } catch (final InvocationTargetException e) { + try { + throw e.getTargetException(); + } catch (final RuntimeMBeanException e2) { + throw e2.getTargetException(); + } + } + } } - } - } - }); - return (T) proxy; + ); + return proxy; } + /** + * removes contents of the directory + * @param dir to be cleaned + * @throws IOException + */ + protected void cleanDirectory(final 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 (final File file : files) { + if (file.isDirectory()) { + cleanDirectory(dir); + } + file.delete(); + } + } }