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