Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / test / TwoInterfacesExportTest.java
1 /*
2  * Copyright (c) 2013, 2017 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
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;
31
32 public class TwoInterfacesExportTest extends AbstractScheduledTest {
33
34     private void assertExists(final String moduleName, final String instanceName) throws Exception {
35         assertExists(null, moduleName, instanceName);
36     }
37
38     private void assertExists(@Nullable final ConfigTransactionJMXClient transaction, final String moduleName,
39             final String instanceName) throws InstanceNotFoundException, IntrospectionException, ReflectionException {
40         if (transaction != null) {
41             transaction.lookupConfigBean(moduleName, instanceName);
42             // make a dummy call
43             platformMBeanServer.getMBeanInfo(ObjectNameUtil.createTransactionModuleON(transaction.getTransactionName(),
44                     moduleName, instanceName));
45         } else {
46             configRegistryClient.lookupConfigBean(moduleName, instanceName);
47             // make a dummy call
48             platformMBeanServer.getMBeanInfo(ObjectNameUtil.createReadOnlyModuleON(moduleName, instanceName));
49         }
50     }
51
52     private void assertNotExists(final String moduleName, final String instanceName) {
53         assertNotExists(null, moduleName, instanceName);
54     }
55
56     private void assertNotExists(@Nullable final ConfigTransactionJMXClient transaction, final String moduleName,
57             final String instanceName) {
58
59         if (transaction != null) {
60             try {
61                 transaction.lookupConfigBean(moduleName, instanceName);
62                 fail();
63             } catch (final InstanceNotFoundException e) {
64                 // FIXME: should be empty?
65             }
66         } else {
67             try {
68                 configRegistryClient.lookupConfigBean(moduleName, instanceName);
69                 fail();
70             } catch (final InstanceNotFoundException e) {
71                 // FIXME: should be empty?
72             }
73         }
74     }
75
76     @Test
77     public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
78         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
79
80         // create using TestingThreadPoolIfc:
81         ObjectName scheduled1name = transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
82
83         ObjectName retrievedName = transaction.lookupConfigBean(TestingScheduledThreadPoolModuleFactory.NAME,
84                 SCHEDULED1);
85         assertEquals(scheduled1name, retrievedName);
86
87         // getExistingConfigBean should resolve moduleName
88         String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
89         retrievedName = transaction.lookupConfigBean(moduleName, SCHEDULED1);
90         ObjectName expected = ObjectNameUtil.createTransactionModuleON(transaction.getTransactionName(), moduleName,
91                 SCHEDULED1);
92         assertEquals(expected, retrievedName);
93
94         // commit
95         transaction.commit();
96         assertEquals(1, TestingScheduledThreadPoolImpl.ALLEXECUTORS.size());
97         assertFalse(TestingScheduledThreadPoolImpl.ALLEXECUTORS.get(0).isTerminated());
98         assertEquals(0, TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
99
100         assertExists(moduleName, SCHEDULED1);
101
102         // destroy using ThreadPool ifc
103         transaction = configRegistryClient.createTransaction();
104         transaction.destroyModule(
105                 ObjectNameUtil.createTransactionModuleON(transaction.getTransactionName(), moduleName, SCHEDULED1));
106         transaction.commit();
107         assertEquals(1, TestingScheduledThreadPoolImpl.ALLEXECUTORS.size());
108         assertTrue(TestingScheduledThreadPoolImpl.ALLEXECUTORS.get(0).isTerminated());
109         assertEquals(1, TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
110
111         // should not be in platform:
112
113         assertNotExists(moduleName, SCHEDULED1);
114
115         transaction = configRegistryClient.createTransaction();
116         // should not be in transaction
117         assertNotExists(transaction, moduleName, SCHEDULED1);
118     }
119
120     @Test
121     public void tryToRegisterThreadPoolWithSameName() throws InstanceAlreadyExistsException {
122         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
123
124         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
125         try {
126             transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
127             fail();
128         } catch (final InstanceAlreadyExistsException e) {
129             assertThat(e.getMessage(), containsString(
130                     "There is an instance registered with name "
131                     + "ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
132         }
133     }
134
135     // --
136     @Test
137     public void testRegisteringAllIfcNames() throws Exception {
138         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
139         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
140         transaction.commit();
141         assertExists(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
142         // another transaction
143         transaction = configRegistryClient.createTransaction();
144         assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
145     }
146
147     @Test
148     public void testWithAPSP_useScheduledNames() throws InstanceAlreadyExistsException, ValidationException {
149         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
150         ObjectName scheduledName = transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
151
152         ObjectName apspName = transaction.createModule(TestingParallelAPSPModuleFactory.NAME, "apsp1");
153         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(apspName,
154                 TestingParallelAPSPConfigMXBean.class);
155         apspProxy.setThreadPool(scheduledName);
156         apspProxy.setSomeParam("someParam");
157         transaction.validateConfig();
158
159     }
160
161     @Test
162     public void testWithAPSP_useIfcNameMismatch() throws Exception {
163         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
164         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1);
165
166         ObjectName apspName = transaction.createModule(TestingParallelAPSPModuleFactory.NAME, "apsp1");
167         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(apspName,
168                 TestingParallelAPSPConfigMXBean.class);
169         apspProxy.setThreadPool(
170                 ObjectNameUtil.createReadOnlyModuleON(TestingScheduledThreadPoolModuleFactory.NAME, SCHEDULED1));
171         apspProxy.setSomeParam("someParam");
172         transaction.validateConfig();
173         transaction.commit();
174
175     }
176
177 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.