Add test for generated code checking list of dependencies.
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / AbstractConfigTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.impl;
9
10 import org.junit.After;
11 import org.mockito.Matchers;
12 import org.opendaylight.controller.config.api.jmx.CommitStatus;
13 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
14 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
15 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
16 import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator;
17 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
18 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
19 import org.opendaylight.controller.config.spi.Module;
20 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
21 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.ServiceRegistration;
24
25 import javax.management.InstanceAlreadyExistsException;
26 import javax.management.MBeanServer;
27 import javax.management.ObjectName;
28 import java.io.Closeable;
29 import java.lang.management.ManagementFactory;
30 import java.util.Dictionary;
31 import java.util.Set;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Mockito.doNothing;
36 import static org.mockito.Mockito.doReturn;
37 import static org.mockito.Mockito.mock;
38
39 /**
40  * Each test that relies on
41  * {@link org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl}
42  * needs to subclass this test.
43  * {@link org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl} is
44  * registered to platform MBean Server using
45  * {@link #initConfigTransactionManagerImpl(org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver)}
46  * typically during setting up the each test.
47  */
48 public abstract class AbstractConfigTest extends
49         AbstractLockedPlatformMBeanServerTest {
50     protected ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
51     protected ConfigRegistryImpl configRegistry;
52     protected ConfigRegistryJMXClient configRegistryClient;
53     protected BaseJMXRegistrator baseJmxRegistrator;
54     protected InternalJMXRegistrator internalJmxRegistrator;
55     protected BundleContext mockedContext = mock(BundleContext.class);
56     protected ServiceRegistration<?> mockedServiceRegistration;
57
58     // this method should be called in @Before
59     protected void initConfigTransactionManagerImpl(
60             ModuleFactoriesResolver resolver) {
61         final MBeanServer platformMBeanServer = ManagementFactory
62                 .getPlatformMBeanServer();
63
64         configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(
65                 platformMBeanServer);
66         this.mockedServiceRegistration = mock(ServiceRegistration.class);
67         doNothing().when(mockedServiceRegistration).unregister();
68         doReturn(mockedServiceRegistration).when(mockedContext).registerService(
69                 Matchers.any(String[].class), any(Closeable.class),
70                 any(Dictionary.class));
71
72         internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer);
73         baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator);
74
75         configRegistry = new ConfigRegistryImpl(resolver,
76                 platformMBeanServer, baseJmxRegistrator);
77         try {
78             configRegistryJMXRegistrator.registerToJMX(configRegistry);
79         } catch (InstanceAlreadyExistsException e) {
80             throw new RuntimeException(e);
81         }
82         configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer);
83     }
84
85     @After
86     public final void cleanUpConfigTransactionManagerImpl() {
87         configRegistryJMXRegistrator.close();
88         configRegistry.close();
89         TestingFixedThreadPool.cleanUp();
90         TestingScheduledThreadPoolImpl.cleanUp();
91     }
92
93     /**
94      * Can be called in @After of tests if some other cleanup is needed that
95      * would be discarded by closing config beans in this method
96      */
97     protected void destroyAllConfigBeans() throws Exception {
98         ConfigTransactionJMXClient transaction = configRegistryClient
99                 .createTransaction();
100         Set<ObjectName> all = transaction.lookupConfigBeans();
101         // workaround for getting same Module more times
102         while (all.size() > 0) {
103             transaction.destroyModule(all.iterator().next());
104             all = transaction.lookupConfigBeans();
105         }
106         transaction.commit();
107     }
108
109     protected void assertSame(ObjectName oN1, ObjectName oN2) {
110         assertEquals(oN1.getKeyProperty("instanceName"),
111                 oN2.getKeyProperty("instanceName"));
112         assertEquals(oN1.getKeyProperty("interfaceName"),
113                 oN2.getKeyProperty("interfaceName"));
114     }
115
116     protected void assertStatus(CommitStatus status, int expectedNewInstances,
117             int expectedRecreatedInstances, int expectedReusedInstances) {
118         assertEquals("New instances mismatch in " + status, expectedNewInstances, status.getNewInstances().size());
119         assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, status.getRecreatedInstances()
120                 .size());
121         assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, status.getReusedInstances()
122                 .size());
123     }
124
125     protected ObjectName createTestConfigBean(
126             ConfigTransactionJMXClient transaction, String implementationName,
127             String name) throws InstanceAlreadyExistsException {
128         ObjectName nameCreated = transaction.createModule(implementationName,
129                 name);
130         return nameCreated;
131     }
132
133     protected void assertBeanCount(int i, String configMXBeanName) {
134         assertEquals(i, configRegistry.lookupConfigBeans(configMXBeanName)
135                 .size());
136     }
137
138     protected void assertBeanExists(int count, String moduleName,
139             String instanceName) {
140         assertEquals(1,
141                 configRegistry.lookupConfigBeans(moduleName, instanceName)
142                         .size());
143     }
144
145     /**
146      *
147      * @param configBeanClass
148      *            Empty constructor class of config bean to be instantiated
149      *            whenever create
150      * @param implementationName
151      * @return
152      */
153     protected ClassBasedModuleFactory createClassBasedCBF(
154             Class<? extends Module> configBeanClass, String implementationName) {
155         return new ClassBasedModuleFactory(implementationName, configBeanClass);
156     }
157 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.