Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / DynamicWritableWrapperTest.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 import static org.junit.Assert.fail;
12
13 import java.util.concurrent.atomic.AtomicBoolean;
14 import javax.management.Attribute;
15 import javax.management.AttributeList;
16 import javax.management.DynamicMBean;
17 import javax.management.JMX;
18 import javax.management.MBeanServerFactory;
19 import javax.management.ObjectName;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ModuleIdentifier;
22 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
23 import org.opendaylight.controller.config.manager.impl.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
24 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
27 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
28 import org.opendaylight.controller.config.spi.Module;
29
30 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
31     private final int newThreadCount = 10;
32     private final AtomicBoolean atomicBoolean = new AtomicBoolean();
33     private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(atomicBoolean);
34
35     @Override
36     protected AbstractDynamicWrapper getDynamicWrapper(final Module module, final ModuleIdentifier moduleIdentifier) {
37         return new DynamicWritableWrapper(module, moduleIdentifier, "transaction-1", readOnlyAtomicBoolean,
38                 MBeanServerFactory.createMBeanServer(), platformMBeanServer);
39     }
40
41     @Test
42     public void testSetAttribute() throws Exception {
43         DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class);
44
45         proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
46
47         assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
48         assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
49
50         AttributeList attributeList = new AttributeList();
51         attributeList.add(new Attribute(THREAD_COUNT, threadCount));
52         boolean bool = true;
53         attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
54         proxy.setAttributes(attributeList);
55
56         assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
57         assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
58     }
59
60     @Test
61     public void testSettersWithMXBeanProxy() {
62         TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON,
63                 TestingFixedThreadPoolConfigMXBean.class);
64         proxy.setThreadCount(newThreadCount);
65         assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
66     }
67
68     /*
69      * Try to call setter with ObjectName containing transaction name. Verify that
70      * ObjectName without transaction name was actually passed on the config bean.
71      */
72     @Test
73     public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON() throws Exception {
74         TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
75         TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory.createModule("", null, null);
76         ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp", "parallel");
77         ObjectName dynON2 = ObjectNameUtil.createReadOnlyModuleON(moduleIdentifier2);
78         AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean, moduleIdentifier2);
79         platformMBeanServer.registerMBean(dyn, dynON2);
80         try {
81             TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(platformMBeanServer, dynON2,
82                     TestingParallelAPSPConfigMXBean.class);
83             ObjectName withTransactionName = ObjectNameUtil.createTransactionModuleON("transaction1", "moduleName",
84                     "instanceName");
85             proxy.setThreadPool(withTransactionName);
86             ObjectName withoutTransactionName = ObjectNameUtil.withoutTransactionName(withTransactionName);
87             assertEquals(withoutTransactionName, proxy.getThreadPool());
88         } finally {
89             platformMBeanServer.unregisterMBean(dynON2);
90         }
91     }
92
93     private void setNumberOfThreads(final int numberOfThreads) throws Exception {
94         DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class);
95
96         proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
97
98     }
99
100     @Test
101     public void testDisablingOfWriteOperations() throws Exception {
102         setNumberOfThreads(newThreadCount);
103         atomicBoolean.set(true);
104         try {
105             setNumberOfThreads(newThreadCount);
106             fail();
107         } catch (final IllegalStateException e) {
108             assertEquals("Operation is not allowed now", e.getMessage());
109         } finally {
110             atomicBoolean.set(false);
111         }
112     }
113 }