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