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.InstanceAlreadyExistsException;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.IntrospectionException;
21 import javax.management.ObjectName;
22 import javax.management.ReflectionException;
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.TestingScheduledThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
32 public class TwoInterfacesExportTest extends AbstractScheduledTest {
34 private void assertExists(final String moduleName, final String instanceName)
36 assertExists(null, moduleName, instanceName);
39 private void assertExists(@Nullable final ConfigTransactionJMXClient transaction,
40 final String moduleName, final String instanceName)
41 throws InstanceNotFoundException, IntrospectionException, ReflectionException {
42 if (transaction != null) {
43 transaction.lookupConfigBean(moduleName, instanceName);
45 platformMBeanServer.getMBeanInfo(ObjectNameUtil.createTransactionModuleON(
46 transaction.getTransactionName(), moduleName, instanceName));
48 configRegistryClient.lookupConfigBean(moduleName, instanceName);
50 platformMBeanServer.getMBeanInfo(ObjectNameUtil.createReadOnlyModuleON(moduleName,
55 private void assertNotExists(final String moduleName, final String instanceName) {
56 assertNotExists(null, moduleName, instanceName);
59 private void assertNotExists(
60 @Nullable final ConfigTransactionJMXClient transaction,
61 final String moduleName, final String instanceName) {
63 if (transaction != null) {
65 transaction.lookupConfigBean(moduleName, instanceName);
67 } catch (InstanceNotFoundException e) {
72 configRegistryClient.lookupConfigBean(moduleName, instanceName);
74 } catch (InstanceNotFoundException e) {
81 public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
82 ConfigTransactionJMXClient transaction = configRegistryClient
85 // create using TestingThreadPoolIfc:
86 ObjectName scheduled1name = transaction.createModule(
87 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
89 ObjectName retrievedName = transaction.lookupConfigBean(
90 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
91 assertEquals(scheduled1name, retrievedName);
93 // getExistingConfigBean should resolve moduleName
94 String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
95 retrievedName = transaction.lookupConfigBean(moduleName, scheduled1);
96 ObjectName expected = ObjectNameUtil.createTransactionModuleON(
97 transaction.getTransactionName(), moduleName, scheduled1);
98 assertEquals(expected, retrievedName);
101 transaction.commit();
102 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
103 assertFalse(TestingScheduledThreadPoolImpl.allExecutors.get(0)
106 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
108 assertExists(moduleName, scheduled1);
110 // destroy using ThreadPool ifc
111 transaction = configRegistryClient.createTransaction();
112 transaction.destroyModule(ObjectNameUtil.createTransactionModuleON(
113 transaction.getTransactionName(), moduleName, scheduled1));
114 transaction.commit();
115 assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
116 assertTrue(TestingScheduledThreadPoolImpl.allExecutors.get(0)
119 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
121 // should not be in platform:
123 assertNotExists(moduleName, scheduled1);
125 transaction = configRegistryClient.createTransaction();
126 // should not be in transaction
127 assertNotExists(transaction, moduleName, scheduled1);
131 public void tryToRegisterThreadPoolWithSameName()
132 throws InstanceAlreadyExistsException {
133 ConfigTransactionJMXClient transaction = configRegistryClient
134 .createTransaction();
136 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
139 transaction.createModule(
140 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
142 } catch (InstanceAlreadyExistsException e) {
145 containsString("There is an instance registered with name ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
151 public void testRegisteringAllIfcNames() throws Exception {
152 ConfigTransactionJMXClient transaction = configRegistryClient
153 .createTransaction();
154 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
156 transaction.commit();
157 assertExists(TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
158 // another transaction
159 transaction = configRegistryClient.createTransaction();
160 assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME,
165 public void testWithAPSP_useScheduledNames()
166 throws InstanceAlreadyExistsException, ValidationException {
167 ConfigTransactionJMXClient transaction = configRegistryClient
168 .createTransaction();
169 ObjectName scheduledName = transaction.createModule(
170 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
172 ObjectName apspName = transaction.createModule(
173 TestingParallelAPSPModuleFactory.NAME, "apsp1");
174 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(
175 apspName, TestingParallelAPSPConfigMXBean.class);
176 apspProxy.setThreadPool(scheduledName);
177 apspProxy.setSomeParam("someParam");
178 transaction.validateConfig();
183 public void testWithAPSP_useIfcNameMismatch() throws Exception {
184 ConfigTransactionJMXClient transaction = configRegistryClient
185 .createTransaction();
186 transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
189 ObjectName apspName = transaction.createModule(
190 TestingParallelAPSPModuleFactory.NAME, "apsp1");
191 TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(
192 apspName, TestingParallelAPSPConfigMXBean.class);
193 apspProxy.setThreadPool(ObjectNameUtil.createReadOnlyModuleON(
194 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1));
195 apspProxy.setSomeParam("someParam");
196 transaction.validateConfig();
197 transaction.commit();