2 * Copyright (c) 2013 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.testingservices.scheduledthreadpool.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
13 import java.util.Collections;
14 import java.util.List;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.ObjectName;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool
26 .TestingScheduledThreadPoolConfigBeanMXBean;
27 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool
28 .TestingScheduledThreadPoolModuleFactory;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34 import com.google.common.collect.Sets;
37 * TestingScheduledThreadPool exports 2 interfaces: <br>
38 * {@link org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory#NAME}
40 * {@link org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory#NAME}
43 * It also exports 2 runtime beans, one default and one with additional
44 * properties of {'a':'b'}.
46 public class RuntimeBeanTest extends AbstractScheduledTest {
48 ObjectName ifc1runtimeON1 = ObjectNameUtil.createRuntimeBeanName(
49 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
50 Maps.<String, String> newHashMap());
51 // additional runtime bean
52 ObjectName ifc1runtimeON2 = ObjectNameUtil.createRuntimeBeanName(
53 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
54 ImmutableMap.of("a", "b"));
56 List<ObjectName> allObjectNames = Lists.newArrayList(ifc1runtimeON1,
59 private ObjectName createScheduled() throws InstanceAlreadyExistsException,
60 ConflictingVersionException, ValidationException {
61 ConfigTransactionJMXClient transaction = configRegistryClient
64 // create using TestingThreadPoolIfc:
65 ObjectName createdConfigBean = transaction.createModule(
66 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
69 return createdConfigBean;
73 public void testCreateScheduled() throws Exception {
78 private void checkRuntimeBeans() throws Exception {
79 // check runtime bean - on 2 places
80 for (ObjectName on : allObjectNames)
84 private void checkRuntimeBean(ObjectName on) throws Exception {
86 platformMBeanServer.getAttribute(on, "ActualNumberOfThreads"));
89 private void checkRuntimeBeanDoesNotExist(ObjectName on) throws Exception {
93 } catch (InstanceNotFoundException e) {
99 public void testLookup() throws Exception {
101 assertEquals(Sets.newHashSet(ifc1runtimeON1, ifc1runtimeON2),
102 configRegistryClient.lookupRuntimeBeans());
106 public void testReuse() throws Exception {
107 ObjectName createdConfigBean = createScheduled();
109 CommitStatus commitInfo = configRegistryClient.createTransaction()
112 // check that it was reused
113 ObjectName readableConfigBean = ObjectNameUtil
114 .withoutTransactionName(createdConfigBean);
115 List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
116 List<ObjectName> reusedInstances = Lists
117 .newArrayList(readableConfigBean);
118 List<ObjectName> recreatedInstaces = Collections
119 .<ObjectName> emptyList();
120 assertEquals(new CommitStatus(newInstances, reusedInstances,
121 recreatedInstaces), commitInfo);
126 public void testRecreate() throws Exception {
127 ObjectName createdConfigBean = createScheduled();
129 ConfigTransactionJMXClient configTransaction = configRegistryClient
130 .createTransaction();
131 ObjectName scheduledWritableON = configTransaction.lookupConfigBean(
132 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
133 TestingScheduledThreadPoolConfigBeanMXBean scheduledWritableProxy = configTransaction
134 .newMXBeanProxy(scheduledWritableON, TestingScheduledThreadPoolConfigBeanMXBean.class);
135 scheduledWritableProxy.setRecreate(true);
136 CommitStatus commitInfo = configTransaction.commit();
137 // check that it was recreated
138 ObjectName readableConfigBean = ObjectNameUtil
139 .withoutTransactionName(createdConfigBean);
140 List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
141 List<ObjectName> reusedInstances = Collections.<ObjectName> emptyList();
142 List<ObjectName> recreatedInstaces = Lists
143 .newArrayList(readableConfigBean);
144 assertEquals(new CommitStatus(newInstances, reusedInstances,
145 recreatedInstaces), commitInfo);
150 public void testDestroy_shouldUnregisterRuntimeBeans() throws Exception {
151 ObjectName createdConfigBean = createScheduled();
152 ConfigTransactionJMXClient configTransaction = configRegistryClient
153 .createTransaction();
154 configTransaction.destroyModule(ObjectNameUtil
155 .createTransactionModuleON(configTransaction.getTransactionName(), createdConfigBean));
156 configTransaction.commit();
157 for (ObjectName on : allObjectNames)
158 checkRuntimeBeanDoesNotExist(on);