Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / AbstractDynamicWrapperTest.java
1 /*
2  * Copyright (c) 2013, 2017 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 AbstractLockedPlatformMBeanServerTest {
34     protected final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
35     private static final String MODULE_NAME = "impl";
36     protected final ObjectName threadPoolDynamicWrapperON =
37             ObjectNameUtil.createReadOnlyModuleON(MODULE_NAME, "fixed1");
38     protected static final String THREAD_COUNT = "ThreadCount";
39     protected static final String TRIGGER_NEW_INSTANCE_CREATION = "TriggerNewInstanceCreation";
40
41     protected final int threadCount = 5;
42     protected TestingFixedThreadPoolModule threadPoolConfigBean;
43     private static final ModuleIdentifier MODULE_IDENTIFIER =
44             new ModuleIdentifier(MODULE_NAME, "clientname2");
45
46     protected MBeanServer internalServer;
47
48     @Before
49     public void registerToJMX() throws Exception {
50         internalServer = MBeanServerFactory.createMBeanServer();
51         TestingFixedThreadPoolModuleFactory testingFixedThreadPoolConfigBeanFactory =
52                 new TestingFixedThreadPoolModuleFactory();
53         threadPoolConfigBean = testingFixedThreadPoolConfigBeanFactory.createModule("", null, null);
54
55         threadPoolConfigBean.setThreadCount(threadCount);
56         AbstractDynamicWrapper dynamicWrapper = getDynamicWrapper(threadPoolConfigBean, MODULE_IDENTIFIER);
57         platformMBeanServer.registerMBean(dynamicWrapper, threadPoolDynamicWrapperON);
58     }
59
60     @After
61     public void unregisterFromJMX() throws Exception {
62         TestingFixedThreadPool.cleanUp();
63         platformMBeanServer.unregisterMBean(threadPoolDynamicWrapperON);
64         MBeanServerFactory.releaseMBeanServer(internalServer);
65     }
66
67     protected abstract AbstractDynamicWrapper getDynamicWrapper(Module module, ModuleIdentifier moduleIdentifier);
68
69     @Test
70     public void testReadAttributes() throws Exception {
71         DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class);
72
73         assertEquals(threadCount, proxy.getAttribute(THREAD_COUNT));
74
75         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(),
76                 proxy.getAttribute(TRIGGER_NEW_INSTANCE_CREATION));
77
78         AttributeList attributes = proxy.getAttributes(new String[] { THREAD_COUNT, TRIGGER_NEW_INSTANCE_CREATION });
79         assertEquals(2, attributes.size());
80         Attribute threadCountAttr = (Attribute) attributes.get(0);
81         assertEquals(THREAD_COUNT, threadCountAttr.getName());
82         assertEquals(threadCount, threadCountAttr.getValue());
83         Attribute boolTestAttr = (Attribute) attributes.get(1);
84         assertEquals(TRIGGER_NEW_INSTANCE_CREATION, boolTestAttr.getName());
85         assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(), boolTestAttr.getValue());
86
87         MBeanInfo beanInfo = proxy.getMBeanInfo();
88         assertEquals(2, beanInfo.getAttributes().length);
89     }
90
91     @Test
92     public void testGettersWithMXBeanProxy() {
93         TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON,
94                 TestingFixedThreadPoolConfigMXBean.class);
95         assertEquals(threadCount, proxy.getThreadCount());
96     }
97 }