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.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
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;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.ValidationException;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
27 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
28 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
31 public class TwoInterfacesExportTest extends AbstractScheduledTest {
33 private void assertExists(final String moduleName, final String instanceName)
35 assertExists(null, moduleName, instanceName);
38 private void assertExists(@Nullable final ConfigTransactionJMXClient transaction,
39 final String moduleName, final String instanceName)
40 throws InstanceNotFoundException {
41 if (transaction != null) {
42 transaction.lookupConfigBean(moduleName, instanceName);
44 configRegistryClient.newMBeanProxy(
45 ObjectNameUtil.createTransactionModuleON(
46 transaction.getTransactionName(), moduleName,
47 instanceName), DynamicMBean.class).getMBeanInfo();
49 configRegistryClient.lookupConfigBean(moduleName, instanceName);
51 configRegistryClient.newMBeanProxy(
52 ObjectNameUtil.createReadOnlyModuleON(moduleName,
53 instanceName), DynamicMBean.class).getMBeanInfo();
57 private void assertNotExists(final String moduleName, final String instanceName) {
58 assertNotExists(null, moduleName, instanceName);
61 private void assertNotExists(
62 @Nullable final ConfigTransactionJMXClient transaction,
63 final String moduleName, final String instanceName) {
65 if (transaction != null) {
67 transaction.lookupConfigBean(moduleName, instanceName);
69 } catch (InstanceNotFoundException e) {
74 configRegistryClient.lookupConfigBean(moduleName, instanceName);
76 } catch (InstanceNotFoundException e) {
83 public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
84 ConfigTransactionJMXClient transaction = configRegistryClient
87 // create using TestingThreadPoolIfc:
88 ObjectName scheduled1name = transaction.createModule(
89 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
91 ObjectName retrievedName = transaction.lookupConfigBean(
92 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
93 assertEquals(scheduled1name, retrievedName);
95 // getExistingConfigBean should resolve moduleName
96 String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
97 retrievedName = transaction.lookupConfigBean(moduleName, scheduled1);
98 ObjectName expected = ObjectNameUtil.createTransactionModuleON(
99 transaction.getTransactionName(), moduleName, scheduled1);
100 assertEquals(expected, retrievedName);
103 transaction.commit();
104 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
105 assertFalse(TestingScheduledThreadPoolImpl.allExecutors.get(0)
108 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
110 assertExists(moduleName, scheduled1);
112 // destroy using ThreadPool ifc
113 transaction = configRegistryClient.createTransaction();
114 transaction.destroyModule(ObjectNameUtil.createTransactionModuleON(
115 transaction.getTransactionName(), moduleName, scheduled1));
116 transaction.commit();
117 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
118 assertTrue(TestingScheduledThreadPoolImpl.allExecutors.get(0)
121 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
123 // should not be in platform:
125 assertNotExists(moduleName, scheduled1);
127 transaction = configRegistryClient.createTransaction();
128 // should not be in transaction
129 assertNotExists(transaction, moduleName, scheduled1);
133 public void tryToRegisterThreadPoolWithSameName()
134 throws InstanceAlreadyExistsException {
135 ConfigTransactionJMXClient transaction = configRegistryClient
136 .createTransaction();
138 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
141 transaction.createModule(
142 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
144 } catch (InstanceAlreadyExistsException e) {
147 containsString("There is an instance registered with name ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
153 public void testRegisteringAllIfcNames() throws Exception {
154 ConfigTransactionJMXClient transaction = configRegistryClient
155 .createTransaction();
156 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
158 transaction.commit();
159 assertExists(TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
160 // another transaction
161 transaction = configRegistryClient.createTransaction();
162 assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME,
167 public void testWithAPSP_useScheduledNames()
168 throws InstanceAlreadyExistsException, ValidationException {
169 ConfigTransactionJMXClient transaction = configRegistryClient
170 .createTransaction();
171 ObjectName scheduledName = transaction.createModule(
172 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
174 ObjectName apspName = transaction.createModule(
175 TestingParallelAPSPModuleFactory.NAME, "apsp1");
176 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
177 apspName, TestingParallelAPSPConfigMXBean.class);
178 apspProxy.setThreadPool(scheduledName);
179 apspProxy.setSomeParam("someParam");
180 transaction.validateConfig();
185 public void testWithAPSP_useIfcNameMismatch() throws Exception {
186 ConfigTransactionJMXClient transaction = configRegistryClient
187 .createTransaction();
188 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
191 ObjectName apspName = transaction.createModule(
192 TestingParallelAPSPModuleFactory.NAME, "apsp1");
193 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
194 apspName, TestingParallelAPSPConfigMXBean.class);
195 apspProxy.setThreadPool(ObjectNameUtil.createReadOnlyModuleON(
196 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1));
197 apspProxy.setSomeParam("someParam");
198 transaction.validateConfig();
199 transaction.commit();