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=d9bbeb4a2c9de5ab420eebbaaf702271e381b8a1;hp=a7e3fa6c6fa29343f62bffb163fb35719aadae88;hb=20500c9eb46d1ceb99d742d1c110dcb7c558dc2b;hpb=713279a4aa4984b57e1a5c0f6645aed9e78a65c9 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 a7e3fa6c6f..d9bbeb4a2c 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,31 +7,50 @@ */ package org.opendaylight.controller.config.manager.impl; +import com.google.common.base.Preconditions; +import junit.framework.Assert; import org.junit.After; import org.mockito.Matchers; +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.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.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.doReturn; import static org.mockito.Mockito.mock; /** @@ -53,6 +72,18 @@ public abstract class AbstractConfigTest extends protected BundleContext mockedContext = mock(BundleContext.class); protected ServiceRegistration mockedServiceRegistration; + private static final Logger logger = LoggerFactory.getLogger(AbstractConfigTest.class); + + // Default handler for OSGi service registration + private static final BundleContextServiceRegistrationHandler noopServiceRegHandler = new BundleContextServiceRegistrationHandler() { + @Override + public void handleServiceRegistration(Object serviceInstance) {} + }; + + protected BundleContextServiceRegistrationHandler getBundleContextServiceRegistrationHandler(Class serviceType) { + return noopServiceRegHandler; + } + // this method should be called in @Before protected void initConfigTransactionManagerImpl( ModuleFactoriesResolver resolver) { @@ -61,17 +92,14 @@ public abstract class AbstractConfigTest extends 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)); + initBundleContext(); internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer); baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator); configRegistry = new ConfigRegistryImpl(resolver, - platformMBeanServer, baseJmxRegistrator); + platformMBeanServer, baseJmxRegistrator, getCodecRegistry()); + try { configRegistryJMXRegistrator.registerToJMX(configRegistry); } catch (InstanceAlreadyExistsException e) { @@ -80,10 +108,41 @@ public abstract class AbstractConfigTest extends configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer); } + 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; + } + @After public final void cleanUpConfigTransactionManagerImpl() { configRegistryJMXRegistrator.close(); configRegistry.close(); + TestingFixedThreadPool.cleanUp(); + TestingScheduledThreadPoolImpl.cleanUp(); } /** @@ -111,19 +170,18 @@ public abstract class AbstractConfigTest extends 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, + return transaction.createModule(implementationName, name); - return nameCreated; } protected void assertBeanCount(int i, String configMXBeanName) { @@ -150,4 +208,81 @@ public abstract class AbstractConfigTest extends Class configBeanClass, String implementationName) { return new ClassBasedModuleFactory(implementationName, configBeanClass); } + + protected CodecRegistry getCodecRegistry() { + return mock(CodecRegistry.class); + } + + + public static interface BundleContextServiceRegistrationHandler { + + void handleServiceRegistration(Object serviceInstance); + + } + + private class RegisterServiceAnswer implements Answer> { + + @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]; + + 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); + } + } + + } else + logger.debug("Not handling service registration of type {}, Unknown type", serviceTypeRaw); + + return mockedServiceRegistration; + } + + private void invokeServiceHandler(Object serviceInstance, Class serviceType) { + BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandler(serviceType); + + if (serviceRegistrationHandler != null) { + serviceRegistrationHandler.handleServiceRegistration(serviceType.cast(serviceInstance)); + } + } + } + + /** + * Expand inner exception wrapped by JMX + * + * @param innerObject jmx proxy which will be wrapped and returned + */ + protected 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; + } + }