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.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
34 * .scheduledthreadpool.TestingScheduledThreadPoolModuleFactory#NAME}
36 * {@link org.opendaylight.controller.config.manager
37 * .testingservices.threadpool.TestingFixedThreadPoolModuleFactory#NAME}
40 * It also exports 2 runtime beans, one default and one with additional
41 * properties of {'a':'b'}.
43 public class RuntimeBeanTest extends AbstractScheduledTest {
45 ObjectName ifc1runtimeON1 = ObjectNameUtil.createRuntimeBeanName(TestingScheduledThreadPoolModuleFactory.NAME,
46 SCHEDULED1, Maps.<String, String>newHashMap());
47 // additional runtime bean
48 ObjectName ifc1runtimeON2 = ObjectNameUtil.createRuntimeBeanName(TestingScheduledThreadPoolModuleFactory.NAME,
49 SCHEDULED1, ImmutableMap.of("a", "b"));
51 List<ObjectName> allObjectNames = Lists.newArrayList(ifc1runtimeON1, ifc1runtimeON2);
53 private ObjectName createScheduled()
54 throws InstanceAlreadyExistsException, ConflictingVersionException, ValidationException {
55 ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
57 // create using TestingThreadPoolIfc:
58 ObjectName createdConfigBean = transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
62 return createdConfigBean;
66 public void testCreateScheduled() throws Exception {
71 private void checkRuntimeBeans() throws Exception {
72 // check runtime bean - on 2 places
73 for (ObjectName on : allObjectNames) {
78 private static void checkRuntimeBean(final ObjectName on) throws Exception {
79 assertEquals(0, platformMBeanServer.getAttribute(on, "ActualNumberOfThreads"));
82 private static void checkRuntimeBeanDoesNotExist(final ObjectName on) throws Exception {
86 } catch (final InstanceNotFoundException e) {
92 public void testLookup() throws Exception {
94 assertEquals(Sets.newHashSet(ifc1runtimeON1, ifc1runtimeON2), configRegistryClient.lookupRuntimeBeans());
98 public void testReuse() throws Exception {
99 ObjectName createdConfigBean = createScheduled();
101 CommitStatus commitInfo = configRegistryClient.createTransaction().commit();
103 // check that it was reused
104 ObjectName readableConfigBean = ObjectNameUtil.withoutTransactionName(createdConfigBean);
105 List<ObjectName> newInstances = Collections.<ObjectName>emptyList();
106 List<ObjectName> reusedInstances = Lists.newArrayList(readableConfigBean);
107 List<ObjectName> recreatedInstaces = Collections.<ObjectName>emptyList();
108 assertEquals(new CommitStatus(newInstances, reusedInstances, recreatedInstaces), commitInfo);
113 public void testRecreate() throws Exception {
114 ObjectName createdConfigBean = createScheduled();
116 ConfigTransactionJMXClient configTransaction = configRegistryClient.createTransaction();
117 ObjectName scheduledWritableON = configTransaction
118 .lookupConfigBean(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
119 TestingScheduledThreadPoolConfigBeanMXBean scheduledWritableProxy = configTransaction
120 .newMXBeanProxy(scheduledWritableON, TestingScheduledThreadPoolConfigBeanMXBean.class);
121 scheduledWritableProxy.setRecreate(true);
122 CommitStatus commitInfo = configTransaction.commit();
123 // check that it was recreated
124 ObjectName readableConfigBean = ObjectNameUtil.withoutTransactionName(createdConfigBean);
125 List<ObjectName> newInstances = Collections.<ObjectName>emptyList();
126 List<ObjectName> reusedInstances = Collections.<ObjectName>emptyList();
127 List<ObjectName> recreatedInstaces = Lists.newArrayList(readableConfigBean);
128 assertEquals(new CommitStatus(newInstances, reusedInstances, recreatedInstaces), commitInfo);
133 public void testDestroy_shouldUnregisterRuntimeBeans() throws Exception {
134 ObjectName createdConfigBean = createScheduled();
135 ConfigTransactionJMXClient configTransaction = configRegistryClient.createTransaction();
136 configTransaction.destroyModule(
137 ObjectNameUtil.createTransactionModuleON(configTransaction.getTransactionName(), createdConfigBean));
138 configTransaction.commit();
139 for (ObjectName on : allObjectNames) {
140 checkRuntimeBeanDoesNotExist(on);