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;
15 import java.util.HashSet;
17 import java.util.Map.Entry;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectName;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.ConfigRegistry;
28 import org.opendaylight.controller.config.api.LookupRegistry;
29 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
30 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
31 import org.opendaylight.controller.config.util.jolokia.ConfigRegistryJolokiaClient;
32 import org.opendaylight.controller.config.util.jolokia.ConfigTransactionJolokiaClient;
34 import com.google.common.collect.ImmutableMap;
35 import com.google.common.collect.Sets;
37 public class LookupTest {
39 private String jolokiaURL;
40 private TestingConfigRegistry testingRegistry;
41 private ObjectName testingRegistryON;
42 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
43 private ConfigRegistryClient jmxRegistryClient, jolokiaRegistryClient;
44 private ConfigTransactionControllerMXBean testingTransactionController;
45 private ObjectName testingTransactionControllerON;
46 private ConfigTransactionClient jmxTransactionClient,
47 jolokiaTransactionClient;
49 Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
52 public void setUp() throws Exception {
53 jolokiaURL = JolokiaHelper.startTestingJolokia();
54 testingRegistry = new TestingConfigRegistry();
55 testingRegistryON = ConfigRegistry.OBJECT_NAME;
56 mbs.registerMBean(testingRegistry, testingRegistryON);
57 jmxRegistryClient = new ConfigRegistryJMXClient(
58 ManagementFactory.getPlatformMBeanServer());
59 jolokiaRegistryClient = new ConfigRegistryJolokiaClient(jolokiaURL);
61 testingTransactionController = new TestingConfigTransactionController();
62 testingTransactionControllerON = new ObjectName(
63 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
64 + "=TransactionController");
65 mbs.registerMBean(testingTransactionController,
66 testingTransactionControllerON);
68 jmxTransactionClient = new ConfigTransactionJMXClient(null,
69 testingTransactionControllerON,
70 ManagementFactory.getPlatformMBeanServer());
71 jolokiaTransactionClient = new ConfigTransactionJolokiaClient(
72 jolokiaURL, testingTransactionControllerON, null);
73 HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient, jolokiaRegistryClient);
74 HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient, jolokiaTransactionClient);
75 lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients, testingTransactionController, configTransactionClients);
79 public void cleanUp() throws Exception {
80 JolokiaHelper.stopJolokia();
81 mbs.unregisterMBean(testingRegistryON);
82 mbs.unregisterMBean(testingTransactionControllerON);
86 public void testLookupConfigBeans() throws Exception {
87 Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
88 Object[] args = new Object[0];
93 public void testLookupConfigBeans1() throws Exception {
94 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
96 Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
101 public void testLookupConfigBeans2() throws Exception {
102 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
103 String.class, String.class);
104 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
105 TestingConfigRegistry.instName1 };
110 public void testLookupConfigBean() throws Exception {
111 Method method = LookupRegistry.class.getMethod("lookupConfigBean",
112 String.class, String.class);
113 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
114 TestingConfigRegistry.instName1 };
118 private void test(Method method, Object[] args) throws Exception {
119 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
121 Object expected = method.invoke(entry.getKey(), args);
122 for (LookupRegistry client : entry.getValue()) {
123 Object actual = method.invoke(client, args);
124 assertEquals("Error while comparing " + entry.getKey()
125 + " with client " + client, expected, actual);
131 public void testException() {
132 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
134 for (LookupRegistry client : entry.getValue()) {
136 client.lookupConfigBean(
137 InstanceNotFoundException.class.getSimpleName(), "");
138 fail(client.toString());
139 } catch (InstanceNotFoundException e) {