2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
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.dynamicmbean.ReadOnlyAtomicBoolean.ReadOnlyAtomicBooleanImpl;
24 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModule;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
27 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolConfigMXBean;
28 import org.opendaylight.controller.config.spi.Module;
30 public class DynamicWritableWrapperTest extends AbstractDynamicWrapperTest {
31 private final int newThreadCount = 10;
32 private final AtomicBoolean atomicBoolean = new AtomicBoolean();
33 private final ReadOnlyAtomicBoolean readOnlyAtomicBoolean = new ReadOnlyAtomicBooleanImpl(atomicBoolean);
36 protected AbstractDynamicWrapper getDynamicWrapper(final Module module, final ModuleIdentifier moduleIdentifier) {
37 return new DynamicWritableWrapper(module, moduleIdentifier, "transaction-1", readOnlyAtomicBoolean,
38 MBeanServerFactory.createMBeanServer(), platformMBeanServer);
42 public void testSetAttribute() throws Exception {
43 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class);
45 proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount));
47 assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT));
48 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
50 AttributeList attributeList = new AttributeList();
51 attributeList.add(new Attribute(THREAD_COUNT, threadCount));
53 attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool));
54 proxy.setAttributes(attributeList);
56 assertEquals(threadCount, threadPoolConfigBean.getThreadCount());
57 assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation());
61 public void testSettersWithMXBeanProxy() {
62 TestingFixedThreadPoolConfigMXBean proxy = JMX.newMXBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON,
63 TestingFixedThreadPoolConfigMXBean.class);
64 proxy.setThreadCount(newThreadCount);
65 assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount());
69 * Try to call setter with ObjectName containing transaction name. Verify that
70 * ObjectName without transaction name was actually passed on the config bean.
73 public void testObjectNameSetterWithONContainingTransaction_shouldBeTranslatedToReadOnlyON() throws Exception {
74 TestingParallelAPSPModuleFactory testingParallelAPSPConfigBeanFactory = new TestingParallelAPSPModuleFactory();
75 TestingParallelAPSPModule apspConfigBean = testingParallelAPSPConfigBeanFactory.createModule("", null, null);
76 ModuleIdentifier moduleIdentifier2 = new ModuleIdentifier("apsp", "parallel");
77 ObjectName dynON2 = ObjectNameUtil.createReadOnlyModuleON(moduleIdentifier2);
78 AbstractDynamicWrapper dyn = getDynamicWrapper(apspConfigBean, moduleIdentifier2);
79 platformMBeanServer.registerMBean(dyn, dynON2);
81 TestingParallelAPSPConfigMXBean proxy = JMX.newMBeanProxy(platformMBeanServer, dynON2,
82 TestingParallelAPSPConfigMXBean.class);
83 ObjectName withTransactionName = ObjectNameUtil.createTransactionModuleON("transaction1", "moduleName",
85 proxy.setThreadPool(withTransactionName);
86 ObjectName withoutTransactionName = ObjectNameUtil.withoutTransactionName(withTransactionName);
87 assertEquals(withoutTransactionName, proxy.getThreadPool());
89 platformMBeanServer.unregisterMBean(dynON2);
93 private void setNumberOfThreads(final int numberOfThreads) throws Exception {
94 DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class);
96 proxy.setAttribute(new Attribute(THREAD_COUNT, numberOfThreads));
101 public void testDisablingOfWriteOperations() throws Exception {
102 setNumberOfThreads(newThreadCount);
103 atomicBoolean.set(true);
105 setNumberOfThreads(newThreadCount);
107 } catch (final IllegalStateException e) {
108 assertEquals("Operation is not allowed now", e.getMessage());
110 atomicBoolean.set(false);