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 com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Sets;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.management.InstanceAlreadyExistsException;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.ObjectName;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.ConflictingVersionException;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.jmx.CommitStatus;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolConfigBeanMXBean;
28 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
32 * TestingScheduledThreadPool exports 2 interfaces: <br>
33 * {@link org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory#NAME}
35 * {@link org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory#NAME}
38 * It also exports 2 runtime beans, one default and one with additional
39 * properties of {'a':'b'}.
41 public class RuntimeBeanTest extends AbstractScheduledTest {
43 ObjectName ifc1runtimeON1 = ObjectNameUtil.createRuntimeBeanName(
44 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
45 Maps.<String, String> newHashMap());
46 // additional runtime bean
47 ObjectName ifc1runtimeON2 = ObjectNameUtil.createRuntimeBeanName(
48 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1,
49 ImmutableMap.of("a", "b"));
51 List<ObjectName> allObjectNames = Lists.newArrayList(ifc1runtimeON1,
54 private ObjectName createScheduled() throws InstanceAlreadyExistsException,
55 ConflictingVersionException, ValidationException {
56 ConfigTransactionJMXClient transaction = configRegistryClient
59 // create using TestingThreadPoolIfc:
60 ObjectName createdConfigBean = transaction.createModule(
61 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
64 return createdConfigBean;
68 public void testCreateScheduled() throws Exception {
73 private void checkRuntimeBeans() throws Exception {
74 // check runtime bean - on 2 places
75 for (ObjectName on : allObjectNames) {
80 private static void checkRuntimeBean(final ObjectName on) throws Exception {
81 assertEquals(0, platformMBeanServer.getAttribute(on, "ActualNumberOfThreads"));
84 private static void checkRuntimeBeanDoesNotExist(final ObjectName on) throws Exception {
88 } catch (InstanceNotFoundException e) {
94 public void testLookup() throws Exception {
96 assertEquals(Sets.newHashSet(ifc1runtimeON1, ifc1runtimeON2),
97 configRegistryClient.lookupRuntimeBeans());
101 public void testReuse() throws Exception {
102 ObjectName createdConfigBean = createScheduled();
104 CommitStatus commitInfo = configRegistryClient.createTransaction()
107 // check that it was reused
108 ObjectName readableConfigBean = ObjectNameUtil
109 .withoutTransactionName(createdConfigBean);
110 List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
111 List<ObjectName> reusedInstances = Lists
112 .newArrayList(readableConfigBean);
113 List<ObjectName> recreatedInstaces = Collections
114 .<ObjectName> emptyList();
115 assertEquals(new CommitStatus(newInstances, reusedInstances,
116 recreatedInstaces), commitInfo);
121 public void testRecreate() throws Exception {
122 ObjectName createdConfigBean = createScheduled();
124 ConfigTransactionJMXClient configTransaction = configRegistryClient
125 .createTransaction();
126 ObjectName scheduledWritableON = configTransaction.lookupConfigBean(
127 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
128 TestingScheduledThreadPoolConfigBeanMXBean scheduledWritableProxy = configTransaction
129 .newMXBeanProxy(scheduledWritableON, TestingScheduledThreadPoolConfigBeanMXBean.class);
130 scheduledWritableProxy.setRecreate(true);
131 CommitStatus commitInfo = configTransaction.commit();
132 // check that it was recreated
133 ObjectName readableConfigBean = ObjectNameUtil
134 .withoutTransactionName(createdConfigBean);
135 List<ObjectName> newInstances = Collections.<ObjectName> emptyList();
136 List<ObjectName> reusedInstances = Collections.<ObjectName> emptyList();
137 List<ObjectName> recreatedInstaces = Lists
138 .newArrayList(readableConfigBean);
139 assertEquals(new CommitStatus(newInstances, reusedInstances,
140 recreatedInstaces), commitInfo);
145 public void testDestroy_shouldUnregisterRuntimeBeans() throws Exception {
146 ObjectName createdConfigBean = createScheduled();
147 ConfigTransactionJMXClient configTransaction = configRegistryClient
148 .createTransaction();
149 configTransaction.destroyModule(ObjectNameUtil
150 .createTransactionModuleON(configTransaction.getTransactionName(), createdConfigBean));
151 configTransaction.commit();
152 for (ObjectName on : allObjectNames) {
153 checkRuntimeBeanDoesNotExist(on);