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