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