Added BundleContext reference to generated factories for config subsystem
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AbstractDynamicWrapperTest.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.dynamicmbean;
9
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.api.ModuleIdentifier;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
15 import org.opendaylight.controller.config.manager.impl.AbstractLockedPlatformMBeanServerTest;
16 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPool;
17 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
18 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModule;
19 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
20 import org.opendaylight.controller.config.spi.Module;
21
22 import javax.management.*;
23 import java.lang.management.ManagementFactory;
24
25 import static org.junit.Assert.assertEquals;
26
27 public abstract class AbstractDynamicWrapperTest extends
28         AbstractLockedPlatformMBeanServerTest {
29     protected final MBeanServer platformMBeanServer = ManagementFactory
30             .getPlatformMBeanServer();
31     private static final String moduleName = "impl";
32     protected final ObjectName threadPoolDynamicWrapperON = ObjectNameUtil
33             .createReadOnlyModuleON(moduleName, "fixed1");
34     protected static final String THREAD_COUNT = "ThreadCount";
35     protected static final String TRIGGER_NEW_INSTANCE_CREATION = "TriggerNewInstanceCreation";
36
37     protected final int threadCount = 5;
38     protected TestingFixedThreadPoolModule threadPoolConfigBean;
39     private static final ModuleIdentifier moduleIdentifier = new ModuleIdentifier(
40             moduleName, "clientname2");
41
42     protected MBeanServer internalServer;
43
44     @Before
45     public void registerToJMX() throws Exception {
46         internalServer = MBeanServerFactory.createMBeanServer();
47         TestingFixedThreadPoolModuleFactory testingFixedThreadPoolConfigBeanFactory = new TestingFixedThreadPoolModuleFactory();
48         threadPoolConfigBean = testingFixedThreadPoolConfigBeanFactory
49                 .createModule("", null, null);
50
51         threadPoolConfigBean.setThreadCount(threadCount);
52         AbstractDynamicWrapper dynamicWrapper = getDynamicWrapper(
53                 threadPoolConfigBean, moduleIdentifier);
54         platformMBeanServer.registerMBean(dynamicWrapper,
55                 threadPoolDynamicWrapperON);
56     }
57
58     @After
59     public void unregisterFromJMX() throws Exception {
60         TestingFixedThreadPool.cleanUp();
61         platformMBeanServer.unregisterMBean(threadPoolDynamicWrapperON);
62         MBeanServerFactory.releaseMBeanServer(internalServer);
63     }
64
65     protected abstract AbstractDynamicWrapper getDynamicWrapper(Module module,
66             ModuleIdentifier moduleIdentifier);
67
68     @Test
69     public void testReadAttributes() throws Exception {
70
71         DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer,
72                 threadPoolDynamicWrapperON, DynamicMBean.class);
73
74         assertEquals(threadCount, proxy.getAttribute(THREAD_COUNT));
75
76         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
77                 proxy.getAttribute(TRIGGER_NEW_INSTANCE_CREATION));
78
79         AttributeList attributes = proxy.getAttributes(new String[] {
80                 THREAD_COUNT, TRIGGER_NEW_INSTANCE_CREATION });
81         assertEquals(2, attributes.size());
82         Attribute threadCountAttr = (Attribute) attributes.get(0);
83         assertEquals(THREAD_COUNT, threadCountAttr.getName());
84         assertEquals(threadCount, threadCountAttr.getValue());
85         Attribute boolTestAttr = (Attribute) attributes.get(1);
86         assertEquals(TRIGGER_NEW_INSTANCE_CREATION, boolTestAttr.getName());
87         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
88                 boolTestAttr.getValue());
89
90         MBeanInfo mBeanInfo = proxy.getMBeanInfo();
91         assertEquals(2, mBeanInfo.getAttributes().length);
92
93     }
94
95     @Test
96     public void testGettersWithMXBeanProxy() {
97         TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(
98                 platformMBeanServer, threadPoolDynamicWrapperON,
99                 TestingFixedThreadPoolConfigMXBean.class);
100         assertEquals(threadCount, proxy.getThreadCount());
101     }
102
103 }