07492043248718c10e2f686f2a8df1761ba633d5
[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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import java.lang.management.ManagementFactory;
14 import java.lang.reflect.Method;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import javax.management.InstanceNotFoundException;
20 import javax.management.MBeanServer;
21 import javax.management.ObjectName;
22
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;
32
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.Sets;
35
36 public class LookupTest {
37
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;
47
48     Map<LookupRegistry, ? extends Set<? extends LookupRegistry>> lookupProvidersToClients;
49
50     @Before
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);
59
60         testingTransactionController = new TestingConfigTransactionController();
61         testingTransactionControllerON = new ObjectName(
62                 ObjectNameUtil.ON_DOMAIN + ":" + ObjectNameUtil.TYPE_KEY
63                         + "=TransactionController");
64         mbs.registerMBean(testingTransactionController,
65                 testingTransactionControllerON);
66
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));
75     }
76
77     @After
78     public void cleanUp() throws Exception {
79         JolokiaHelper.stopJolokia();
80         mbs.unregisterMBean(testingRegistryON);
81         mbs.unregisterMBean(testingTransactionControllerON);
82     }
83
84     @Test
85     public void testLookupConfigBeans() throws Exception {
86         Method method = LookupRegistry.class.getMethod("lookupConfigBeans");
87         Object[] args = new Object[0];
88         test(method, args);
89     }
90
91     @Test
92     public void testLookupConfigBeans1() throws Exception {
93         Method method = LookupRegistry.class.getMethod("lookupConfigBeans",
94                 String.class);
95         Object[] args = new Object[] { TestingConfigRegistry.moduleName1 };
96         test(method, args);
97     }
98
99     @Test
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 };
105         test(method, args);
106     }
107
108     @Test
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 };
114         test(method, args);
115     }
116
117     private void test(Method method, Object[] args) throws Exception {
118         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
119                 .entrySet()) {
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);
125             }
126         }
127     }
128
129     @Test
130     public void testException() {
131         for (Entry<LookupRegistry, ? extends Set<? extends LookupRegistry>> entry : lookupProvidersToClients
132                 .entrySet()) {
133             for (LookupRegistry client : entry.getValue()) {
134                 try {
135                     client.lookupConfigBean(
136                             InstanceNotFoundException.class.getSimpleName(), "");
137                     fail(client.toString());
138                 } catch (InstanceNotFoundException e) {
139
140                 }
141             }
142         }
143     }
144 }