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