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.fail;
13 import java.lang.management.ManagementFactory;
14 import java.lang.reflect.Method;
16 import java.util.Map.Entry;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.MBeanServer;
21 import javax.management.ObjectName;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.config.api.ConfigRegistry;
27 import org.opendaylight.controller.config.api.LookupRegistry;
28 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
29 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
30 import org.opendaylight.controller.config.util.jolokia.ConfigRegistryJolokiaClient;
31 import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaClient;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.Sets;
36 public class LookupTest {
38 private String jolokiaURL;
39 private TestingConfigRegistry testingRegistry;
40 private ObjectName testingRegistryON;
41 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
42 private ConfigRegistryClient jmxRegistryClient, jolokiaRegistryClient;
43 private ConfigTransactionControllerMXBean testingTransactionController;
44 private ObjectName testingTransactionControllerON;
45 private ConfigTransactionClient jmxTransactionClient,
46 jolokiaTransactionClient;
48 Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
51 public void setUp() throws Exception {
52 jolokiaURL = JolokiaHelper.startTestingJolokia();
53 testingRegistry = new TestingConfigRegistry();
54 testingRegistryON = ConfigRegistry.OBJECT_NAME;
55 mbs.registerMBean(testingRegistry, testingRegistryON);
56 jmxRegistryClient = new ConfigRegistryJMXClient(
57 ManagementFactory.getPlatformMBeanServer());
58 jolokiaRegistryClient = new ConfigRegistryJolokiaClient(jolokiaURL);
60 testingTransactionController = new TestingConfigTransactionController();
61 testingTransactionControllerON = new ObjectName(
62 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
63 + "=TransactionController");
64 mbs.registerMBean(testingTransactionController,
65 testingTransactionControllerON);
67 jmxTransactionClient = new ConfigTransactionJMXClient(null,
68 testingTransactionControllerON,
69 ManagementFactory.getPlatformMBeanServer());
70 jolokiaTransactionClient = new ConfigTransactionJolokiaClient(
71 jolokiaURL, testingTransactionControllerON, null);
72 lookupProvidersToClients = ImmutableMap
73 .of(testingRegistry, Sets.newHashSet(jmxRegistryClient, jolokiaRegistryClient),
74 testingTransactionController, Sets.newHashSet(jmxTransactionClient, jolokiaTransactionClient));
78 public void cleanUp() throws Exception {
79 JolokiaHelper.stopJolokia();
80 mbs.unregisterMBean(testingRegistryON);
81 mbs.unregisterMBean(testingTransactionControllerON);
85 public void testLookupConfigBeans() throws Exception {
86 Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
87 Object[] args = new Object[0];
92 public void testLookupConfigBeans1() throws Exception {
93 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
95 Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
100 public void testLookupConfigBeans2() throws Exception {
101 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
102 String.class, String.class);
103 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
104 TestingConfigRegistry.instName1 };
109 public void testLookupConfigBean() throws Exception {
110 Method method = LookupRegistry.class.getMethod("lookupConfigBean",
111 String.class, String.class);
112 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
113 TestingConfigRegistry.instName1 };
117 private void test(Method method, Object[] args) throws Exception {
118 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
120 Object expected = method.invoke(entry.getKey(), args);
121 for (LookupRegistry client : entry.getValue()) {
122 Object actual = method.invoke(client, args);
123 assertEquals("Error while comparing " + entry.getKey()
124 + " with client " + client, expected, actual);
130 public void testException() {
131 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
133 for (LookupRegistry client : entry.getValue()) {
135 client.lookupConfigBean(
136 InstanceNotFoundException.class.getSimpleName(), "");
137 fail(client.toString());
138 } catch (InstanceNotFoundException e) {