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.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.mock;
15 import java.lang.management.ManagementFactory;
16 import java.util.Collections;
20 import javax.management.Attribute;
21 import javax.management.MBeanException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.controller.config.api.ValidationException;
31 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
32 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
34 import com.google.common.collect.Sets;
36 public class ConfigTransactionClientsTest {
37 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
38 private TestingConfigTransactionController transactionController;
39 private ObjectName transactionControllerON;
40 private ConfigTransactionClient jmxTransactionClient;
45 public void setUp() throws Exception {
46 transactionController = new TestingConfigTransactionController();
47 transactionControllerON = new ObjectName(ObjectNameUtil.ON_DOMAIN + ":"
48 + ObjectNameUtil.TYPE_KEY + "=TransactionController");
49 mbs.registerMBean(transactionController, transactionControllerON);
50 jmxTransactionClient = new ConfigTransactionJMXClient(null,
51 transactionControllerON,
52 ManagementFactory.getPlatformMBeanServer());
56 public void cleanUp() throws Exception {
57 if (transactionControllerON != null) {
58 mbs.unregisterMBean(transactionControllerON);
63 public void testLookupConfigBeans() throws Exception {
64 Set<ObjectName> jmxLookup = testClientLookupConfigBeans(jmxTransactionClient);
65 assertEquals(Sets.newHashSet(transactionController.conf1,
66 transactionController.conf2, transactionController.conf3),
70 private Set<ObjectName> testClientLookupConfigBeans(
71 ConfigTransactionClient client) {
72 Set<ObjectName> beans = client.lookupConfigBeans();
73 for (ObjectName on : beans) {
74 assertEquals("Module", on.getKeyProperty("type"));
76 assertEquals(3, beans.size());
81 public void testGetObjectName() throws Exception {
82 testClientGetObjectName(jmxTransactionClient);
83 assertEquals(testClientGetObjectName(jmxTransactionClient), true);
86 private boolean testClientGetObjectName(ConfigTransactionClient client) {
87 return transactionControllerON.equals(client.getObjectName());
91 public void testGetAvailableModuleNames() throws Exception {
92 Set<String> jmxMN = testClientGetAvailableModuleNames(jmxTransactionClient);
96 private Set<String> testClientGetAvailableModuleNames(
97 ConfigTransactionClient client) {
98 return client.getAvailableModuleNames();
102 public void testGetTransactionName() throws Exception {
103 String jmxTN = testClientGetTransactionName(jmxTransactionClient);
104 assertEquals("transactionName", jmxTN);
107 private String testClientGetTransactionName(ConfigTransactionClient client) {
108 return client.getTransactionName();
112 public void testGetVersion() throws Exception {
113 long jmxVersion = jmxTransactionClient.getVersion();
114 assertNull(jmxVersion);
118 public void testGetParentVersion() throws Exception {
119 long jmxParentVersion = jmxTransactionClient.getParentVersion();
120 assertNull(jmxParentVersion);
124 public void testValidateConfig() throws Exception {
125 jmxTransactionClient.validateConfig();
129 public void testAbortConfig() throws Exception {
130 jmxTransactionClient.abortConfig();
134 public void testDestroyModule2() throws Exception {
135 jmxTransactionClient.destroyModule("moduleB", "instB");
136 assertNull(transactionController.conf4);
140 public void testDestroyModule() throws Exception {
141 ObjectName on = testClientCreateModule(jmxTransactionClient);
142 jmxTransactionClient.destroyModule(on);
146 public void testCreateModule() throws Exception {
147 ObjectName on = testClientCreateModule(jmxTransactionClient);
148 Assert.assertNotNull(on);
151 private ObjectName testClientCreateModule(ConfigTransactionClient client)
153 return client.createModule("testModuleName", "testInstanceName");
157 public void testAssertVersion() {
158 jmxTransactionClient.assertVersion((int)jmxTransactionClient.getParentVersion(),
159 (int)jmxTransactionClient.getVersion());
162 @Test(expected = NullPointerException.class)
163 public void testCommit() throws Exception {
164 jmxTransactionClient.commit();
168 public void testLookupConfigBeans2() throws Exception {
169 Set<ObjectName> jmxLookup = testClientLookupConfigBeans2(
170 jmxTransactionClient, "moduleB");
171 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
174 private Set<ObjectName> testClientLookupConfigBeans2(
175 ConfigTransactionClient client, String moduleName) {
176 Set<ObjectName> beans = client.lookupConfigBeans(moduleName);
177 assertEquals(1, beans.size());
182 public void testLookupConfigBean() throws Exception {
183 Set<ObjectName> jmxLookup = testClientLookupConfigBean(
184 jmxTransactionClient, "moduleB", "instB");
185 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
188 private Set<ObjectName> testClientLookupConfigBean(
189 ConfigTransactionClient client, String moduleName,
190 String instanceName) {
191 Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
193 assertEquals(1, beans.size());
198 public void testLookupConfigBeans3() throws Exception {
199 Set<ObjectName> jmxLookup = testClientLookupConfigBeans3(
200 jmxTransactionClient, "moduleB", "instB");
201 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
204 private Set<ObjectName> testClientLookupConfigBeans3(
205 ConfigTransactionClient client, String moduleName,
206 String instanceName) {
207 Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
209 assertEquals(1, beans.size());
214 public void testCheckConfigBeanExists() throws Exception {
215 jmxTransactionClient.checkConfigBeanExists(transactionControllerON);
216 assertEquals("configBeanExists", transactionController.check);
220 public void testSaveServiceReference() throws Exception {
221 assertEquals(transactionControllerON, jmxTransactionClient.saveServiceReference("serviceInterfaceName", "refName", transactionControllerON));
225 public void testRemoveServiceReference() throws Exception {
226 jmxTransactionClient.removeServiceReference("serviceInterface", "refName");
227 assertEquals("refName", transactionController.check);
231 public void testRemoveAllServiceReferences() throws Exception {
232 jmxTransactionClient.removeAllServiceReferences();
233 assertNull(transactionController.check);
237 public void testLookupConfigBeanByServiceInterfaceName() throws Exception {
238 assertEquals(transactionController.conf3, jmxTransactionClient.lookupConfigBeanByServiceInterfaceName("serviceInterface", "refName"));
242 public void testGetServiceMapping() throws Exception {
243 Assert.assertNotNull(jmxTransactionClient.getServiceMapping());
247 public void testLookupServiceReferencesByServiceInterfaceName() throws Exception {
248 Assert.assertNotNull(jmxTransactionClient.lookupServiceReferencesByServiceInterfaceName("serviceInterfaceQName"));
252 public void testLookupServiceInterfaceNames() throws Exception {
253 assertEquals(Sets.newHashSet("setA"), jmxTransactionClient.lookupServiceInterfaceNames(transactionControllerON));
257 public void testGetServiceInterfaceName() throws Exception {
258 assertEquals("namespace" + "localName", jmxTransactionClient.getServiceInterfaceName("namespace", "localName"));
262 public void removeServiceReferences() throws Exception {
263 assertEquals(true, jmxTransactionClient.removeServiceReferences(transactionControllerON));
267 public void testGetServiceReference() throws Exception {
268 assertEquals(transactionController.conf3, jmxTransactionClient.getServiceReference("serviceInterfaceQName", "refName"));
272 public void testCheckServiceReferenceExists() throws Exception {
273 jmxTransactionClient.checkServiceReferenceExists(transactionControllerON);
274 assertEquals("referenceExist", transactionController.check);
277 @Test(expected = RuntimeException.class)
278 public void testValidateBean() throws Exception {
279 jmxTransactionClient.validateBean(transactionControllerON);
282 @Test(expected = ValidationException.class)
283 public void testValidateBean2() throws Exception {
284 MBeanServer mbsLocal = mock(MBeanServer.class);
285 MBeanException mBeanException = new MBeanException(new ValidationException(
286 Collections.<String, Map<String, ExceptionMessageWithStackTrace>>emptyMap()));
287 doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
289 ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
290 transactionControllerON,
292 jmxTransactionClientFake.validateBean(transactionControllerON);
295 @Test(expected = RuntimeException.class)
296 public void testValidateBean3() throws Exception {
297 MBeanServer mbsLocal = mock(MBeanServer.class);
298 MBeanException mBeanException = new MBeanException(new RuntimeException());
299 doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
300 ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
301 transactionControllerON,
303 jmxTransactionClientFake.validateBean(transactionControllerON);
306 @Test(expected = IllegalArgumentException.class)
307 public void testSetAttribute() throws Exception {
309 jmxTransactionClient.setAttribute(transactionControllerON, "attrName", attr);
312 @Test(expected = IllegalArgumentException.class)
313 public void testGetAttribute() throws Exception {
314 attr = jmxTransactionClient.getAttribute(transactionController.conf3, "attrName");
319 public void testGetAvailableModuleFactoryQNames() throws Exception {
320 Assert.assertNotNull(jmxTransactionClient.getAvailableModuleFactoryQNames());