Merge "On openflow plugin restart, NPE in tx poller"
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / LookupTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.util;
9
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;
19
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;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.fail;
32
33 public class LookupTest {
34
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;
42
43     Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
44
45     @Before
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());
52
53
54         testingTransactionController = new TestingConfigTransactionController();
55         testingTransactionControllerON = new ObjectName(
56                 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
57                         + "=TransactionController");
58         mbs.registerMBean(testingTransactionController,
59                 testingTransactionControllerON);
60
61         jmxTransactionClient = new ConfigTransactionJMXClient(null,
62                 testingTransactionControllerON,
63                 ManagementFactory.getPlatformMBeanServer());
64
65         HashSet<ConfigRegistryClient> registryClients = Sets.newHashSet(jmxRegistryClient);
66         HashSet<ConfigTransactionClient> configTransactionClients = Sets.newHashSet(jmxTransactionClient);
67         lookupProvidersToClients = ImmutableMap.of((LookupRegistry) testingRegistry, registryClients,
68                 testingTransactionController, configTransactionClients);
69     }
70
71     @After
72     public void cleanUp() throws Exception {
73         mbs.unregisterMBean(testingRegistryON);
74         mbs.unregisterMBean(testingTransactionControllerON);
75     }
76
77     @Test
78     public void testLookupConfigBeans() throws Exception {
79         Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
80         Object[] args = new Object[0];
81         test(method, args);
82     }
83
84     @Test
85     public void testLookupConfigBeans1() throws Exception {
86         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
87                 String.class);
88         Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
89         test(method, args);
90     }
91
92     @Test
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 };
98         test(method, args);
99     }
100
101     @Test
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 };
107         test(method, args);
108     }
109
110     private void test(Method method, Object[] args) throws Exception {
111         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
112                 .entrySet()) {
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);
118             }
119         }
120     }
121
122     @Test
123     public void testException() {
124         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
125                 .entrySet()) {
126             for (LookupRegistry client : entry.getValue()) {
127                 try {
128                     client.lookupConfigBean(
129                             InstanceNotFoundException.class.getSimpleName(), "");
130                     fail(client.toString());
131                 } catch (InstanceNotFoundException e) {
132
133                 }
134             }
135         }
136     }
137 }