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