Merge "Bug 2233 - RPC register exception when rpc has no input"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / test / TwoInterfacesExportTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.test;
9
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;
16 import javax.annotation.Nullable;
17 import javax.management.DynamicMBean;
18 import javax.management.InstanceAlreadyExistsException;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.ObjectName;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
24 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
25 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
26 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
27 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
28 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
29
30 public class TwoInterfacesExportTest extends AbstractScheduledTest {
31
32     private void assertExists(final String moduleName, final String instanceName)
33             throws Exception {
34         assertExists(null, moduleName, instanceName);
35     }
36
37     private void assertExists(@Nullable final ConfigTransactionJMXClient transaction,
38             final String moduleName, final String instanceName)
39             throws InstanceNotFoundException {
40         if (transaction != null) {
41             transaction.lookupConfigBean(moduleName, instanceName);
42             // make a dummy call
43             configRegistryClient.newMBeanProxy(
44                     ObjectNameUtil.createTransactionModuleON(
45                             transaction.getTransactionName(), moduleName,
46                             instanceName), DynamicMBean.class).getMBeanInfo();
47         } else {
48             configRegistryClient.lookupConfigBean(moduleName, instanceName);
49             // make a dummy call
50             configRegistryClient.newMBeanProxy(
51                     ObjectNameUtil.createReadOnlyModuleON(moduleName,
52                             instanceName), DynamicMBean.class).getMBeanInfo();
53         }
54     }
55
56     private void assertNotExists(final String moduleName, final String instanceName) {
57         assertNotExists(null, moduleName, instanceName);
58     }
59
60     private void assertNotExists(
61             @Nullable final ConfigTransactionJMXClient transaction,
62             final String moduleName, final String instanceName) {
63
64         if (transaction != null) {
65             try {
66                 transaction.lookupConfigBean(moduleName, instanceName);
67                 fail();
68             } catch (InstanceNotFoundException e) {
69
70             }
71         } else {
72             try {
73                 configRegistryClient.lookupConfigBean(moduleName, instanceName);
74                 fail();
75             } catch (InstanceNotFoundException e) {
76
77             }
78         }
79     }
80
81     @Test
82     public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
83         ConfigTransactionJMXClient transaction = configRegistryClient
84                 .createTransaction();
85
86         // create using TestingThreadPoolIfc:
87         ObjectName scheduled1name = transaction.createModule(
88                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
89
90         ObjectName retrievedName = transaction.lookupConfigBean(
91                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
92         assertEquals(scheduled1name, retrievedName);
93
94         // getExistingConfigBean should resolve moduleName
95         String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
96         retrievedName = transaction.lookupConfigBean(moduleName, scheduled1);
97         ObjectName expected = ObjectNameUtil.createTransactionModuleON(
98                 transaction.getTransactionName(), moduleName, scheduled1);
99         assertEquals(expected, retrievedName);
100
101         // commit
102         transaction.commit();
103         assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
104         assertFalse(TestingScheduledThreadPoolImpl.allExecutors.get(0)
105                 .isTerminated());
106         assertEquals(0,
107                 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
108
109         assertExists(moduleName, scheduled1);
110
111         // destroy using ThreadPool ifc
112         transaction = configRegistryClient.createTransaction();
113         transaction.destroyModule(ObjectNameUtil.createTransactionModuleON(
114                 transaction.getTransactionName(), moduleName, scheduled1));
115         transaction.commit();
116         assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
117         assertTrue(TestingScheduledThreadPoolImpl.allExecutors.get(0)
118                 .isTerminated());
119         assertEquals(1,
120                 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
121
122         // should not be in platform:
123
124         assertNotExists(moduleName, scheduled1);
125
126         transaction = configRegistryClient.createTransaction();
127         // should not be in transaction
128         assertNotExists(transaction, moduleName, scheduled1);
129     }
130
131     @Test
132     public void tryToRegisterThreadPoolWithSameName()
133             throws InstanceAlreadyExistsException {
134         ConfigTransactionJMXClient transaction = configRegistryClient
135                 .createTransaction();
136
137         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
138                 scheduled1);
139         try {
140             transaction.createModule(
141                     TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
142             fail();
143         } catch (InstanceAlreadyExistsException e) {
144             assertThat(
145                     e.getMessage(),
146                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
147         }
148     }
149
150     // --
151     @Test
152     public void testRegisteringAllIfcNames() throws Exception {
153         ConfigTransactionJMXClient transaction = configRegistryClient
154                 .createTransaction();
155         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
156                 scheduled1);
157         transaction.commit();
158         assertExists(TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
159         // another transaction
160         transaction = configRegistryClient.createTransaction();
161         assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME,
162                 scheduled1);
163     }
164
165     @Test
166     public void testWithAPSP_useScheduledNames()
167             throws InstanceAlreadyExistsException, ValidationException {
168         ConfigTransactionJMXClient transaction = configRegistryClient
169                 .createTransaction();
170         ObjectName scheduledName = transaction.createModule(
171                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
172
173         ObjectName apspName = transaction.createModule(
174                 TestingParallelAPSPModuleFactory.NAME, "apsp1");
175         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
176                 apspName, TestingParallelAPSPConfigMXBean.class);
177         apspProxy.setThreadPool(scheduledName);
178         apspProxy.setSomeParam("someParam");
179         transaction.validateConfig();
180
181     }
182
183     @Test
184     public void testWithAPSP_useIfcNameMismatch() throws Exception {
185         ConfigTransactionJMXClient transaction = configRegistryClient
186                 .createTransaction();
187         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
188                 scheduled1);
189
190         ObjectName apspName = transaction.createModule(
191                 TestingParallelAPSPModuleFactory.NAME, "apsp1");
192         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMBeanProxy(
193                 apspName, TestingParallelAPSPConfigMXBean.class);
194         apspProxy.setThreadPool(ObjectNameUtil.createReadOnlyModuleON(
195                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1));
196         apspProxy.setSomeParam("someParam");
197         transaction.validateConfig();
198         transaction.commit();
199
200     }
201
202 }