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 com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.Sets;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.api.ConfigRegistry;
16 import org.opendaylight.controller.config.api.LookupRegistry;
17 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
18 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectName;
23 import java.lang.management.ManagementFactory;
24 import java.lang.reflect.Method;
25 import java.util.HashSet;
27 import java.util.Map.Entry;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.fail;
33 public class LookupTest {
35 private TestingConfigRegistry testingRegistry;
36 private ObjectName testingRegistryON;
37 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
38 private ConfigRegistryClient jmxRegistryClient;
39 private ConfigTransactionControllerMXBean testingTransactionController;
40 private ObjectName testingTransactionControllerON;
41 private ConfigTransactionClient jmxTransactionClient;
43 Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
46 public void setUp() throws Exception {
47 testingRegistry = new TestingConfigRegistry();
48 testingRegistryON = ConfigRegistry.OBJECT_NAME;
49 mbs.registerMBean(testingRegistry, testingRegistryON);
50 jmxRegistryClient = new ConfigRegistryJMXClient(
51 ManagementFactory.getPlatformMBeanServer());
54 testingTransactionController = new TestingConfigTransactionController();
55 testingTransactionControllerON = new ObjectName(
56 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
57 + "=TransactionController");
58 mbs.registerMBean(testingTransactionController,
59 testingTransactionControllerON);
61 jmxTransactionClient = new ConfigTransactionJMXClient(null,
62 testingTransactionControllerON,
63 ManagementFactory.getPlatformMBeanServer());
65 HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient);
66 HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient);
67 lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients,
68 testingTransactionController, configTransactionClients);
72 public void cleanUp() throws Exception {
73 mbs.unregisterMBean(testingRegistryON);
74 mbs.unregisterMBean(testingTransactionControllerON);
78 public void testLookupConfigBeans() throws Exception {
79 Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
80 Object[] args = new Object[0];
85 public void testLookupConfigBeans1() throws Exception {
86 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
88 Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
93 public void testLookupConfigBeans2() throws Exception {
94 Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
95 String.class, String.class);
96 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
97 TestingConfigRegistry.instName1 };
102 public void testLookupConfigBean() throws Exception {
103 Method method = LookupRegistry.class.getMethod("lookupConfigBean",
104 String.class, String.class);
105 Object[] args = new Object[] { TestingConfigRegistry.moduleName1,
106 TestingConfigRegistry.instName1 };
110 private void test(Method method, Object[] args) throws Exception {
111 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
113 Object expected = method.invoke(entry.getKey(), args);
114 for (LookupRegistry client : entry.getValue()) {
115 Object actual = method.invoke(client, args);
116 assertEquals("Error while comparing " + entry.getKey()
117 + " with client " + client, expected, actual);
123 public void testException() {
124 for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
126 for (LookupRegistry client : entry.getValue()) {
128 client.lookupConfigBean(
129 InstanceNotFoundException.class.getSimpleName(), "");
130 fail(client.toString());
131 } catch (InstanceNotFoundException e) {