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.assertFalse;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.junit.matchers.JUnitMatchers.containsString;
17 import javax.annotation.Nullable;
18 import javax.management.DynamicMBean;
19 import javax.management.InstanceAlreadyExistsException;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.ObjectName;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
27 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
28 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
29 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool
30 .TestingScheduledThreadPoolModuleFactory;
31 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
33 public class TwoInterfacesExportTest extends AbstractScheduledTest {
35 private void assertExists(String moduleName, String instanceName)
37 assertExists(null, moduleName, instanceName);
40 private void assertExists(@Nullable ConfigTransactionJMXClient transaction,
41 String moduleName, String instanceName)
42 throws InstanceNotFoundException {
43 if (transaction != null) {
44 transaction.lookupConfigBean(moduleName, instanceName);
46 configRegistryClient.newMBeanProxy(
47 ObjectNameUtil.createTransactionModuleON(
48 transaction.getTransactionName(), moduleName,
49 instanceName), DynamicMBean.class).getMBeanInfo();
51 configRegistryClient.lookupConfigBean(moduleName, instanceName);
53 configRegistryClient.newMBeanProxy(
54 ObjectNameUtil.createReadOnlyModuleON(moduleName,
55 instanceName), DynamicMBean.class).getMBeanInfo();
59 private void assertNotExists(String moduleName, String instanceName) {
60 assertNotExists(null, moduleName, instanceName);
63 private void assertNotExists(
64 @Nullable ConfigTransactionJMXClient transaction,
65 String moduleName, String instanceName) {
67 if (transaction != null) {
69 transaction.lookupConfigBean(moduleName, instanceName);
71 } catch (InstanceNotFoundException e) {
76 configRegistryClient.lookupConfigBean(moduleName, instanceName);
78 } catch (InstanceNotFoundException e) {
85 public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
86 ConfigTransactionJMXClient transaction = configRegistryClient
89 // create using TestingThreadPoolIfc:
90 ObjectName scheduled1name = transaction.createModule(
91 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
93 ObjectName retrievedName = transaction.lookupConfigBean(
94 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
95 assertEquals(scheduled1name, retrievedName);
97 // getExistingConfigBean should resolve moduleName
98 String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
99 retrievedName = transaction.lookupConfigBean(moduleName, scheduled1);
100 ObjectName expected = ObjectNameUtil.createTransactionModuleON(
101 transaction.getTransactionName(), moduleName, scheduled1);
102 assertEquals(expected, retrievedName);
105 transaction.commit();
106 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
107 assertFalse(TestingScheduledThreadPoolImpl.allExecutors.get(0)
110 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
112 assertExists(moduleName, scheduled1);
114 // destroy using ThreadPool ifc
115 transaction = configRegistryClient.createTransaction();
116 transaction.destroyModule(ObjectNameUtil.createTransactionModuleON(
117 transaction.getTransactionName(), moduleName, scheduled1));
118 transaction.commit();
119 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
120 assertTrue(TestingScheduledThreadPoolImpl.allExecutors.get(0)
123 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
125 // should not be in platform:
127 assertNotExists(moduleName, scheduled1);
129 transaction = configRegistryClient.createTransaction();
130 // should not be in transaction
131 assertNotExists(transaction, moduleName, scheduled1);
135 public void tryToRegisterThreadPoolWithSameName()
136 throws InstanceAlreadyExistsException {
137 ConfigTransactionJMXClient transaction = configRegistryClient
138 .createTransaction();
140 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
143 transaction.createModule(
144 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
146 } catch (InstanceAlreadyExistsException e) {
149 containsString("There is an instance registered with name ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
155 public void testRegisteringAllIfcNames() throws Exception {
156 ConfigTransactionJMXClient transaction = configRegistryClient
157 .createTransaction();
158 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
160 transaction.commit();
161 assertExists(TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
162 // another transaction
163 transaction = configRegistryClient.createTransaction();
164 assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME,
169 public void testWithAPSP_useScheduledNames()
170 throws InstanceAlreadyExistsException, ValidationException {
171 ConfigTransactionJMXClient transaction = configRegistryClient
172 .createTransaction();
173 ObjectName scheduledName = transaction.createModule(
174 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
176 ObjectName apspName = transaction.createModule(
177 TestingParallelAPSPModuleFactory.NAME, "apsp1");
178 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
179 apspName, TestingParallelAPSPConfigMXBean.class);
180 apspProxy.setThreadPool(scheduledName);
181 apspProxy.setSomeParam("someParam");
182 transaction.validateConfig();
187 public void testWithAPSP_useIfcNameMismatch() throws Exception {
188 ConfigTransactionJMXClient transaction = configRegistryClient
189 .createTransaction();
190 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
193 ObjectName apspName = transaction.createModule(
194 TestingParallelAPSPModuleFactory.NAME, "apsp1");
195 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
196 apspName, TestingParallelAPSPConfigMXBean.class);
197 apspProxy.setThreadPool(ObjectNameUtil.createReadOnlyModuleON(
198 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1));
199 apspProxy.setSomeParam("someParam");
200 transaction.validateConfig();
201 transaction.commit();