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