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 com.google.common.collect.Sets;
16 import java.lang.management.ManagementFactory;
17 import java.util.Collections;
20 import javax.management.Attribute;
21 import javax.management.MBeanException;
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.opendaylight.controller.config.api.ValidationException;
30 import org.opendaylight.controller.config.api.ValidationException.ExceptionMessageWithStackTrace;
31 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
33 public class ConfigTransactionClientsTest {
34 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
35 private TestingConfigTransactionController transactionController;
36 private ObjectName transactionControllerON;
37 private ConfigTransactionClient jmxTransactionClient;
42 public void setUp() throws Exception {
43 transactionController = new TestingConfigTransactionController();
44 transactionControllerON = new ObjectName(ObjectNameUtil.ON_DOMAIN + ":"
45 + ObjectNameUtil.TYPE_KEY + "=TransactionController");
46 mbs.registerMBean(transactionController, transactionControllerON);
47 jmxTransactionClient = new ConfigTransactionJMXClient(null,
48 transactionControllerON,
49 ManagementFactory.getPlatformMBeanServer());
53 public void cleanUp() throws Exception {
54 if (transactionControllerON != null) {
55 mbs.unregisterMBean(transactionControllerON);
60 public void testLookupConfigBeans() throws Exception {
61 Set<ObjectName> jmxLookup = testClientLookupConfigBeans(jmxTransactionClient);
62 assertEquals(Sets.newHashSet(transactionController.conf1,
63 transactionController.conf2, transactionController.conf3),
67 private Set<ObjectName> testClientLookupConfigBeans(
68 final ConfigTransactionClient client) {
69 Set<ObjectName> beans = client.lookupConfigBeans();
70 for (ObjectName on : beans) {
71 assertEquals("Module", on.getKeyProperty("type"));
73 assertEquals(3, beans.size());
78 public void testGetObjectName() throws Exception {
79 testClientGetObjectName(jmxTransactionClient);
80 assertEquals(testClientGetObjectName(jmxTransactionClient), true);
83 private boolean testClientGetObjectName(final ConfigTransactionClient client) {
84 return transactionControllerON.equals(client.getObjectName());
88 public void testGetAvailableModuleNames() throws Exception {
89 Set<String> jmxMN = testClientGetAvailableModuleNames(jmxTransactionClient);
93 private Set<String> testClientGetAvailableModuleNames(
94 final ConfigTransactionClient client) {
95 return client.getAvailableModuleNames();
99 public void testGetTransactionName() throws Exception {
100 String jmxTN = testClientGetTransactionName(jmxTransactionClient);
101 assertEquals("transactionName", jmxTN);
104 private String testClientGetTransactionName(final ConfigTransactionClient client) {
105 return client.getTransactionName();
109 public void testGetVersion() throws Exception {
110 long jmxVersion = jmxTransactionClient.getVersion();
111 assertNull(jmxVersion);
115 public void testGetParentVersion() throws Exception {
116 long jmxParentVersion = jmxTransactionClient.getParentVersion();
117 assertNull(jmxParentVersion);
121 public void testValidateConfig() throws Exception {
122 jmxTransactionClient.validateConfig();
126 public void testAbortConfig() throws Exception {
127 jmxTransactionClient.abortConfig();
131 public void testDestroyModule2() throws Exception {
132 jmxTransactionClient.destroyModule("moduleB", "instB");
133 assertNull(transactionController.conf4);
137 public void testDestroyModule() throws Exception {
138 ObjectName on = testClientCreateModule(jmxTransactionClient);
139 jmxTransactionClient.destroyModule(on);
143 public void testCreateModule() throws Exception {
144 ObjectName on = testClientCreateModule(jmxTransactionClient);
145 Assert.assertNotNull(on);
148 private ObjectName testClientCreateModule(final ConfigTransactionClient client)
150 return client.createModule("testModuleName", "testInstanceName");
154 public void testAssertVersion() {
155 jmxTransactionClient.assertVersion((int)jmxTransactionClient.getParentVersion(),
156 (int)jmxTransactionClient.getVersion());
159 @Test(expected = NullPointerException.class)
160 public void testCommit() throws Exception {
161 jmxTransactionClient.commit();
165 public void testLookupConfigBeans2() throws Exception {
166 Set<ObjectName> jmxLookup = testClientLookupConfigBeans2(
167 jmxTransactionClient, "moduleB");
168 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
171 private Set<ObjectName> testClientLookupConfigBeans2(
172 final ConfigTransactionClient client, final String moduleName) {
173 Set<ObjectName> beans = client.lookupConfigBeans(moduleName);
174 assertEquals(1, beans.size());
179 public void testLookupConfigBean() throws Exception {
180 Set<ObjectName> jmxLookup = testClientLookupConfigBean(
181 jmxTransactionClient, "moduleB", "instB");
182 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
185 private Set<ObjectName> testClientLookupConfigBean(
186 final ConfigTransactionClient client, final String moduleName,
187 final String instanceName) {
188 Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
190 assertEquals(1, beans.size());
195 public void testLookupConfigBeans3() throws Exception {
196 Set<ObjectName> jmxLookup = testClientLookupConfigBeans3(
197 jmxTransactionClient, "moduleB", "instB");
198 assertEquals(Sets.newHashSet(transactionController.conf3), jmxLookup);
201 private Set<ObjectName> testClientLookupConfigBeans3(
202 final ConfigTransactionClient client, final String moduleName,
203 final String instanceName) {
204 Set<ObjectName> beans = client.lookupConfigBeans(moduleName,
206 assertEquals(1, beans.size());
211 public void testCheckConfigBeanExists() throws Exception {
212 jmxTransactionClient.checkConfigBeanExists(transactionControllerON);
213 assertEquals("configBeanExists", transactionController.check);
217 public void testSaveServiceReference() throws Exception {
218 assertEquals(transactionControllerON, jmxTransactionClient.saveServiceReference("serviceInterfaceName", "refName", transactionControllerON));
222 public void testRemoveServiceReference() throws Exception {
223 jmxTransactionClient.removeServiceReference("serviceInterface", "refName");
224 assertEquals("refName", transactionController.check);
228 public void testRemoveAllServiceReferences() throws Exception {
229 jmxTransactionClient.removeAllServiceReferences();
230 assertNull(transactionController.check);
234 public void testLookupConfigBeanByServiceInterfaceName() throws Exception {
235 assertEquals(transactionController.conf3, jmxTransactionClient.lookupConfigBeanByServiceInterfaceName("serviceInterface", "refName"));
239 public void testGetServiceMapping() throws Exception {
240 Assert.assertNotNull(jmxTransactionClient.getServiceMapping());
244 public void testLookupServiceReferencesByServiceInterfaceName() throws Exception {
245 Assert.assertNotNull(jmxTransactionClient.lookupServiceReferencesByServiceInterfaceName("serviceInterfaceQName"));
249 public void testLookupServiceInterfaceNames() throws Exception {
250 assertEquals(Sets.newHashSet("setA"), jmxTransactionClient.lookupServiceInterfaceNames(transactionControllerON));
254 public void testGetServiceInterfaceName() throws Exception {
255 assertEquals("namespace" + "localName", jmxTransactionClient.getServiceInterfaceName("namespace", "localName"));
259 public void removeServiceReferences() throws Exception {
260 assertEquals(true, jmxTransactionClient.removeServiceReferences(transactionControllerON));
264 public void testGetServiceReference() throws Exception {
265 assertEquals(transactionController.conf3, jmxTransactionClient.getServiceReference("serviceInterfaceQName", "refName"));
269 public void testCheckServiceReferenceExists() throws Exception {
270 jmxTransactionClient.checkServiceReferenceExists(transactionControllerON);
271 assertEquals("referenceExist", transactionController.check);
274 @Test(expected = RuntimeException.class)
275 public void testValidateBean() throws Exception {
276 jmxTransactionClient.validateBean(transactionControllerON);
279 @Test(expected = ValidationException.class)
280 public void testValidateBean2() throws Exception {
281 MBeanServer mbsLocal = mock(MBeanServer.class);
282 MBeanException mBeanException = new MBeanException(new ValidationException(
283 Collections.<String, Map<String, ExceptionMessageWithStackTrace>>emptyMap()));
284 doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
286 ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
287 transactionControllerON,
289 jmxTransactionClientFake.validateBean(transactionControllerON);
292 @Test(expected = RuntimeException.class)
293 public void testValidateBean3() throws Exception {
294 MBeanServer mbsLocal = mock(MBeanServer.class);
295 MBeanException mBeanException = new MBeanException(new RuntimeException());
296 doThrow(mBeanException).when(mbsLocal).invoke(transactionControllerON, "validate", null, null);
297 ConfigTransactionJMXClient jmxTransactionClientFake = new ConfigTransactionJMXClient(null,
298 transactionControllerON,
300 jmxTransactionClientFake.validateBean(transactionControllerON);
303 @Test(expected = IllegalArgumentException.class)
304 public void testSetAttribute() throws Exception {
306 jmxTransactionClient.setAttribute(transactionControllerON, "attrName", attr);
309 @Test(expected = IllegalArgumentException.class)
310 public void testGetAttribute() throws Exception {
311 attr = jmxTransactionClient.getAttribute(transactionController.conf3, "attrName");
316 public void testGetAvailableModuleFactoryQNames() throws Exception {
317 Assert.assertNotNull(jmxTransactionClient.getAvailableModuleFactoryQNames());