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 com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Sets;
15 import java.lang.management.ManagementFactory;
16 import java.lang.reflect.Method;
17 import java.util.HashSet;
19 import java.util.Map.Entry;
21 import javax.management.InstanceNotFoundException;
22 import javax.management.MBeanServer;
23 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;
32 public class LookupTest {
34 private TestingConfigRegistry testingRegistry;
35 private ObjectName testingRegistryON;
36 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
37 private ConfigRegistryClient jmxRegistryClient;
38 private ConfigTransactionControllerMXBean testingTransactionController;
39 private ObjectName testingTransactionControllerON;
40 private ConfigTransactionClient jmxTransactionClient;
42 Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
45 public void setUp() throws Exception {
46 testingRegistry = new TestingConfigRegistry();
47 testingRegistryON = ConfigRegistry.OBJECT_NAME;
48 mbs.registerMBean(testingRegistry, testingRegistryON);
49 jmxRegistryClient = new ConfigRegistryJMXClient(
50 ManagementFactory.getPlatformMBeanServer());
53 testingTransactionController = new TestingConfigTransactionController();
54 testingTransactionControllerON = new ObjectName(
55 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
56 + "=TransactionController");
57 mbs.registerMBean(testingTransactionController,
58 testingTransactionControllerON);
60 jmxTransactionClient = new ConfigTransactionJMXClient(null,
61 testingTransactionControllerON,
62 ManagementFactory.getPlatformMBeanServer());
64 HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient);
65 HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient);
66 lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients,
67 testingTransactionController, configTransactionClients);
71 public void cleanUp() throws Exception {
72 mbs.unregisterMBean(testingRegistryON);
73 mbs.unregisterMBean(testingTransactionControllerON);
77 public void testLookupConfigBeans() throws Exception {
78 Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
79 Object[] args = new Object[0];
84 public void testLookupConfigBeans1() throws Exception {
85 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
87 Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
92 public void testLookupConfigBeans2() throws Exception {
93 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
94 String.class, String.class);
95 Object[] args = new Object[] { TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1 };
100 public void testLookupConfigBean() throws Exception {
101 Method method = LookupRegistry.class.getMethod("lookupConfigBean",
102 String.class, String.class);
103 Object[] args = new Object[] { TestingConfigRegistry.moduleName1, TestingConfigRegistry.instName1 };
107 private void test(final Method method, final Object[] args) throws Exception {
108 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
110 Object expected = method.invoke(entry.getKey(), args);
111 for (LookupRegistry client : entry.getValue()) {
112 Object actual = method.invoke(client, args);
113 assertEquals("Error while comparing " + entry.getKey()
114 + " with client " + client, expected, actual);
120 public void testException() {
121 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
123 for (LookupRegistry client : entry.getValue()) {
125 client.lookupConfigBean(
126 InstanceNotFoundException.class.getSimpleName(), "");
127 fail(client.toString());
128 } catch (final InstanceNotFoundException e) {